CognitiveScience commited on
Commit
4fe8a03
1 Parent(s): 8c3e918

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +219 -0
app.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from bs4 import BeautifulSoup
3
+ import requests
4
+ from acogsphere import acf
5
+ from bcogsphere import bcf
6
+ import math
7
+ import glob
8
+ #from python_actr import *
9
+ #from cogscidighum import *
10
+
11
+
12
+ #class myCelSci(Model):
13
+ # pass
14
+
15
+ #def main(link):
16
+ # response=getviews(link)+getresult("hello world")[0]["label"] + str(math.trunc(getresult("hello world")[0]["score"])*100/100)
17
+ # return response #result #soup.prettify()
18
+
19
+
20
+
21
+ import sqlite3
22
+ import huggingface_hub
23
+ import pandas as pd
24
+ import shutil
25
+ import os
26
+ import datetime
27
+ from apscheduler.schedulers.background import BackgroundScheduler
28
+
29
+ import random
30
+ import time
31
+
32
+ DB_FILE = "./reviews.db"
33
+
34
+ TOKEN = os.environ.get('HF_KEY')
35
+
36
+ repo = huggingface_hub.Repository(
37
+ local_dir="data",
38
+ repo_type="dataset",
39
+ clone_from="CognitiveScience/csdhdata",
40
+ use_auth_token=TOKEN
41
+ )
42
+ repo.git_pull()
43
+
44
+ # Set db to latest
45
+ shutil.copyfile("./data/reviews.db", DB_FILE)
46
+
47
+ # Create table if it doesn't already exist
48
+
49
+ db = sqlite3.connect(DB_FILE)
50
+ try:
51
+ db.execute("SELECT * FROM reviews").fetchall()
52
+ db.close()
53
+ except sqlite3.OperationalError:
54
+ db.execute(
55
+ '''
56
+ CREATE TABLE reviews (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
57
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
58
+ name TEXT, review INTEGER, comments TEXT)
59
+ ''')
60
+ db.commit()
61
+ db.close()
62
+
63
+
64
+ def get_latest_reviews(db: sqlite3.Connection):
65
+ reviews = db.execute("SELECT * FROM reviews ORDER BY id DESC limit 10").fetchall()
66
+ total_reviews = db.execute("Select COUNT(id) from reviews").fetchone()[0]
67
+ reviews = pd.DataFrame(reviews, columns=["id", "date_created", "name", "review", "comments"])
68
+ return reviews, total_reviews
69
+
70
+
71
+ def add_review(name: str, review: int, comments: str):
72
+ db = sqlite3.connect(DB_FILE)
73
+ cursor = db.cursor()
74
+ cursor.execute("INSERT INTO reviews(name, review, comments) VALUES(?,?,?)", [name, review, comments])
75
+ db.commit()
76
+ reviews, total_reviews = get_latest_reviews(db)
77
+ db.close()
78
+ return reviews, total_reviews
79
+
80
+ def load_data():
81
+ db = sqlite3.connect(DB_FILE)
82
+ reviews, total_reviews = get_latest_reviews(db)
83
+ db.close()
84
+ return reviews, total_reviews
85
+
86
+ def delete_review(id: int):
87
+ db = sqlite3.connect(DB_FILE)
88
+ cursor = db.cursor()
89
+ cursor.execute("DELETE FROM reviews WHERE id = ?", [id])
90
+ db.commit()
91
+ reviews, total_reviews = get_latest_reviews(db)
92
+ db.close()
93
+ return reviews, total_reviews
94
+
95
+ def delete_all_reviews():
96
+ db = sqlite3.connect(DB_FILE)
97
+ cursor = db.cursor()
98
+ cursor.execute("DELETE FROM reviews")
99
+ db.commit()
100
+ reviews, total_reviews = get_latest_reviews(db)
101
+ db.close()
102
+ return reviews, total_reviews
103
+ #def cs(link):
104
+ # response="Hi " + "bcf" #(link) #acf("hello world")[0]["label"] + str(math.trunc(acf("hello world")[0]["score"])*100/100)+bcf(link)
105
+ # return response #result #soup.prettify()
106
+
107
+ def respond3(message, chat_history):
108
+ bot_message = random.choice(["How are you3?", "I love you3", "I'm very hungry3"])
109
+ chat_history.append((message, bot_message))
110
+ time.sleep(2)
111
+
112
+ return "", chat_history
113
+
114
+ with gr.Blocks() as demo:
115
+ with gr.Row():
116
+ with gr.Column():
117
+ with gr.Box():
118
+ gr.Markdown("Based on dataset [here](https://huggingface.co/datasets/freddyaboulton/gradio-reviews)")
119
+ #data = gr.Dataframe()
120
+ count = gr.Number(label="Total number of reviews")
121
+ name = gr.Textbox(label="Name", placeholder="ur name?")
122
+ review = gr.Radio(label="How satisfied are you with your pick?", choices=[1, 2, 3, 4, 5, 6])
123
+ comments = gr.Textbox(label="Comments0", lines=10, placeholder="comm?")
124
+ cssubmit = gr.Button(value="Submit Choice")
125
+ #cschatbot = gr.Chatbot()
126
+ #csinp = gr.Textbox()
127
+ #csout=cs(csinp)
128
+ #csclear = gr.ClearButton([csinp, cschatbot])
129
+
130
+ #csinp.submit(cs, [csinp, cschatbot], [csinp, cschatbot])
131
+
132
+ def cs(link):
133
+ response="Hi " + link #(link) #acf("hello world")[0]["label"] + str(math.trunc(acf("hello world")[0]["score"])*100/100)+bcf(link)
134
+ return response,1 #result #soup.prettify()
135
+ cssubmit.click(cs, name, [comments,count])
136
+
137
+ with gr.Row():
138
+ with gr.Column():
139
+ name = gr.Textbox(label="Name", placeholder="What is your name?")
140
+ review = gr.Radio(label="How satisfied are you with using gradio?", choices=[1, 2, 3, 4, 5])
141
+ comments = gr.Textbox(label="Comments", lines=10, placeholder="Do you have any feedback on gradio?")
142
+ submit = gr.Button(value="Submit Feedback")
143
+ with gr.Column():
144
+ gr.FileExplorer(label="Working directory")
145
+ gr.FileExplorer(root="./data", label="Persistent storage")
146
+
147
+ with gr.Column():
148
+ chatbot = gr.Chatbot()
149
+ msg = gr.Textbox()
150
+ clear = gr.ClearButton([msg, chatbot])
151
+
152
+ def respond(message, chat_history):
153
+ bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
154
+ chat_history.append((message, bot_message))
155
+ time.sleep(2)
156
+
157
+ return "", chat_history
158
+
159
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
160
+
161
+ with gr.Column():
162
+ submitsave = gr.Button(value="Save")
163
+
164
+ def backup_db2():
165
+ shutil.copyfile(DB_FILE, "./data/reviews.db")
166
+ db = sqlite3.connect(DB_FILE)
167
+ reviews = db.execute("SELECT * FROM reviews").fetchall()
168
+ pd.DataFrame(reviews).to_csv("./data/reviews.csv", index=False)
169
+ print("updating db")
170
+ repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
171
+ submit.click(backup_db2)
172
+ with gr.Column():
173
+ with gr.Box():
174
+ gr.Code(
175
+ value="""def hello_world():
176
+ return "Hello, world!"
177
+
178
+ print(hello_world())""",
179
+ language="python",
180
+ interactive=True,
181
+ show_label=False,
182
+ )
183
+ gr.Markdown("Based on dataset [here](https://huggingface.co/datasets/freddyaboulton/gradio-reviews)")
184
+ data = gr.Dataframe()
185
+ count = gr.Number(label="Total number of reviews")
186
+
187
+ submit.click(add_review, [name, review, comments], [data, count])
188
+
189
+ #cssubmit.click(add_review, [name, review, comments], [data, count])
190
+
191
+ record2del = gr.Textbox(label="Id: ", lines=1, placeholder="to delete?")
192
+
193
+ submit2 = gr.Button(value="Delete Review")
194
+ id_input = gr.Number(value=202, visible=False)
195
+ submit2.click(delete_review, id_input)
196
+
197
+
198
+
199
+ submit3 = gr.Button(value="Delete All Reviews")
200
+ submit3.click(delete_all_reviews)
201
+
202
+
203
+ demo.load(load_data, None, [data, count])
204
+
205
+ def backup_db():
206
+ shutil.copyfile(DB_FILE, "./data/reviews.db")
207
+ db = sqlite3.connect(DB_FILE)
208
+ reviews = db.execute("SELECT * FROM reviews").fetchall()
209
+ pd.DataFrame(reviews).to_csv("./data/reviews.csv", index=False)
210
+ print("updating db")
211
+ repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
212
+
213
+
214
+ scheduler = BackgroundScheduler()
215
+ scheduler.add_job(func=backup_db, trigger="interval", seconds=60)
216
+ scheduler.start()
217
+
218
+
219
+ demo.launch()