Spaces:
Runtime error
Runtime error
umairahmad89
commited on
Commit
•
d0c74c0
1
Parent(s):
241e234
Add functions for create_thread, and clear all
Browse files
app.py
CHANGED
@@ -25,15 +25,43 @@ class PPTChat:
|
|
25 |
"",
|
26 |
history,
|
27 |
)
|
28 |
-
|
29 |
def create_thread(self):
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
self.assistant.add_file(file)
|
35 |
|
36 |
-
def add_message(
|
|
|
|
|
37 |
for file in message["files"]:
|
38 |
file_type = utils.file_type(file)
|
39 |
if file_type:
|
@@ -41,18 +69,17 @@ class PPTChat:
|
|
41 |
self.add_file(file)
|
42 |
else:
|
43 |
history.append((f"Unsupported file type: {file}", None))
|
44 |
-
|
45 |
if message["text"]:
|
46 |
history.append((message["text"], None))
|
47 |
-
|
48 |
return history, gr.MultimodalTextbox(value=None, interactive=True)
|
49 |
|
50 |
def bot_response(self, history: List[Tuple]) -> List[Tuple]:
|
51 |
-
|
52 |
last_message = history[-1][0]
|
53 |
response = self.assistant.chat(last_message, self.thread_id)
|
54 |
history[-1] = (history[-1][0], response)
|
55 |
-
print(">>>>>>>>>>>>>>>>>>",response)
|
56 |
return history
|
57 |
|
58 |
def create_interface(self):
|
@@ -67,31 +94,25 @@ class PPTChat:
|
|
67 |
interactive=True,
|
68 |
file_count="multiple",
|
69 |
placeholder="Enter message or upload file...",
|
70 |
-
show_label=False
|
71 |
)
|
72 |
-
|
|
|
73 |
chat_msg = chat_input.submit(
|
74 |
-
self.add_message,
|
75 |
-
[chatbot, chat_input],
|
76 |
-
[chatbot, chat_input]
|
77 |
)
|
78 |
bot_msg = chat_msg.then(
|
79 |
-
self.bot_response,
|
80 |
-
chatbot,
|
81 |
-
chatbot,
|
82 |
-
api_name="bot_response"
|
83 |
)
|
84 |
bot_msg.then(
|
85 |
-
lambda: gr.MultimodalTextbox(interactive=True),
|
86 |
-
None,
|
87 |
-
[chat_input]
|
88 |
)
|
89 |
-
|
|
|
90 |
return demo
|
91 |
|
92 |
|
93 |
if __name__ == "__main__":
|
94 |
-
|
95 |
chatbot = PPTChat()
|
96 |
|
97 |
interface = chatbot.create_interface()
|
|
|
25 |
"",
|
26 |
history,
|
27 |
)
|
28 |
+
|
29 |
def create_thread(self):
|
30 |
+
try:
|
31 |
+
self.assistant.delete_thread(self.thread_id)
|
32 |
+
self.thread_id = self.assistant.create_thread().id
|
33 |
+
gr.Info(message="New thread created. Start as New!")
|
34 |
+
except Exception as e:
|
35 |
+
gr.Error(message=f"Unable to create new thread. Error: {e}")
|
36 |
+
|
37 |
+
def clear_all(self):
|
38 |
+
# create new thread
|
39 |
+
# delete previous files
|
40 |
+
# create new chatbot
|
41 |
+
|
42 |
+
self.create_thread()
|
43 |
+
gr.Info(message="Created new thread")
|
44 |
+
try:
|
45 |
+
assistant_file_ids = self.assistant.get_files_list()
|
46 |
+
print(">>>>>>>>>>>>>>>>>>>>>>>>",assistant_file_ids)
|
47 |
+
for file_id in assistant_file_ids:
|
48 |
+
self.assistant.remove_file(file_id=file_id)
|
49 |
+
|
50 |
+
gr.Info(message="Deleted files in assistant")
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
gr.Error(message=f"Unable to delete files. Error: {e}")
|
54 |
+
|
55 |
+
gr.Info("Chat is cleared.")
|
56 |
+
|
57 |
+
return [("Clear Chatbot", "Chatbot cleared.")]
|
58 |
+
|
59 |
+
def add_file(self, file: gr.File):
|
60 |
self.assistant.add_file(file)
|
61 |
|
62 |
+
def add_message(
|
63 |
+
self, history: List[Tuple], message: dict
|
64 |
+
) -> Tuple[List[Tuple], gr.MultimodalTextbox]:
|
65 |
for file in message["files"]:
|
66 |
file_type = utils.file_type(file)
|
67 |
if file_type:
|
|
|
69 |
self.add_file(file)
|
70 |
else:
|
71 |
history.append((f"Unsupported file type: {file}", None))
|
72 |
+
|
73 |
if message["text"]:
|
74 |
history.append((message["text"], None))
|
75 |
+
|
76 |
return history, gr.MultimodalTextbox(value=None, interactive=True)
|
77 |
|
78 |
def bot_response(self, history: List[Tuple]) -> List[Tuple]:
|
|
|
79 |
last_message = history[-1][0]
|
80 |
response = self.assistant.chat(last_message, self.thread_id)
|
81 |
history[-1] = (history[-1][0], response)
|
82 |
+
print(">>>>>>>>>>>>>>>>>>", response)
|
83 |
return history
|
84 |
|
85 |
def create_interface(self):
|
|
|
94 |
interactive=True,
|
95 |
file_count="multiple",
|
96 |
placeholder="Enter message or upload file...",
|
97 |
+
show_label=False,
|
98 |
)
|
99 |
+
new_thread_button = gr.Button(value="Create New Thread")
|
100 |
+
clear_button = gr.Button(value="Clear All")
|
101 |
chat_msg = chat_input.submit(
|
102 |
+
self.add_message, [chatbot, chat_input], [chatbot, chat_input]
|
|
|
|
|
103 |
)
|
104 |
bot_msg = chat_msg.then(
|
105 |
+
self.bot_response, chatbot, chatbot, api_name="bot_response"
|
|
|
|
|
|
|
106 |
)
|
107 |
bot_msg.then(
|
108 |
+
lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input]
|
|
|
|
|
109 |
)
|
110 |
+
new_thread_button.click(self.create_thread)
|
111 |
+
clear_button.click(self.clear_all, outputs=chatbot)
|
112 |
return demo
|
113 |
|
114 |
|
115 |
if __name__ == "__main__":
|
|
|
116 |
chatbot = PPTChat()
|
117 |
|
118 |
interface = chatbot.create_interface()
|