freddyaboulton HF staff commited on
Commit
74a0b08
1 Parent(s): 2aaaa50

Fix requirements

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