Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
mrfakename
commited on
Commit
•
5e67379
1
Parent(s):
9d1fc61
Sync DB
Browse files
app.py
CHANGED
@@ -70,7 +70,7 @@ def create_db():
|
|
70 |
downvote INTEGER
|
71 |
);
|
72 |
''')
|
73 |
-
|
74 |
def get_data():
|
75 |
conn = get_db()
|
76 |
cursor = conn.cursor()
|
@@ -192,14 +192,46 @@ with gr.Blocks(theme=theme, css="footer {visibility: hidden}") as demo:
|
|
192 |
gr.Markdown(DESCR)
|
193 |
gr.TabbedInterface([vote, leaderboard], ['Vote', 'Leaderboard'])
|
194 |
def restart_space():
|
195 |
-
time.sleep(60)
|
196 |
-
print("Restarting space")
|
197 |
api = HfApi(
|
198 |
token=os.getenv('HF_TOKEN')
|
199 |
)
|
|
|
|
|
200 |
api.restart_space(repo_id=os.getenv('HF_ID'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
if os.getenv('HF_ID'):
|
202 |
restart_thread = threading.Thread(target=restart_space)
|
203 |
restart_thread.daemon = True
|
204 |
restart_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
demo.queue(api_open=False).launch(show_api=False)
|
|
|
70 |
downvote INTEGER
|
71 |
);
|
72 |
''')
|
73 |
+
|
74 |
def get_data():
|
75 |
conn = get_db()
|
76 |
cursor = conn.cursor()
|
|
|
192 |
gr.Markdown(DESCR)
|
193 |
gr.TabbedInterface([vote, leaderboard], ['Vote', 'Leaderboard'])
|
194 |
def restart_space():
|
|
|
|
|
195 |
api = HfApi(
|
196 |
token=os.getenv('HF_TOKEN')
|
197 |
)
|
198 |
+
time.sleep(60 * 60) # Every hour
|
199 |
+
print("Restarting space")
|
200 |
api.restart_space(repo_id=os.getenv('HF_ID'))
|
201 |
+
def sync_db():
|
202 |
+
api = HfApi(
|
203 |
+
token=os.getenv('HF_TOKEN')
|
204 |
+
)
|
205 |
+
while True:
|
206 |
+
time.sleep(60 * 15)
|
207 |
+
print("Uploading DB")
|
208 |
+
api.upload_file(
|
209 |
+
path_or_fileobj='database.db',
|
210 |
+
path_in_repo='database.db',
|
211 |
+
repo_id=os.getenv('DATASET_ID'),
|
212 |
+
repo_type='dataset'
|
213 |
+
)
|
214 |
if os.getenv('HF_ID'):
|
215 |
restart_thread = threading.Thread(target=restart_space)
|
216 |
restart_thread.daemon = True
|
217 |
restart_thread.start()
|
218 |
+
if os.getenv('DATASET_ID'):
|
219 |
+
# Fetch DB
|
220 |
+
api = HfApi(
|
221 |
+
token=os.getenv('HF_TOKEN')
|
222 |
+
)
|
223 |
+
try:
|
224 |
+
api.hf_hub_download(
|
225 |
+
repo_id=os.getenv('DATASET_ID'),
|
226 |
+
repo_type='dataset',
|
227 |
+
filename='database.db',
|
228 |
+
cache_dir='./'
|
229 |
+
)
|
230 |
+
except:
|
231 |
+
pass
|
232 |
+
# Update DB
|
233 |
+
db_thread = threading.Thread(target=restart_space)
|
234 |
+
db_thread.daemon = True
|
235 |
+
db_thread.start()
|
236 |
+
create_db()
|
237 |
demo.queue(api_open=False).launch(show_api=False)
|