peterkros commited on
Commit
b27abf3
·
verified ·
1 Parent(s): bedf39d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -42,7 +42,12 @@ def initialize_chat(initial_question):
42
  chat_history = [(None, initial_question)]
43
  response, follow_up_questions, token_info = generate_response(initial_question, 0)
44
  chat_history.append((None, response))
45
- return chat_history, follow_up_questions, token_info
 
 
 
 
 
46
 
47
  def generate_response(prompt, token_count=0):
48
  print(f"Received prompt: {prompt}")
@@ -160,15 +165,18 @@ css = """
160
 
161
  # Initialize the chat history and suggested questionsw
162
  # Initialize the chat history and suggested questions
163
- chat_history, follow_up_questions, initial_token_info = initialize_chat("I'm 14 years old female and want to become a graphic designer. I'm living in Uttar Pradesh in India. How can I start?")
164
 
165
 
166
  # Initial example questions from the initialization
167
- formatted_questions = [q.strip() for q in follow_up_questions.split('\n')[:3]]
168
- initial_examples = [[q] for q in formatted_questions]
169
 
170
  with gr.Blocks(css=css) as demo:
171
- examples_state = gr.State(initial_examples)
 
 
 
 
172
  with gr.Row():
173
  with gr.Column(scale=1):
174
  gr.Markdown(
@@ -177,22 +185,24 @@ with gr.Blocks(css=css) as demo:
177
  In the realm of digital communication, the development of an advanced chatbot that incorporates topic modeling represents a significant leap towards enhancing user interaction and maintaining focus during conversations. This innovative chatbot design is specifically engineered to streamline discussions by guiding users to select from a curated list of suggested questions. This approach is crafted to mitigate the risk of diverging into off-topic dialogues, which are common pitfalls in conventional chatbot systems.
178
  """
179
  )
180
- token_info = gr.Markdown(
181
- value=initial_token_info,
182
  elem_id="token-info"
183
  )
184
- follow_up_questions_md = gr.Markdown(
185
- value=follow_up_questions,
186
  elem_id="follow-up-questions",
187
- label="Follow up Quiestions"
188
  )
189
  initial_question_input = gr.Textbox(
190
  placeholder="Type your initial question here...",
191
  label="Initial Question"
192
  )
 
 
193
  with gr.Column(scale=1, elem_id="chat-container"):
194
  chatbot = gr.Chatbot(
195
- value=chat_history,
196
  elem_id="chatbot",
197
  bubble_full_width=False,
198
  label="Safe Chatbot v1"
@@ -204,22 +214,23 @@ with gr.Blocks(css=css) as demo:
204
 
205
  btn.click(
206
  fn=process_response,
207
- inputs=[txt, gr.State(chat_history), gr.State(initial_examples), gr.State(examples_state.value)],
208
- outputs=[chatbot, token_info, follow_up_questions_md, examples_state]
209
-
210
  )
211
  examples_component = gr.Examples(
212
- examples=examples_state.value,
213
- inputs=[txt],
214
- label="Questions"
215
- )
216
- initial_question_input.submit(
217
- fn=initialize_chat,
218
- inputs=[initial_question_input],
219
- outputs=[chatbot, follow_up_questions_md, token_info]
220
- )
221
 
222
  chatbot.like(print_like_dislike, None, None)
223
 
 
 
 
 
 
 
 
224
  if __name__ == "__main__":
225
  demo.launch(share=False)
 
42
  chat_history = [(None, initial_question)]
43
  response, follow_up_questions, token_info = generate_response(initial_question, 0)
44
  chat_history.append((None, response))
45
+
46
+ # Extract follow-up questions as examples
47
+ follow_up_questions_formatted = [q.strip() for q in follow_up_questions.split('\n') if q.strip()]
48
+ examples_state = [[q] for q in follow_up_questions_formatted]
49
+
50
+ return chat_history, follow_up_questions, token_info, examples_state
51
 
52
  def generate_response(prompt, token_count=0):
53
  print(f"Received prompt: {prompt}")
 
165
 
166
  # Initialize the chat history and suggested questionsw
167
  # Initialize the chat history and suggested questions
168
+ #chat_history, follow_up_questions, initial_token_info = initialize_chat("I'm 14 years old female and want to become a graphic designer. I'm living in Uttar Pradesh in India. How can I start?")
169
 
170
 
171
  # Initial example questions from the initialization
172
+
 
173
 
174
  with gr.Blocks(css=css) as demo:
175
+ examples_state = gr.State([])
176
+ chat_history = gr.State([])
177
+ token_info = gr.State("")
178
+ follow_up_questions_md = gr.State("")
179
+
180
  with gr.Row():
181
  with gr.Column(scale=1):
182
  gr.Markdown(
 
185
  In the realm of digital communication, the development of an advanced chatbot that incorporates topic modeling represents a significant leap towards enhancing user interaction and maintaining focus during conversations. This innovative chatbot design is specifically engineered to streamline discussions by guiding users to select from a curated list of suggested questions. This approach is crafted to mitigate the risk of diverging into off-topic dialogues, which are common pitfalls in conventional chatbot systems.
186
  """
187
  )
188
+ token_info_display = gr.Markdown(
189
+ value="",
190
  elem_id="token-info"
191
  )
192
+ follow_up_questions_display = gr.Markdown(
193
+ value="",
194
  elem_id="follow-up-questions",
195
+ label="Follow up Questions"
196
  )
197
  initial_question_input = gr.Textbox(
198
  placeholder="Type your initial question here...",
199
  label="Initial Question"
200
  )
201
+ initialize_button = gr.Button("Initialize Chat")
202
+
203
  with gr.Column(scale=1, elem_id="chat-container"):
204
  chatbot = gr.Chatbot(
205
+ value=[],
206
  elem_id="chatbot",
207
  bubble_full_width=False,
208
  label="Safe Chatbot v1"
 
214
 
215
  btn.click(
216
  fn=process_response,
217
+ inputs=[txt, chat_history, gr.State(0), examples_state],
218
+ outputs=[chatbot, token_info_display, follow_up_questions_display, examples_state]
 
219
  )
220
  examples_component = gr.Examples(
221
+ examples=examples_state.value,
222
+ inputs=[txt],
223
+ label="Questions"
224
+ )
 
 
 
 
 
225
 
226
  chatbot.like(print_like_dislike, None, None)
227
 
228
+ initialize_button.click(
229
+ fn=initialize_chat,
230
+ inputs=[initial_question_input],
231
+ outputs=[chat_history, follow_up_questions_display, token_info_display, examples_state]
232
+ )
233
+ chatbot.set(chat_history)
234
+
235
  if __name__ == "__main__":
236
  demo.launch(share=False)