Spaces:
Runtime error
Runtime error
keep db different dir from repo, route for force push
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from datasets import load_dataset
|
|
| 8 |
from flask import Flask, request, jsonify
|
| 9 |
from flask_cors import CORS
|
| 10 |
from flask_apscheduler import APScheduler
|
| 11 |
-
|
| 12 |
from PIL import Image
|
| 13 |
import sqlite3
|
| 14 |
from huggingface_hub import Repository
|
|
@@ -19,7 +19,7 @@ CORS(app)
|
|
| 19 |
|
| 20 |
TOKEN = os.environ.get('dataset_token')
|
| 21 |
|
| 22 |
-
DB_FILE = Path("./
|
| 23 |
|
| 24 |
repo = Repository(
|
| 25 |
local_dir="data",
|
|
@@ -28,10 +28,13 @@ repo = Repository(
|
|
| 28 |
use_auth_token=TOKEN
|
| 29 |
)
|
| 30 |
repo.git_pull()
|
|
|
|
|
|
|
| 31 |
|
| 32 |
dataset = load_dataset(
|
| 33 |
"huggingface-projects/wordalle_prompts",
|
| 34 |
use_auth_token=TOKEN)
|
|
|
|
| 35 |
Path("static/images").mkdir(parents=True, exist_ok=True)
|
| 36 |
|
| 37 |
db = sqlite3.connect(DB_FILE)
|
|
@@ -60,7 +63,11 @@ with open('static/data.json', 'w') as f:
|
|
| 60 |
|
| 61 |
|
| 62 |
def update_repository():
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
db.row_factory = sqlite3.Row
|
| 65 |
result = db.execute("SELECT * FROM prompts").fetchall()
|
| 66 |
data = [dict(row) for row in result]
|
|
@@ -69,7 +76,6 @@ def update_repository():
|
|
| 69 |
json.dump(data, f, separators=(',', ':'))
|
| 70 |
|
| 71 |
print("Updating repository")
|
| 72 |
-
repo.git_pull()
|
| 73 |
repo.push_to_hub(blocking=False)
|
| 74 |
|
| 75 |
|
|
@@ -78,6 +84,21 @@ def index():
|
|
| 78 |
return app.send_static_file('index.html')
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
@app.route('/data')
|
| 82 |
def getdata():
|
| 83 |
return app.send_static_file('data.json')
|
|
@@ -105,7 +126,7 @@ if __name__ == '__main__':
|
|
| 105 |
if mode != 'development':
|
| 106 |
scheduler = APScheduler()
|
| 107 |
scheduler.add_job(id='Update Dataset Repository',
|
| 108 |
-
|
| 109 |
scheduler.start()
|
| 110 |
app.run(host='0.0.0.0', port=int(
|
| 111 |
os.environ.get('PORT', 7860)), debug=True)
|
|
|
|
| 8 |
from flask import Flask, request, jsonify
|
| 9 |
from flask_cors import CORS
|
| 10 |
from flask_apscheduler import APScheduler
|
| 11 |
+
import shutil
|
| 12 |
from PIL import Image
|
| 13 |
import sqlite3
|
| 14 |
from huggingface_hub import Repository
|
|
|
|
| 19 |
|
| 20 |
TOKEN = os.environ.get('dataset_token')
|
| 21 |
|
| 22 |
+
DB_FILE = Path("./prompts.db")
|
| 23 |
|
| 24 |
repo = Repository(
|
| 25 |
local_dir="data",
|
|
|
|
| 28 |
use_auth_token=TOKEN
|
| 29 |
)
|
| 30 |
repo.git_pull()
|
| 31 |
+
# copy db on db to local path
|
| 32 |
+
shutil.copyfile("./data/prompts.db", DB_FILE)
|
| 33 |
|
| 34 |
dataset = load_dataset(
|
| 35 |
"huggingface-projects/wordalle_prompts",
|
| 36 |
use_auth_token=TOKEN)
|
| 37 |
+
|
| 38 |
Path("static/images").mkdir(parents=True, exist_ok=True)
|
| 39 |
|
| 40 |
db = sqlite3.connect(DB_FILE)
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def update_repository():
|
| 66 |
+
repo.git_pull()
|
| 67 |
+
# copy db on db to local path
|
| 68 |
+
shutil.copyfile(DB_FILE, "./data/prompts.db")
|
| 69 |
+
|
| 70 |
+
with sqlite3.connect("./data/prompts.db") as db:
|
| 71 |
db.row_factory = sqlite3.Row
|
| 72 |
result = db.execute("SELECT * FROM prompts").fetchall()
|
| 73 |
data = [dict(row) for row in result]
|
|
|
|
| 76 |
json.dump(data, f, separators=(',', ':'))
|
| 77 |
|
| 78 |
print("Updating repository")
|
|
|
|
| 79 |
repo.push_to_hub(blocking=False)
|
| 80 |
|
| 81 |
|
|
|
|
| 84 |
return app.send_static_file('index.html')
|
| 85 |
|
| 86 |
|
| 87 |
+
@app.route('/force_push')
|
| 88 |
+
def push():
|
| 89 |
+
if(request.headers['token'] == TOKEN):
|
| 90 |
+
print("Force Push repository")
|
| 91 |
+
shutil.copyfile(DB_FILE, "./data/prompts.db")
|
| 92 |
+
oldpwd = os.getcwd()
|
| 93 |
+
os.chdir("./data")
|
| 94 |
+
os.system("git add .")
|
| 95 |
+
os.system("git commit -m 'force push'")
|
| 96 |
+
os.system("git push --force")
|
| 97 |
+
os.chdir(oldpwd)
|
| 98 |
+
return "Success", 200
|
| 99 |
+
else:
|
| 100 |
+
return "Error", 401
|
| 101 |
+
|
| 102 |
@app.route('/data')
|
| 103 |
def getdata():
|
| 104 |
return app.send_static_file('data.json')
|
|
|
|
| 126 |
if mode != 'development':
|
| 127 |
scheduler = APScheduler()
|
| 128 |
scheduler.add_job(id='Update Dataset Repository',
|
| 129 |
+
func=update_repository, trigger='interval', seconds=300)
|
| 130 |
scheduler.start()
|
| 131 |
app.run(host='0.0.0.0', port=int(
|
| 132 |
os.environ.get('PORT', 7860)), debug=True)
|