oleksandrburlakov commited on
Commit
62c1c78
·
1 Parent(s): abc080f

Allow to store different thread id for different users

Browse files
Files changed (1) hide show
  1. app.py +114 -92
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
- return client.beta.threads.create()
 
 
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(threadId):
 
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 response_message, list_of_suggestions
76
-
77
- with gr.Blocks(css=CSS, fill_height=True) as demo:
78
- streaming_thread = createThread()
79
- initial_message, initial_suggestions = initiate_chatting(streaming_thread.id)
80
-
81
- chatbot = gr.Chatbot(label="Facility managment bot", elem_id="chatbot", value=[[None, initial_message]])
82
- with gr.Row():
83
- for w in initial_suggestions:
84
- btn = gr.Button(visible=True, value=w)
85
- btn_list.append(btn)
86
- for i in range(6-len(initial_suggestions)):
87
- btn = gr.Button(visible=False)
88
- btn_list.append(btn)
89
- msg = gr.Textbox(label="Answer")
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):
99
- print("Responding")
100
- global btn_list
101
- message = chat_history[-1][0]
102
- threadId = streaming_thread.id
103
- chat_history[-1][1] = ""
104
- addMessage(message, threadId)
105
- global list_of_suggestions
106
- list_of_suggestions = []
107
- string_of_suggestions = ""
108
- is_loading_suggestions = False
109
- is_it_first_response = True
110
- with client.beta.threads.runs.stream(
111
- thread_id=threadId,
112
- assistant_id=assistantId,
113
- ) as stream:
114
- for text in stream.text_deltas:
115
- print(text, end="")
116
- local_message = None
117
- if "[" in text and is_it_first_response:
118
- is_loading_suggestions = True
119
-
120
- is_it_first_response = False
121
-
122
- if is_loading_suggestions != True:
123
- local_message = text
124
- else:
125
- string_of_suggestions = string_of_suggestions + text
126
- if "#s#" in string_of_suggestions:
127
- is_loading_suggestions = False
128
- list_of_suggestions, local_message = handle_suggestions(string_of_suggestions)
129
- if local_message is not None:
130
- chat_history[-1][1] += local_message
131
- yield {chatbot: chat_history}
132
-
133
- stream.until_done()
134
-
135
- def update_suggestions():
136
- global list_of_suggestions
137
- btn_list = create_suggestions_list(list_of_suggestions)
138
- return btn_list
139
-
140
- def hide_suggestions():
141
- return [gr.update(visible=False, value="") for _ in range(6)]
142
-
143
- def disable_msg():
144
- message_box = gr.Textbox(value=None, interactive=False)
145
- return message_box
146
-
147
- def enable_msg():
148
- message_box = gr.Textbox(value=None, interactive=True)
149
- return message_box
150
-
151
- add_user_message_flow = [user, [msg,chatbot], [msg,chatbot]]
152
- chat_response_flow = [respond, [chatbot], [chatbot]]
153
- update_suggestions_flow = [update_suggestions, None, btn_list]
154
- hide_suggestions_flow = [hide_suggestions, None, btn_list]
155
- disable_msg_flow = [disable_msg, None, msg]
156
- enable_msg_flow = [enable_msg, None, msg]
157
-
158
- btn.click(*add_user_message_flow).then(*hide_suggestions_flow).then(*disable_msg_flow).then(*chat_response_flow).then(*update_suggestions_flow).then(*enable_msg_flow)
159
- msg.submit(*add_user_message_flow).then(*hide_suggestions_flow).then(*disable_msg_flow).then(*chat_response_flow).then(*update_suggestions_flow).then(*enable_msg_flow)
160
- for sug_btn in btn_list:
161
- add_suggestion_message_flow = [user, [sug_btn, chatbot], [msg, chatbot]]
162
- sug_btn.click(*add_suggestion_message_flow).then(*hide_suggestions_flow).then(*disable_msg_flow).then(*chat_response_flow).then(*update_suggestions_flow).then(*enable_msg_flow)
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()