Spaces:
Build error
Build error
Commit
·
3b538c3
1
Parent(s):
b32ea14
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from functools import partial
|
4 |
+
|
5 |
+
def save_chatbot_dialogue(chat_tutor, save_type):
|
6 |
+
|
7 |
+
formatted_convo = pd.DataFrame(chat_tutor.conversation_memory, columns=['user', 'chatbot'])
|
8 |
+
|
9 |
+
output_fname = f'tutoring_conversation.{save_type}'
|
10 |
+
|
11 |
+
if save_type == 'csv':
|
12 |
+
formatted_convo.to_csv(output_fname, index=False)
|
13 |
+
elif save_type == 'json':
|
14 |
+
formatted_convo.to_json(output_fname, orient='records')
|
15 |
+
elif save_type == 'txt':
|
16 |
+
temp = formatted_convo.apply(lambda x: 'User: {0}\nAI: {1}'.format(x[0], x[1]), axis=1)
|
17 |
+
temp = '\n\n'.join(temp.tolist())
|
18 |
+
with open(output_fname, 'w') as f:
|
19 |
+
f.write(temp)
|
20 |
+
else:
|
21 |
+
gr.update(value=None, visible=False)
|
22 |
+
|
23 |
+
return gr.update(value=output_fname, visible=True)
|
24 |
+
|
25 |
+
save_json = partial(save_chatbot_dialogue, save_type='json')
|
26 |
+
save_txt = partial(save_chatbot_dialogue, save_type='txt')
|
27 |
+
save_csv = partial(save_chatbot_dialogue, save_type='csv')
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
class BasicTutor:
|
32 |
+
# create basic initialization function
|
33 |
+
def __init__(self):
|
34 |
+
self.conversation_memory = []
|
35 |
+
self.flattened_conversation = ''
|
36 |
+
|
37 |
+
def add_user_message(self, user_message):
|
38 |
+
self.conversation_memory.append([user_message, None])
|
39 |
+
self.flattened_conversation = self.flattened_conversation + '\n\n' + 'User: ' + user_message
|
40 |
+
|
41 |
+
def get_tutor_reply(self):
|
42 |
+
# get tutor message
|
43 |
+
tutor_message = "Yes"
|
44 |
+
# add tutor message to conversation memory
|
45 |
+
self.conversation_memory[-1][1] = tutor_message
|
46 |
+
self.flattened_conversation = self.flattened_conversation + '\nAI: ' + tutor_message
|
47 |
+
|
48 |
+
def forget_conversation(self):
|
49 |
+
self.conversation_memory = []
|
50 |
+
self.flattened_conversation = ''
|
51 |
+
|
52 |
+
### Chatbot Functions ###
|
53 |
+
|
54 |
+
def add_user_message(user_message, chat_tutor):
|
55 |
+
"""Display user message and update chat history to include it."""
|
56 |
+
chat_tutor.add_user_message(user_message)
|
57 |
+
return chat_tutor.conversation_memory, chat_tutor
|
58 |
+
|
59 |
+
def get_tutor_reply(chat_tutor):
|
60 |
+
chat_tutor.get_tutor_reply()
|
61 |
+
return chat_tutor.conversation_memory, chat_tutor
|
62 |
+
|
63 |
+
# history is a list of list [[user_input_str, bot_response_str], ...]
|
64 |
+
def user(message, history):
|
65 |
+
return "", history + [[message, None]]
|
66 |
+
|
67 |
+
def bot(history):
|
68 |
+
user_message = history[-1][0]
|
69 |
+
tutor_message = "You typed: " + user_message
|
70 |
+
|
71 |
+
|
72 |
+
with gr.Blocks() as demo:
|
73 |
+
#initialize tutor (with state)
|
74 |
+
study_tutor = gr.State(BasicTutor())
|
75 |
+
|
76 |
+
# Chatbot interface
|
77 |
+
gr.Markdown("""
|
78 |
+
## Chat with the Model
|
79 |
+
Description here
|
80 |
+
""")
|
81 |
+
|
82 |
+
with gr.Row(equal_height=True):
|
83 |
+
with gr.Column(scale=2):
|
84 |
+
chatbot = gr.Chatbot()
|
85 |
+
with gr.Row():
|
86 |
+
user_chat_input = gr.Textbox(label="User input", scale=9)
|
87 |
+
user_chat_submit = gr.Button("Ask/answer model", scale=1)
|
88 |
+
|
89 |
+
|
90 |
+
async_response = user_chat_submit.click(add_user_message,
|
91 |
+
[user_chat_input, study_tutor],
|
92 |
+
[user_chat_input, chatbot, study_tutor], queue=False) \
|
93 |
+
.then(get_tutor_reply, [study_tutor], [user_chat_input, chatbot, study_tutor], queue=True)
|
94 |
+
|
95 |
+
|
96 |
+
with gr.Blocks():
|
97 |
+
gr.Markdown("""
|
98 |
+
## Export Your Chat History
|
99 |
+
Export your chat history as a .json, .txt, or .csv file
|
100 |
+
""")
|
101 |
+
with gr.Row():
|
102 |
+
export_dialogue_button_json = gr.Button("JSON")
|
103 |
+
export_dialogue_button_txt = gr.Button("TXT")
|
104 |
+
export_dialogue_button_csv = gr.Button("CSV")
|
105 |
+
|
106 |
+
file_download = gr.Files(label="Download here",
|
107 |
+
file_types=['.txt', '.csv', '.json'], type="file", visible=False)
|
108 |
+
|
109 |
+
export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
|
110 |
+
export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
|
111 |
+
export_dialogue_button_csv.click(save_csv, study_tutor, file_download, show_progress=True)
|
112 |
+
|
113 |
+
demo.queue()
|
114 |
+
demo.launch()
|