Spaces:
Runtime error
Runtime error
Commit
·
022601f
1
Parent(s):
f74d71b
First commit
Browse files- __pycache__/response_db.cpython-37.pyc +0 -0
- app.py +80 -0
- images/img_1.jpg +0 -0
- images/img_10.jpg +0 -0
- images/img_11.jpg +0 -0
- images/img_12.jpg +0 -0
- images/img_13.jpg +0 -0
- images/img_14.jpg +0 -0
- images/img_15.jpg +0 -0
- images/img_16.jpg +0 -0
- images/img_17.jpg +0 -0
- images/img_18.jpg +0 -0
- images/img_19.jpg +0 -0
- images/img_2.jpg +0 -0
- images/img_20.jpg +0 -0
- images/img_21.jpg +0 -0
- images/img_22.jpg +0 -0
- images/img_23.jpg +0 -0
- images/img_24.jpg +0 -0
- images/img_25.jpg +0 -0
- images/img_26.jpg +0 -0
- images/img_27.jpg +0 -0
- images/img_28.jpg +0 -0
- images/img_29.jpg +0 -0
- images/img_3.jpg +0 -0
- images/img_30.jpg +0 -0
- images/img_4.jpg +0 -0
- images/img_5.jpg +0 -0
- images/img_6.jpg +0 -0
- images/img_7.jpg +0 -0
- images/img_8.jpg +0 -0
- images/img_9.jpg +0 -0
- justin_demo.py +24 -0
- open_db.py +12 -0
- response.db +0 -0
- response_db.py +79 -0
__pycache__/response_db.cpython-37.pyc
ADDED
Binary file (2.86 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from response_db import StResponseDb
|
3 |
+
db = StResponseDb()
|
4 |
+
a = gr.Number(0)
|
5 |
+
|
6 |
+
def get_next_question(history):
|
7 |
+
if len(history)==2:
|
8 |
+
question = "What is the man doing?"
|
9 |
+
elif len(history)==4:
|
10 |
+
question = "How many apples are there?"
|
11 |
+
else:
|
12 |
+
question = "What color is the cat?"
|
13 |
+
return question
|
14 |
+
|
15 |
+
def ask_a_question(input, taskid, history=[]):
|
16 |
+
history.append(input)
|
17 |
+
db.add(int(a.value), taskid, len(history)//2-1, history[-2], history[-1])
|
18 |
+
history.append(get_next_question(history))
|
19 |
+
|
20 |
+
# write some HTML
|
21 |
+
html = "<div class='chatbot'>"
|
22 |
+
for m, msg in enumerate(history):
|
23 |
+
cls = "bot" if m%2 == 0 else "user"
|
24 |
+
html += "<div class='msg {}'> {}</div>".format(cls, msg)
|
25 |
+
html += "</div>"
|
26 |
+
return html, history
|
27 |
+
|
28 |
+
|
29 |
+
css = """
|
30 |
+
.chatbox {display:flex;flex-direction:column}
|
31 |
+
.msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
|
32 |
+
.msg.user {background-color:cornflowerblue;color:white}
|
33 |
+
.msg.bot {background-color:lightgray;align-self:self-end}
|
34 |
+
.footer {display:none !important}
|
35 |
+
"""
|
36 |
+
|
37 |
+
def set_images(taskid):
|
38 |
+
id1 = f"images/img_{int(10*(taskid-1)+1)}.jpg"
|
39 |
+
id2 = f"images/img_{int(10*(taskid-1)+2)}.jpg"
|
40 |
+
id3 = f"images/img_{int(10*(taskid-1)+3)}.jpg"
|
41 |
+
id4 = f"images/img_{int(10*(taskid-1)+4)}.jpg"
|
42 |
+
id5 = f"images/img_{int(10*(taskid-1)+5)}.jpg"
|
43 |
+
id6 = f"images/img_{int(10*(taskid-1)+6)}.jpg"
|
44 |
+
id7 = f"images/img_{int(10*(taskid-1)+7)}.jpg"
|
45 |
+
id8 = f"images/img_{int(10*(taskid-1)+8)}.jpg"
|
46 |
+
id9 = f"images/img_{int(10*(taskid-1)+9)}.jpg"
|
47 |
+
id10 = f"images/img_{int(10*(taskid-1)+10)}.jpg"
|
48 |
+
first_question = "How many dogs are there?"
|
49 |
+
first_question_html = f"<div class='chatbot'><div class='msg bot'>{first_question}</div></div>"
|
50 |
+
a.value = a.value+1
|
51 |
+
return id1, id2, id3, id4, id5, id6, id7, id8, id9, id10, [first_question], first_question_html
|
52 |
+
|
53 |
+
with gr.Blocks(css=css) as demo:
|
54 |
+
|
55 |
+
with gr.Column() as img_block:
|
56 |
+
with gr.Row():
|
57 |
+
img1 = gr.Image()
|
58 |
+
img2 = gr.Image()
|
59 |
+
img3 = gr.Image()
|
60 |
+
img4 = gr.Image()
|
61 |
+
img5 = gr.Image()
|
62 |
+
with gr.Row():
|
63 |
+
img6 = gr.Image()
|
64 |
+
img7 = gr.Image()
|
65 |
+
img8 = gr.Image()
|
66 |
+
img9 = gr.Image()
|
67 |
+
img10 = gr.Image()
|
68 |
+
conversation = gr.HTML()
|
69 |
+
history_log = gr.State([])
|
70 |
+
|
71 |
+
with gr.Column():
|
72 |
+
with gr.Row():
|
73 |
+
taskid = gr.Number(label="Task ID (Choose from [1,2,3])")
|
74 |
+
btn1 = gr.Button("Enter")
|
75 |
+
btn1.click(set_images, inputs=taskid, outputs=[img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, history_log, conversation])
|
76 |
+
answer = gr.inputs.Textbox(placeholder="Insert answer here.", label="Answer the given question.")
|
77 |
+
submit = gr.Button("Submit")
|
78 |
+
submit.click(fn=ask_a_question, inputs=[answer, taskid, history_log], outputs=[conversation, history_log])
|
79 |
+
|
80 |
+
demo.launch(share=True)
|
images/img_1.jpg
ADDED
![]() |
images/img_10.jpg
ADDED
![]() |
images/img_11.jpg
ADDED
![]() |
images/img_12.jpg
ADDED
![]() |
images/img_13.jpg
ADDED
![]() |
images/img_14.jpg
ADDED
![]() |
images/img_15.jpg
ADDED
![]() |
images/img_16.jpg
ADDED
![]() |
images/img_17.jpg
ADDED
![]() |
images/img_18.jpg
ADDED
![]() |
images/img_19.jpg
ADDED
![]() |
images/img_2.jpg
ADDED
![]() |
images/img_20.jpg
ADDED
![]() |
images/img_21.jpg
ADDED
![]() |
images/img_22.jpg
ADDED
![]() |
images/img_23.jpg
ADDED
![]() |
images/img_24.jpg
ADDED
![]() |
images/img_25.jpg
ADDED
![]() |
images/img_26.jpg
ADDED
![]() |
images/img_27.jpg
ADDED
![]() |
images/img_28.jpg
ADDED
![]() |
images/img_29.jpg
ADDED
![]() |
images/img_3.jpg
ADDED
![]() |
images/img_30.jpg
ADDED
![]() |
images/img_4.jpg
ADDED
![]() |
images/img_5.jpg
ADDED
![]() |
images/img_6.jpg
ADDED
![]() |
images/img_7.jpg
ADDED
![]() |
images/img_8.jpg
ADDED
![]() |
images/img_9.jpg
ADDED
![]() |
justin_demo.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from response_db import StResponseDb
|
3 |
+
|
4 |
+
ID = "1234"
|
5 |
+
turn = int(0)
|
6 |
+
db = StResponseDb()
|
7 |
+
question = "What color is the cat?"
|
8 |
+
|
9 |
+
img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Orange_tabby_cat_sitting_on_fallen_leaves-Hisashi-01A.jpg/1920px-Orange_tabby_cat_sitting_on_fallen_leaves-Hisashi-01A.jpg',
|
10 |
+
|
11 |
+
def add_response(response):
|
12 |
+
db.add(ID, turn, question, response)
|
13 |
+
return str(db.get_all())
|
14 |
+
|
15 |
+
with gr.Blocks() as demo:
|
16 |
+
#gr.Image(value=img_url, shape=(400,None))
|
17 |
+
gr.Textbox(label="Question", value=question)
|
18 |
+
#image = gr.Image(value=img_url)
|
19 |
+
response = gr.Textbox(label="Response")
|
20 |
+
submit = gr.Button("Submit")
|
21 |
+
db_contents = gr.Textbox(label="output")
|
22 |
+
submit.click(fn=add_response, inputs=response, outputs=db_contents)
|
23 |
+
|
24 |
+
demo.launch(share=True)
|
open_db.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
db = sqlite3.connect("response.db")
|
5 |
+
df = pd.read_sql('SELECT * from responses', db)
|
6 |
+
print(df)
|
7 |
+
# def importdb(db):
|
8 |
+
# conn = sqlite3.connect(db)
|
9 |
+
# c = conn.cursor()
|
10 |
+
# c.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
11 |
+
# for table in c.fetchall()
|
12 |
+
# yield list(c.execute('SELECT * from ?;', (table[0],)))
|
response.db
ADDED
Binary file (8.19 kB). View file
|
|
response_db.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlite3
|
2 |
+
from sqlite3 import Connection
|
3 |
+
import datetime
|
4 |
+
|
5 |
+
class ResponseDb:
|
6 |
+
DB_PATH = "response.db"
|
7 |
+
|
8 |
+
def __init__(self):
|
9 |
+
# establish connection
|
10 |
+
self.con = self.get_connection()
|
11 |
+
cur = self.con.cursor()
|
12 |
+
# create table if it doesnt already exist
|
13 |
+
table_string = "CREATE TABLE IF NOT EXISTS responses (dialogue_id text, task_id text, turn integer, question text, response text, datetime date)"
|
14 |
+
cur.execute(table_string)
|
15 |
+
self.con.commit()
|
16 |
+
cur.close()
|
17 |
+
|
18 |
+
def get_connection(self):
|
19 |
+
"""Put the connection in cache to reuse if path does not change between Streamlit reruns.
|
20 |
+
NB : https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa
|
21 |
+
"""
|
22 |
+
return sqlite3.connect(self.DB_PATH, check_same_thread=False)
|
23 |
+
|
24 |
+
#def __del__(self):
|
25 |
+
#self.con.close()
|
26 |
+
|
27 |
+
def add(self, dialogue_id, task_id, turn, question, response):
|
28 |
+
cur = self.con.cursor()
|
29 |
+
curr_datetime = datetime.datetime.now()
|
30 |
+
cur.execute(
|
31 |
+
"insert into responses values (?, ?, ?, ?, ?, ?)",
|
32 |
+
(dialogue_id, int(task_id), turn, question, response, curr_datetime),
|
33 |
+
)
|
34 |
+
self.con.commit()
|
35 |
+
cur.close()
|
36 |
+
|
37 |
+
def get_id(self, dialogue_id):
|
38 |
+
cur = self.con.cursor()
|
39 |
+
cur.execute(
|
40 |
+
"select * from responses where conv=:id",
|
41 |
+
{"id": dialogue_id},
|
42 |
+
)
|
43 |
+
results = cur.fetchall()
|
44 |
+
cur.close()
|
45 |
+
return results
|
46 |
+
|
47 |
+
def get_id_turn(self, dialogue_id, turn):
|
48 |
+
cur = self.con.cursor()
|
49 |
+
cur.execute(
|
50 |
+
"select * from responses where conv=:id and turn=:turn",
|
51 |
+
{
|
52 |
+
"id": dialogue_id,
|
53 |
+
"turn": turn,
|
54 |
+
},
|
55 |
+
)
|
56 |
+
results = cur.fetchall()
|
57 |
+
cur.close()
|
58 |
+
return results
|
59 |
+
|
60 |
+
def get_all(self):
|
61 |
+
cur = self.con.cursor()
|
62 |
+
cur.execute(
|
63 |
+
"select * from responses",
|
64 |
+
)
|
65 |
+
results = cur.fetchall()
|
66 |
+
cur.close()
|
67 |
+
return results
|
68 |
+
|
69 |
+
class StResponseDb(ResponseDb):
|
70 |
+
def get_connection(self):
|
71 |
+
"""Put the connection in cache to reuse if path does not change between Streamlit reruns.
|
72 |
+
NB : https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa
|
73 |
+
"""
|
74 |
+
return sqlite3.connect(self.DB_PATH, check_same_thread=False)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
db = ResponseDb()
|
78 |
+
print(db.get_all())
|
79 |
+
|