import os import re import json from pathlib import Path import sqlite3 from huggingface_hub import Repository, HfFolder import tqdm import subprocess import uvicorn from fastapi import FastAPI from fastapi_utils.tasks import repeat_every from fastapi.staticfiles import StaticFiles DATA_FOLDER = Path("data") DATA_DB = DATA_FOLDER / "rooms_data.db" repo = Repository( local_dir=DATA_FOLDER, repo_type="dataset", clone_from="triple-t/dummy", use_auth_token=True, ) # repo.git_pull() if not DATA_DB.exists(): print("Creating database") print("DATA_DB", DATA_DB) db = sqlite3.connect(DATA_DB) with open(Path("schema.sql"), "r") as f: db.executescript(f.read()) db.commit() db.close() def get_db(db_path): db = sqlite3.connect(db_path, check_same_thread=False) db.row_factory = sqlite3.Row try: yield db except Exception: db.rollback() finally: db.close() app = FastAPI() app.mount("/", StaticFiles(directory="./static", html=True), name="static") @app.on_event("startup") @repeat_every(seconds=1800) def repeat_sync(): return "Synced data to huggingface datasets" if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug", reload=False)