File size: 1,299 Bytes
feb4f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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)