Download app/main.py from 000x/MusicGen: direct link, hf CLI and curl.
- Browser
- Download file 3.09 kB
-
https://huggingface.co/spaces/000x/MusicGen/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/000x/MusicGen/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/000x/MusicGen/resolve/main/app/main.py
3.09 kB
| """Entry point: uvicorn app.main:app""" | |
| from __future__ import annotations | |
| import asyncio | |
| import logging | |
| import os | |
| import shutil | |
| import tempfile | |
| import time | |
| from contextlib import asynccontextmanager | |
| from types import SimpleNamespace | |
| from fastapi import FastAPI | |
| from .bot import Bot | |
| from .config import load | |
| from .crypto import Sealer | |
| from .db import Database | |
| from .sweeper import run_forever | |
| from .wa import WhatsApp | |
| from .web import create_app | |
| logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s") | |
| log = logging.getLogger("main") | |
| STATIC_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "public") | |
| def _prepare_media_dir(path: str) -> None: | |
| """Temp folder for media, owned by this app: created here, cleaned on start and stop.""" | |
| os.makedirs(path, exist_ok=True) | |
| for name in os.listdir(path): | |
| full = os.path.join(path, name) | |
| try: | |
| if os.path.isdir(full): | |
| shutil.rmtree(full) | |
| else: | |
| os.unlink(full) | |
| except OSError: | |
| pass | |
| os.environ["TMPDIR"] = os.environ["TEMP"] = os.environ["TMP"] = path | |
| tempfile.tempdir = path | |
| async def _cleanup_media_dir(path: str, max_age: float = 15 * 60) -> None: | |
| while True: | |
| await asyncio.sleep(30 * 60) | |
| for name in os.listdir(path): | |
| full = os.path.join(path, name) | |
| try: | |
| if time.time() - os.path.getmtime(full) > max_age: | |
| if os.path.isfile(full): | |
| os.unlink(full) | |
| else: | |
| shutil.rmtree(full) | |
| except OSError: | |
| pass | |
| async def lifespan(app: FastAPI): | |
| tasks: list[asyncio.Task] = [] | |
| if getattr(app.state, "services", None) is None: | |
| cfg = load() | |
| _prepare_media_dir(cfg.media_tmp_dir) | |
| db = await Database.connect(cfg.database_url, cfg.database_ssl, cfg.auto_delete_default) | |
| await db.init() | |
| for phone in cfg.admin_phones: | |
| await db.touch_user(phone) | |
| await db.update_user(phone, {"role": "admin"}) | |
| sealer = Sealer(cfg.session_key) | |
| bot = Bot(db) | |
| wa = WhatsApp(db, sealer, bot, cfg.session_dir, cfg.media_tmp_dir, cfg.bot_number) | |
| app.state.services = SimpleNamespace(cfg=cfg, db=db, wa=wa, sealer=sealer, bot=bot) | |
| try: | |
| await wa.start() | |
| except Exception as e: | |
| log.error("WhatsApp did not start: %s. The dashboard still works; retry from the dashboard.", e) | |
| tasks.append(asyncio.create_task(run_forever(db, wa.delete_for_everyone, wa.is_connected))) | |
| tasks.append(asyncio.create_task(_cleanup_media_dir(cfg.media_tmp_dir))) | |
| log.info("File Desk ready on port %s", cfg.port) | |
| try: | |
| yield | |
| finally: | |
| for task in tasks: | |
| task.cancel() | |
| s = app.state.services | |
| await s.wa.shutdown() | |
| await s.db.close() | |
| app = create_app(STATIC_DIR) | |
| app.router.lifespan_context = lifespan | |