Kilos1 commited on
Commit
878fe17
·
verified ·
1 Parent(s): ae647fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def add_review(name: str, review: int, comments: str):
50
+ db = sqlite3.connect(DB_FILE)
51
+ cursor = db.cursor()
52
+ cursor.execute("INSERT INTO reviews(name, review, comments) VALUES(?,?,?)", [name, review, comments])
53
+ db.commit()
54
+ reviews, total_reviews = get_latest_reviews(db)
55
+ db.close()
56
+ return reviews, total_reviews
57
+
58
+ def load_data():
59
+ db = sqlite3.connect(DB_FILE)
60
+ reviews, total_reviews = get_latest_reviews(db)
61
+ db.close()
62
+ return reviews, total_reviews
63
+
64
+
65
+ with gr.Blocks() as demo:
66
+ with gr.Row():
67
+ with gr.Column():
68
+ name = gr.Textbox(label="Name", placeholder="What is your name?")
69
+ review = gr.Radio(label="How satisfied are you with using gradio?", choices=[1, 2, 3, 4, 5])
70
+ comments = gr.Textbox(label="Comments", lines=10, placeholder="Do you have any feedback on gradio?")
71
+ submit = gr.Button(value="Submit Feedback")
72
+ with gr.Column():
73
+ with gr.Box():
74
+ gr.Markdown("Most recently created 10 rows: See full dataset [here](https://huggingface.co/datasets/freddyaboulton/gradio-reviews)")
75
+ data = gr.Dataframe()
76
+ count = gr.Number(label="Total number of reviews")
77
+ submit.click(add_review, [name, review, comments], [data, count])
78
+ demo.load(load_data, None, [data, count])
79
+
80
+
81
+ def backup_db():
82
+ shutil.copyfile(DB_FILE, "./data/reviews.db")
83
+ db = sqlite3.connect(DB_FILE)
84
+ reviews = db.execute("SELECT * FROM reviews").fetchall()
85
+ pd.DataFrame(reviews).to_csv("./data/reviews.csv", index=False)
86
+ print("updating db")
87
+ repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
88
+
89
+
90
+ scheduler = BackgroundScheduler()
91
+ scheduler.add_job(func=backup_db, trigger="interval", seconds=60)
92
+ scheduler.start()
93
+
94
+
95
+ demo.launch()