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

Added chat initiation

Browse files
Files changed (1) hide show
  1. app.py +43 -7
app.py CHANGED
@@ -41,13 +41,51 @@ CSS ="""
41
  #chatbot { flex-grow: 1; overflow: auto;}
42
  """
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  with gr.Blocks(css=CSS, fill_height=True) as demo:
45
  streaming_thread = createThread()
46
- chatbot = gr.Chatbot(label="Facility managment bot", elem_id="chatbot") #just to fit the notebook
 
 
47
  with gr.Row():
48
- for i in range(6):
49
- btn = gr.Button(visible=False)
50
- btn_list.append(btn)
 
 
 
51
  msg = gr.Textbox(label="Answer")
52
  with gr.Row():
53
  clear_btn = gr.ClearButton(chatbot)
@@ -124,7 +162,5 @@ with gr.Blocks(css=CSS, fill_height=True) as demo:
124
  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)
125
 
126
 
127
-
128
  if __name__ == "__main__":
129
- demo.launch()
130
- gr.close_all()
 
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 = []
48
+ string_of_suggestions = ""
49
+ is_loading_suggestions = False
50
+ is_it_first_response = True
51
+ response_message = ""
52
+ with client.beta.threads.runs.stream(
53
+ thread_id=threadId,
54
+ assistant_id=assistantId,
55
+ ) as stream:
56
+ for text in stream.text_deltas:
57
+ print(text, end="")
58
+ local_message = None
59
+ if "[" in text and is_it_first_response:
60
+ is_loading_suggestions = True
61
+
62
+ is_it_first_response = False
63
+
64
+ if is_loading_suggestions != True:
65
+ local_message = text
66
+ else:
67
+ string_of_suggestions = string_of_suggestions + text
68
+ if "#s#" in string_of_suggestions:
69
+ is_loading_suggestions = False
70
+ list_of_suggestions, local_message = handle_suggestions(string_of_suggestions)
71
+ if local_message is not None:
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)
 
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()