Skip to content

Commit

Permalink
add olah entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
jstzwj committed Sep 4, 2024
1 parent 2365951 commit e4ec74d
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 21 deletions.
2 changes: 2 additions & 0 deletions assets/full_configs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ port = 8090
ssl-key = ""
ssl-cert = ""
repos-path = "./repos"
cache-size-limit = ""
cache-clean-strategy = "LRU"
hf-scheme = "https"
hf-netloc = "huggingface.co"
hf-lfs-netloc = "cdn-lfs.huggingface.co"
Expand Down
2 changes: 0 additions & 2 deletions olah/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class FileLevelLRU(BaseModel):

db.connect()
db.create_tables([
User,
Token,
UserToken,
DownloadLogs,
FileLevelLRU,
])
3 changes: 2 additions & 1 deletion olah/mirror/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def get_pathinfos(
results.append(self._get_path_info(index_obj))

for r in results:
r.pop("name")
if "name" in r:
r.pop("name")
return results

def get_tree(
Expand Down
10 changes: 5 additions & 5 deletions olah/proxy/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
from olah.cache.olah_cache import OlahCache
from olah.proxy.pathsinfo import pathsinfo_generator
from olah.utils.cache_utils import _read_cache_request, _write_cache_request
from olah.utils.cache_utils import read_cache_request, write_cache_request
from olah.utils.disk_utils import touch_file_access_time
from olah.utils.url_utils import (
RemoteInfo,
Expand Down Expand Up @@ -97,7 +97,7 @@ async def _file_full_header(
assert method.lower() == "head"
if not app.app_settings.config.offline:
if os.path.exists(head_path):
cache_rq = await _read_cache_request(head_path)
cache_rq = await read_cache_request(head_path)
response_headers_dict = {
k.lower(): v for k, v in cache_rq["headers"].items()
}
Expand All @@ -122,7 +122,7 @@ async def _file_full_header(
response_headers_dict = {k.lower(): v for k, v in response.headers.items()}
if allow_cache and method.lower() == "head":
if response.status_code == 200:
await _write_cache_request(
await write_cache_request(
head_path,
response.status_code,
response_headers_dict,
Expand All @@ -148,7 +148,7 @@ async def _file_full_header(
ORIGINAL_LOC,
response.headers["location"],
)
await _write_cache_request(
await write_cache_request(
head_path,
response.status_code,
response_headers_dict,
Expand All @@ -165,7 +165,7 @@ async def _file_full_header(
return response.status_code, response_headers_dict, response.content
else:
if os.path.exists(head_path):
cache_rq = await _read_cache_request(head_path)
cache_rq = await read_cache_request(head_path)
response_headers_dict = {
k.lower(): v for k, v in cache_rq["headers"].items()
}
Expand Down
6 changes: 3 additions & 3 deletions olah/proxy/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import httpx
from olah.constants import CHUNK_SIZE, WORKER_API_TIMEOUT

from olah.utils.cache_utils import _read_cache_request, _write_cache_request
from olah.utils.cache_utils import read_cache_request, write_cache_request
from olah.utils.rule_utils import check_cache_rules_hf
from olah.utils.repo_utils import get_org_repo
from olah.utils.file_utils import make_dirs

async def _meta_cache_generator(save_path: str):
cache_rq = await _read_cache_request(save_path)
cache_rq = await read_cache_request(save_path)
yield cache_rq["headers"]
yield cache_rq["content"]

Expand Down Expand Up @@ -57,7 +57,7 @@ async def _meta_proxy_generator(
content += chunk

if allow_cache and response_status_code == 200:
await _write_cache_request(
await write_cache_request(
save_path, response_status_code, response_headers, bytes(content)
)

Expand Down
6 changes: 3 additions & 3 deletions olah/proxy/pathsinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import httpx
from olah.constants import CHUNK_SIZE, WORKER_API_TIMEOUT

from olah.utils.cache_utils import _read_cache_request, _write_cache_request
from olah.utils.cache_utils import read_cache_request, write_cache_request
from olah.utils.rule_utils import check_cache_rules_hf
from olah.utils.repo_utils import get_org_repo
from olah.utils.file_utils import make_dirs


async def _pathsinfo_cache(save_path: str):
cache_rq = await _read_cache_request(save_path)
cache_rq = await read_cache_request(save_path)
return cache_rq["status_code"], cache_rq["headers"], cache_rq["content"]


Expand All @@ -48,7 +48,7 @@ async def _pathsinfo_proxy(

if allow_cache and response.status_code == 200:
make_dirs(save_path)
await _write_cache_request(
await write_cache_request(
save_path,
response.status_code,
response.headers,
Expand Down
6 changes: 3 additions & 3 deletions olah/proxy/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import httpx
from olah.constants import CHUNK_SIZE, WORKER_API_TIMEOUT

from olah.utils.cache_utils import _read_cache_request, _write_cache_request
from olah.utils.cache_utils import read_cache_request, write_cache_request
from olah.utils.rule_utils import check_cache_rules_hf
from olah.utils.repo_utils import get_org_repo
from olah.utils.file_utils import make_dirs


async def _tree_cache_generator(save_path: str):
cache_rq = await _read_cache_request(save_path)
cache_rq = await read_cache_request(save_path)
yield cache_rq["status_code"]
yield cache_rq["headers"]
yield cache_rq["content"]
Expand Down Expand Up @@ -60,7 +60,7 @@ async def _tree_proxy_generator(

if allow_cache and response_status_code == 200:
make_dirs(save_path)
await _write_cache_request(
await write_cache_request(
save_path, response_status_code, response_headers, bytes(content)
)

Expand Down
24 changes: 22 additions & 2 deletions olah/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,8 @@ async def repos(request: Request):
},
)

if __name__ in ["__main__", "olah.server"]:

def init():
parser = argparse.ArgumentParser(
description="Olah Huggingface Mirror Server."
)
Expand Down Expand Up @@ -959,9 +960,12 @@ def is_default_value(args, arg_name):

# Init app settings
app.app_settings = AppSettings(config=config)
return args

import uvicorn
def main():
args = init()
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"olah.server:app",
host=args.host,
Expand All @@ -971,3 +975,19 @@ def is_default_value(args, arg_name):
ssl_keyfile=args.ssl_key,
ssl_certfile=args.ssl_cert
)

def cli():
args = init()
import uvicorn
uvicorn.run(
"olah.server:app",
host=args.host,
port=args.port,
log_level="info",
reload=False,
ssl_keyfile=args.ssl_key,
ssl_certfile=args.ssl_cert
)

if __name__ in ["olah.server", "__main__"]:
main()
4 changes: 2 additions & 2 deletions olah/utils/cache_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Dict, Mapping, Union


async def _write_cache_request(
async def write_cache_request(
save_path: str,
status_code: int,
headers: Union[Dict[str, str], Mapping],
Expand Down Expand Up @@ -39,7 +39,7 @@ async def _write_cache_request(
f.write(json.dumps(rq, ensure_ascii=False))


async def _read_cache_request(save_path: str) -> Dict[str, str]:
async def read_cache_request(save_path: str) -> Dict[str, str]:
"""
Read the request's status code, headers, and content from a cache file.
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ dev = ["black==24.4.2", "pylint==3.2.5", "pytest==8.2.2"]
"Homepage" = "https://github.com/vtuber-plan/olah"
"Bug Tracker" = "https://github.com/vtuber-plan/olah/issues"

[project.scripts]
olah-cli = "olah.server:cli"

[tool.setuptools.packages.find]
exclude = ["assets*", "benchmark*", "docs", "dist*", "playground*", "scripts*", "tests*"]

Expand Down

0 comments on commit e4ec74d

Please sign in to comment.