SGS-1 / app.py
spectralrohan's picture
first commit
8a98a08
import os
import sys
from pathlib import Path
from huggingface_hub import snapshot_download
import importlib.util
# from dotenv import load_dotenv
# load_dotenv()
def setup_cache_directory():
cache_dir = Path("cache")
cache_dir.mkdir(exist_ok=True)
return cache_dir
def download_space(cache_dir):
token = os.environ.get("HF_TOKEN")
repo_id = os.environ.get("REPO_ID")
if not token or not repo_id:
return False
try:
snapshot_download(
repo_id=repo_id,
repo_type="space",
local_dir=cache_dir,
token=token
)
return True
except:
return False
def load_app(cache_dir):
sys.path.insert(0, str(cache_dir))
app_path = cache_dir / "app.py"
spec = importlib.util.spec_from_file_location("app", app_path)
app = importlib.util.module_from_spec(spec)
spec.loader.exec_module(app)
return app.demo
if __name__ == "__main__":
cache_dir = setup_cache_directory()
if download_space(cache_dir):
demo = load_app(cache_dir)
demo.launch()