Spaces:
Sleeping
Sleeping
import os | |
from datasets import load_dataset | |
import json | |
import uuid | |
from pathlib import Path | |
import json | |
from datasets import load_dataset | |
from flask import Flask, request, jsonify | |
from flask_cors import CORS | |
from flask_apscheduler import APScheduler | |
import shutil | |
from PIL import Image | |
import sqlite3 | |
from huggingface_hub import Repository | |
import subprocess | |
app = Flask(__name__, static_url_path='/static') | |
CORS(app) | |
Path("static/images").mkdir(parents=True, exist_ok=True) | |
def update_repository(): | |
print("Updating repository") | |
def index(): | |
return app.send_static_file('index.html') | |
def push(): | |
if(request.headers['token'] == TOKEN): | |
print("Force Push repository") | |
shutil.copyfile(DB_FILE, "./data/prompts.db") | |
subprocess.Popen( | |
"git add . && git commit --amend -m 'update' && git push --force", cwd="./data", shell=True) | |
return "Success", 200 | |
else: | |
return "Error", 401 | |
def getdata(): | |
return app.send_static_file('data.json') | |
def create(): | |
if request.method == 'POST': | |
try: | |
data = request.get_json() | |
guess = data['guess'] | |
correct = data['correct'] | |
with sqlite3.connect(DB_FILE) as db: | |
db.execute( | |
'INSERT INTO prompts (guess, correct) VALUES (?, ?)', (guess, correct)) | |
db.commit() | |
return 'OK', 200 | |
except: | |
return 'Missing guess or correct', 400 | |
if __name__ == '__main__': | |
mode = os.environ.get('FLASK_ENV', 'production') | |
print(mode) | |
dev = mode == 'development' | |
if not dev: | |
print("Starting scheduler -- Running Production") | |
scheduler = APScheduler() | |
scheduler.add_job(id='Update Dataset Repository', | |
func=update_repository, trigger='interval', hours=1) | |
scheduler.start() | |
else: | |
print("Not Starting scheduler -- Running Development") | |
app.run(host='0.0.0.0', port=int( | |
os.environ.get('PORT', 7860)), debug=True, use_reloader=dev) | |