Spaces:
Runtime error
Runtime error
Commit
•
10c1724
1
Parent(s):
848ef5f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
Hugging Face's logo Hugging Face
|
3 |
+
|
4 |
+
Models
|
5 |
+
Datasets
|
6 |
+
Spaces
|
7 |
+
Docs
|
8 |
+
Pricing
|
9 |
+
Spaces:
|
10 |
+
huggingface-projects
|
11 |
+
/
|
12 |
+
sd-multiplayer-bot
|
13 |
+
private
|
14 |
+
App
|
15 |
+
Files and versions
|
16 |
+
Community
|
17 |
+
Settings
|
18 |
+
sd-multiplayer-bot
|
19 |
+
/ app.py
|
20 |
+
radames's picture
|
21 |
+
radames
|
22 |
+
HF staff
|
23 |
+
add uuid from files
|
24 |
+
f6a107f
|
25 |
+
unverified
|
26 |
+
about 2 months ago
|
27 |
+
raw
|
28 |
+
history
|
29 |
+
blame
|
30 |
+
contribute
|
31 |
+
delete
|
32 |
+
3.65 kB
|
33 |
+
import boto3
|
34 |
+
import os
|
35 |
+
import re
|
36 |
+
import json
|
37 |
+
from pathlib import Path
|
38 |
+
import sqlite3
|
39 |
+
from huggingface_hub import Repository, HfFolder
|
40 |
+
import tqdm
|
41 |
+
import subprocess
|
42 |
+
|
43 |
+
from fastapi import FastAPI
|
44 |
+
from fastapi_utils.tasks import repeat_every
|
45 |
+
|
46 |
+
|
47 |
+
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
|
48 |
+
AWS_SECRET_KEY = os.getenv('AWS_SECRET_KEY')
|
49 |
+
AWS_S3_BUCKET_NAME = os.getenv('AWS_S3_BUCKET_NAME')
|
50 |
+
|
51 |
+
s3 = boto3.client(service_name='s3',
|
52 |
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
53 |
+
aws_secret_access_key=AWS_SECRET_KEY)
|
54 |
+
|
55 |
+
|
56 |
+
paginator = s3.get_paginator('list_objects_v2')
|
57 |
+
|
58 |
+
|
59 |
+
S3_DATA_FOLDER = Path("sd-multiplayer-data")
|
60 |
+
ROOMS_DATA_DB = S3_DATA_FOLDER / "rooms_data.db"
|
61 |
+
|
62 |
+
|
63 |
+
repo = Repository(
|
64 |
+
local_dir=S3_DATA_FOLDER,
|
65 |
+
repo_type="dataset",
|
66 |
+
clone_from="huggingface-projects/sd-multiplayer-data",
|
67 |
+
use_auth_token=True,
|
68 |
+
)
|
69 |
+
repo.git_pull()
|
70 |
+
|
71 |
+
|
72 |
+
if not ROOMS_DATA_DB.exists():
|
73 |
+
print("Creating database")
|
74 |
+
print("ROOMS_DATA_DB", ROOMS_DATA_DB)
|
75 |
+
db = sqlite3.connect(ROOMS_DATA_DB)
|
76 |
+
with open(Path("schema.sql"), "r") as f:
|
77 |
+
db.executescript(f.read())
|
78 |
+
db.commit()
|
79 |
+
db.close()
|
80 |
+
|
81 |
+
|
82 |
+
def get_db(db_path):
|
83 |
+
db = sqlite3.connect(db_path, check_same_thread=False)
|
84 |
+
db.row_factory = sqlite3.Row
|
85 |
+
try:
|
86 |
+
yield db
|
87 |
+
except Exception:
|
88 |
+
db.rollback()
|
89 |
+
finally:
|
90 |
+
db.close()
|
91 |
+
|
92 |
+
|
93 |
+
def sync_rooms_to_dataset():
|
94 |
+
for room_data_db in get_db(ROOMS_DATA_DB):
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
print(e)
|
98 |
+
continue
|
99 |
+
|
100 |
+
objects += results
|
101 |
+
room_data_db.commit()
|
102 |
+
|
103 |
+
all_rows = [dict(row) for row in room_data_db.execute(
|
104 |
+
"SELECT * FROM rooms_data WHERE room_id = ?", (room_id,)).fetchall()]
|
105 |
+
data_path = S3_DATA_FOLDER / f"{room_id}.json"
|
106 |
+
with open(data_path, 'w') as f:
|
107 |
+
json.dump(all_rows, f, separators=(',', ':'))
|
108 |
+
print("Updating repository")
|
109 |
+
subprocess.Popen(
|
110 |
+
"git add . && git commit --amend -m 'update' && git push --force", cwd=S3_DATA_FOLDER, shell=True)
|
111 |
+
|
112 |
+
|
113 |
+
app = FastAPI()
|
114 |
+
|
115 |
+
|
116 |
+
@app.post("/")
|
117 |
+
def read_root():
|
118 |
+
return "Just a bot to sync data to huggingface datasets and tweet tha latest data"
|
119 |
+
|
120 |
+
|
121 |
+
@app.post("/sync")
|
122 |
+
def sync():
|
123 |
+
sync_rooms_to_dataset()
|
124 |
+
return "Synced data to huggingface datasets"
|
125 |
+
|
126 |
+
|
127 |
+
@app.on_event("startup")
|
128 |
+
@repeat_every(seconds=1800)
|
129 |
+
def repeat_sync():
|
130 |
+
sync_rooms_to_dataset()
|
131 |
+
return "Synced data to huggingface datasets"
|