Spaces:
Runtime error
Runtime error
Commit
·
62c1c78
1
Parent(s):
abc080f
Allow to store different thread id for different users
Browse files
app.py
CHANGED
@@ -5,8 +5,10 @@ import os
|
|
5 |
client = OpenAI(api_key=os.environ['openAIToken'])
|
6 |
assistantId = os.environ['assistantId']
|
7 |
|
8 |
-
def createThread():
|
9 |
-
|
|
|
|
|
10 |
|
11 |
def addMessage(text, threadId):
|
12 |
return client.beta.threads.messages.create(
|
@@ -41,7 +43,8 @@ CSS ="""
|
|
41 |
#chatbot { flex-grow: 1; overflow: auto;}
|
42 |
"""
|
43 |
|
44 |
-
def initiate_chatting(
|
|
|
45 |
addMessage("Hi", threadId)
|
46 |
global list_of_suggestions
|
47 |
list_of_suggestions = []
|
@@ -72,95 +75,114 @@ def initiate_chatting(threadId):
|
|
72 |
response_message += local_message
|
73 |
stream.until_done()
|
74 |
|
75 |
-
return
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
if __name__ == "__main__":
|
|
|
166 |
demo.launch()
|
|
|
5 |
client = OpenAI(api_key=os.environ['openAIToken'])
|
6 |
assistantId = os.environ['assistantId']
|
7 |
|
8 |
+
def createThread(sessionStorage):
|
9 |
+
streaming_thread = client.beta.threads.create()
|
10 |
+
sessionStorage.append(streaming_thread.id)
|
11 |
+
return sessionStorage
|
12 |
|
13 |
def addMessage(text, threadId):
|
14 |
return client.beta.threads.messages.create(
|
|
|
43 |
#chatbot { flex-grow: 1; overflow: auto;}
|
44 |
"""
|
45 |
|
46 |
+
def initiate_chatting(storage):
|
47 |
+
threadId = storage[0]
|
48 |
addMessage("Hi", threadId)
|
49 |
global list_of_suggestions
|
50 |
list_of_suggestions = []
|
|
|
75 |
response_message += local_message
|
76 |
stream.until_done()
|
77 |
|
78 |
+
return [[None, response_message]]
|
79 |
+
|
80 |
+
def createDemo():
|
81 |
+
with gr.Blocks(css=CSS, fill_height=True) as demo:
|
82 |
+
storage = gr.State([])
|
83 |
+
inital_response_message = ''
|
84 |
+
chatbot = gr.Chatbot(label="Facility managment bot", elem_id="chatbot")
|
85 |
+
with gr.Row():
|
86 |
+
for i in range(6):
|
87 |
+
btn = gr.Button(visible=False)
|
88 |
+
btn_list.append(btn)
|
89 |
+
msg = gr.Textbox(label="Answer", interactive=False)
|
90 |
+
with gr.Row():
|
91 |
+
clear_btn = gr.ClearButton(chatbot)
|
92 |
+
btn = gr.Button("Submit")
|
93 |
+
|
94 |
+
|
95 |
+
def user(user_message, history):
|
96 |
+
return "", history + [[user_message, None]]
|
97 |
+
|
98 |
+
def respond(chat_history, storage):
|
99 |
+
print("Responding")
|
100 |
+
global btn_list
|
101 |
+
message = chat_history[-1][0]
|
102 |
+
threadId = storage[0]
|
103 |
+
print("THREAD_ID:", threadId)
|
104 |
+
chat_history[-1][1] = ""
|
105 |
+
addMessage(message, threadId)
|
106 |
+
global list_of_suggestions
|
107 |
+
list_of_suggestions = []
|
108 |
+
string_of_suggestions = ""
|
109 |
+
is_loading_suggestions = False
|
110 |
+
is_it_first_response = True
|
111 |
+
with client.beta.threads.runs.stream(
|
112 |
+
thread_id=threadId,
|
113 |
+
assistant_id=assistantId,
|
114 |
+
) as stream:
|
115 |
+
for text in stream.text_deltas:
|
116 |
+
print(text, end="")
|
117 |
+
local_message = None
|
118 |
+
if "[" in text and is_it_first_response:
|
119 |
+
is_loading_suggestions = True
|
120 |
+
|
121 |
+
is_it_first_response = False
|
122 |
+
|
123 |
+
if is_loading_suggestions != True:
|
124 |
+
local_message = text
|
125 |
+
else:
|
126 |
+
string_of_suggestions = string_of_suggestions + text
|
127 |
+
if "#s#" in string_of_suggestions:
|
128 |
+
is_loading_suggestions = False
|
129 |
+
list_of_suggestions, local_message = handle_suggestions(string_of_suggestions)
|
130 |
+
if local_message is not None:
|
131 |
+
chat_history[-1][1] += local_message
|
132 |
+
yield {chatbot: chat_history}
|
133 |
+
|
134 |
+
stream.until_done()
|
135 |
+
|
136 |
+
def update_suggestions():
|
137 |
+
global list_of_suggestions
|
138 |
+
btn_list = create_suggestions_list(list_of_suggestions)
|
139 |
+
return btn_list
|
140 |
+
|
141 |
+
def hide_suggestions():
|
142 |
+
return [gr.update(visible=False, value="") for _ in range(6)]
|
143 |
+
|
144 |
+
def disable_msg():
|
145 |
+
message_box = gr.Textbox(value=None, interactive=False)
|
146 |
+
return message_box
|
147 |
+
|
148 |
+
def enable_msg():
|
149 |
+
message_box = gr.Textbox(value=None, interactive=True)
|
150 |
+
return message_box
|
151 |
+
|
152 |
+
add_user_message_flow = [user, [msg,chatbot], [msg,chatbot]]
|
153 |
+
chat_response_flow = [respond, [chatbot, storage], [chatbot]]
|
154 |
+
update_suggestions_flow = [update_suggestions, None, btn_list]
|
155 |
+
hide_suggestions_flow = [hide_suggestions, None, btn_list]
|
156 |
+
disable_msg_flow = [disable_msg, None, msg]
|
157 |
+
enable_msg_flow = [enable_msg, None, msg]
|
158 |
+
|
159 |
+
btn.click(*add_user_message_flow
|
160 |
+
).then(*hide_suggestions_flow
|
161 |
+
).then(*disable_msg_flow
|
162 |
+
).then(*chat_response_flow
|
163 |
+
).then(*update_suggestions_flow
|
164 |
+
).then(*enable_msg_flow)
|
165 |
+
msg.submit(*add_user_message_flow
|
166 |
+
).then(*hide_suggestions_flow
|
167 |
+
).then(*disable_msg_flow
|
168 |
+
).then(*chat_response_flow
|
169 |
+
).then(*update_suggestions_flow
|
170 |
+
).then(*enable_msg_flow)
|
171 |
+
for sug_btn in btn_list:
|
172 |
+
add_suggestion_message_flow = [user, [sug_btn, chatbot], [msg, chatbot]]
|
173 |
+
sug_btn.click(*add_suggestion_message_flow
|
174 |
+
).then(*hide_suggestions_flow
|
175 |
+
).then(*disable_msg_flow
|
176 |
+
).then(*chat_response_flow
|
177 |
+
).then(*update_suggestions_flow
|
178 |
+
).then(*enable_msg_flow)
|
179 |
+
|
180 |
+
demo.load(createThread, inputs=storage, outputs=storage
|
181 |
+
).then(initiate_chatting, inputs=storage, outputs=chatbot
|
182 |
+
).then(*update_suggestions_flow
|
183 |
+
).then(*enable_msg_flow)
|
184 |
+
return demo
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
+
demo = createDemo()
|
188 |
demo.launch()
|