Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import huggingface_hub
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
import datetime
|
| 8 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
DB_FILE = "./reviews.db"
|
| 12 |
+
|
| 13 |
+
TOKEN = os.environ.get('HUB_TOKEN')
|
| 14 |
+
repo = huggingface_hub.Repository(
|
| 15 |
+
local_dir="data",
|
| 16 |
+
repo_type="dataset",
|
| 17 |
+
clone_from="freddyaboulton/gradio-reviews",
|
| 18 |
+
use_auth_token=TOKEN
|
| 19 |
+
)
|
| 20 |
+
repo.git_pull()
|
| 21 |
+
|
| 22 |
+
# Set db to latest
|
| 23 |
+
shutil.copyfile("./data/reviews.db", DB_FILE)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Create table if it doesn't already exist
|
| 27 |
+
|
| 28 |
+
db = sqlite3.connect(DB_FILE)
|
| 29 |
+
try:
|
| 30 |
+
db.execute("SELECT * FROM reviews").fetchall()
|
| 31 |
+
db.close()
|
| 32 |
+
except sqlite3.OperationalError:
|
| 33 |
+
db.execute(
|
| 34 |
+
'''
|
| 35 |
+
CREATE TABLE reviews (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
| 36 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
| 37 |
+
name TEXT, review INTEGER, comments TEXT)
|
| 38 |
+
''')
|
| 39 |
+
db.commit()
|
| 40 |
+
db.close()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_latest_reviews(db: sqlite3.Connection):
|
| 44 |
+
reviews = db.execute("SELECT * FROM reviews ORDER BY id DESC limit 10").fetchall()
|
| 45 |
+
total_reviews = db.execute("Select COUNT(id) from reviews").fetchone()[0]
|
| 46 |
+
reviews = pd.DataFrame(reviews, columns=["id", "date_created", "name", "review", "comments"])
|
| 47 |
+
return reviews, total_reviews
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def add_review(name: str, review: int, comments: str):
|
| 51 |
+
db = sqlite3.connect(DB_FILE)
|
| 52 |
+
cursor = db.cursor()
|
| 53 |
+
cursor.execute("INSERT INTO reviews(name, review, comments) VALUES(?,?,?)", [name, review, comments])
|
| 54 |
+
db.commit()
|
| 55 |
+
reviews, total_reviews = get_latest_reviews(db)
|
| 56 |
+
db.close()
|
| 57 |
+
return reviews, total_reviews
|
| 58 |
+
|
| 59 |
+
def load_data():
|
| 60 |
+
db = sqlite3.connect(DB_FILE)
|
| 61 |
+
reviews, total_reviews = get_latest_reviews(db)
|
| 62 |
+
db.close()
|
| 63 |
+
return reviews, total_reviews
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
name = gr.Textbox(label="Name", placeholder="What is your name?")
|
| 70 |
+
review = gr.Radio(label="How satisfied are you with using gradio?", choices=[1, 2, 3, 4, 5])
|
| 71 |
+
comments = gr.Textbox(label="Comments", lines=10, placeholder="Do you have any feedback on gradio?")
|
| 72 |
+
submit = gr.Button(value="Submit Feedback")
|
| 73 |
+
with gr.Column():
|
| 74 |
+
with gr.Box():
|
| 75 |
+
gr.Markdown("Most recently created 10 rows: See full dataset [here](https://huggingface.co/datasets/freddyaboulton/gradio-reviews)")
|
| 76 |
+
data = gr.Dataframe()
|
| 77 |
+
count = gr.Number(label="Total number of reviews")
|
| 78 |
+
submit.click(add_review, [name, review, comments], [data, count])
|
| 79 |
+
demo.load(load_data, None, [data, count])
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def backup_db():
|
| 83 |
+
shutil.copyfile(DB_FILE, "./data/reviews.db")
|
| 84 |
+
db = sqlite3.connect(DB_FILE)
|
| 85 |
+
reviews = db.execute("SELECT * FROM reviews").fetchall()
|
| 86 |
+
pd.DataFrame(reviews).to_csv("./data/reviews.csv", index=False)
|
| 87 |
+
print("updating db")
|
| 88 |
+
repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
scheduler = BackgroundScheduler()
|
| 92 |
+
scheduler.add_job(func=backup_db, trigger="interval", seconds=60)
|
| 93 |
+
scheduler.start()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
demo.launch()
|