Giuliano commited on
Commit
60617fe
1 Parent(s): 0c35043

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py CHANGED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('pip install gradio==2.3.5b0')
3
+ os.system('pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.9.0+${CUDA}.html')
4
+
5
+
6
+ import gradio as gr
7
+
8
+ from transformers import pipeline
9
+
10
+ import pandas as pd
11
+
12
+
13
+ table = pd.DataFrame()
14
+ tqa = pipeline(task="table-question-answering", model="google/tapas-base-finetuned-wtq")
15
+
16
+
17
+ def chat(message):
18
+ history = gr.get_state() or []
19
+
20
+
21
+ global table
22
+
23
+ if message.startswith('http'):
24
+ table = pd.read_csv(message)
25
+ table = table.astype(str)
26
+ response = 'thank you to give me a dataset... now you can ask questions about it'
27
+
28
+ elif table.empty:
29
+ response = 'Hi! You still have not given me the url of a dataset in csv format. Send a url of a csv file and then ask as many questions as you want about it. If you want to talk about another dataset, just send a new link.'
30
+
31
+ else:
32
+ response = tqa(table=table, query=message)["answer"]
33
+
34
+
35
+
36
+ history.append((message, response))
37
+ gr.set_state(history)
38
+ html = "<div class='chatbot'>"
39
+ for user_msg, resp_msg in history:
40
+ html += f"<div class='user_msg'>{user_msg}</div>"
41
+ html += f"<div class='resp_msg'>{resp_msg}</div>"
42
+ html += "</div>"
43
+ return html
44
+
45
+ iface = gr.Interface(chat, "text", "html", css="""
46
+ .chatbox {display:flex;flex-direction:column}
47
+ .user_msg, .resp_msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
48
+ .user_msg {background-color:cornflowerblue;color:white;align-self:start}
49
+ .resp_msg {background-color:lightgray;align-self:self-end}
50
+ """, allow_screenshot=False, allow_flagging=False)
51
+ if __name__ == "__main__":
52
+ iface.launch(debug=True)