File size: 1,787 Bytes
55d21bf
 
 
 
 
 
 
 
bb48ea5
 
55d21bf
8c273bb
55d21bf
 
 
 
218f679
55d21bf
 
 
bb48ea5
55d21bf
bb48ea5
55d21bf
 
bb48ea5
8c273bb
dd09e83
 
 
 
 
 
 
 
55d21bf
 
 
 
dd09e83
55d21bf
 
bb48ea5
 
 
 
 
 
 
55d21bf
bb48ea5
 
8c273bb
bb48ea5
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
import sqlite3
import huggingface_hub
import gradio as gr
import pandas as pd
import shutil
import os
import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from loguru import logger
from common.util import date_str

DB_FILE = "app.db"
TOKEN = os.environ.get('HUB_TOKEN')
repo = huggingface_hub.Repository(
    local_dir="data",
    repo_type="dataset",
    clone_from="linxy/oh-my-words",
    use_auth_token=TOKEN
)
repo.git_pull()
DATASET_FILE = f"./data/{DB_FILE}"
# Set db to latest
shutil.copyfile(DATASET_FILE, DB_FILE)

def backup_db():
    shutil.copyfile(DB_FILE, DATASET_FILE)
    logger.info(f"save database at {DB_FILE}")
    # db = sqlite3.connect(DB_FILE)
    # pd.DataFrame(db.execute("SELECT * FROM word").fetchall()).to_csv("./data/word.csv", index=False)
    # logger.info("save word.csv")
    # pd.DataFrame(db.execute("SELECT * FROM book").fetchall()).to_csv("./data/book.csv", index=False)
    # logger.info("save book.csv")
    # pd.DataFrame(db.execute("SELECT * FROM unit").fetchall()).to_csv("./data/unit.csv", index=False)
    # logger.info("save unit.csv")
    # db.close()
    repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")


scheduler = BackgroundScheduler()
scheduler.add_job(func=backup_db, trigger="interval", seconds=60 * 30)
scheduler.start()

# def load_data():
#     db = sqlite3.connect(DB_FILE)
#     reviews, total_reviews = get_latest_reviews(db)
#     db.close()
#     return reviews, total_reviews

# demo.load(load_data, None, [data, count])

if __name__ == "__main__":
    logger.add(f"output/logs/web_{date_str}.log", rotation="1 day", retention="7 days", level="INFO")
    from web import demo
    demo.launch()