Spaces:
Build error
Build error
Commit
·
fda45a4
1
Parent(s):
212e48a
Update app.py
Browse files
app.py
CHANGED
@@ -38,9 +38,9 @@ class BasicTutor:
|
|
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 = "
|
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
|
@@ -49,30 +49,23 @@ class BasicTutor:
|
|
49 |
self.conversation_memory = []
|
50 |
self.flattened_conversation = ''
|
51 |
|
52 |
-
|
|
|
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 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
def bot(history):
|
68 |
-
user_message = history[-1][0]
|
69 |
-
bot_message = "You typed: " + user_message
|
70 |
-
history[-1][1] = bot_message
|
71 |
-
return history
|
72 |
|
73 |
with gr.Blocks() as demo:
|
74 |
#initialize tutor (with state)
|
75 |
-
|
76 |
|
77 |
# Chatbot interface
|
78 |
gr.Markdown("""
|
@@ -89,9 +82,10 @@ with gr.Blocks() as demo:
|
|
89 |
with gr.Column():
|
90 |
clear = gr.Button("Clear")
|
91 |
|
92 |
-
user_chat_submit.click(
|
93 |
-
|
94 |
-
|
|
|
95 |
|
96 |
with gr.Blocks():
|
97 |
gr.Markdown("""
|
@@ -101,14 +95,12 @@ with gr.Blocks() as demo:
|
|
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', '.
|
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()
|
|
|
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, user_message):
|
42 |
# get tutor message
|
43 |
+
tutor_message = "You said: " + user_message
|
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
|
|
|
49 |
self.conversation_memory = []
|
50 |
self.flattened_conversation = ''
|
51 |
|
52 |
+
# history is a list of list
|
53 |
+
# [[user_input_str, bot_response_str], ...]
|
54 |
|
55 |
+
### Chatbot Functions ###
|
56 |
def add_user_message(user_message, chat_tutor):
|
57 |
"""Display user message and update chat history to include it."""
|
58 |
chat_tutor.add_user_message(user_message)
|
59 |
+
return gr.update(value="", interactive=False), chat_tutor.conversation_memory, chat_tutor
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
def get_tutor_reply(user_message, chat_tutor):
|
62 |
+
chat_tutor.get_tutor_reply(user_message)
|
63 |
+
return gr.update(value="", interactive=True), chat_tutor.conversation_memory, chat_tutor
|
64 |
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
with gr.Blocks() as demo:
|
67 |
#initialize tutor (with state)
|
68 |
+
study_tutor = gr.State(BasicTutor())
|
69 |
|
70 |
# Chatbot interface
|
71 |
gr.Markdown("""
|
|
|
82 |
with gr.Column():
|
83 |
clear = gr.Button("Clear")
|
84 |
|
85 |
+
user_chat_submit.click(add_user_message,
|
86 |
+
[user_chat_input, study_tutor],
|
87 |
+
[user_chat_input, chatbot, study_tutor], queue=False) \
|
88 |
+
.then(get_tutor_reply, [user_chat_input, study_tutor], [user_chat_input, chatbot, study_tutor], queue=True)
|
89 |
|
90 |
with gr.Blocks():
|
91 |
gr.Markdown("""
|
|
|
95 |
with gr.Row():
|
96 |
export_dialogue_button_json = gr.Button("JSON")
|
97 |
export_dialogue_button_txt = gr.Button("TXT")
|
|
|
98 |
|
99 |
file_download = gr.Files(label="Download here",
|
100 |
+
file_types=['.txt', '.json'], type="file", visible=False)
|
101 |
|
102 |
export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
|
103 |
export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
|
|
|
104 |
|
105 |
demo.queue()
|
106 |
demo.launch()
|