Chia Woon Yap commited on
Commit
6c76f8f
·
verified ·
1 Parent(s): 1544faa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -4
app.py CHANGED
@@ -120,9 +120,32 @@ def retrieve_documents(query):
120
  results = vectorstore.similarity_search(query, k=3)
121
  return [doc.page_content for doc in results]
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  # Function to handle chatbot interactions with short-term memory
124
  def chat_with_groq(user_input, chat_history):
125
  try:
 
 
 
126
  # Retrieve relevant documents for additional context
127
  relevant_docs = retrieve_documents(user_input)
128
  context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found."
@@ -142,15 +165,18 @@ def chat_with_groq(user_input, chat_history):
142
  chat_memory.append(f"User: {user_input}")
143
  chat_memory.append(f"AI: {cleaned_response_text}")
144
 
145
- # Update chat history for Gradio
146
- chat_history.append((user_input, cleaned_response_text))
 
147
 
148
  # Convert response to speech
149
  audio_file = speech_playback(cleaned_response_text)
150
 
151
  return chat_history, "", audio_file
152
  except Exception as e:
153
- chat_history.append((user_input, f"Error: {str(e)}"))
 
 
154
  return chat_history, "", None
155
 
156
  # Function to play response as speech using gTTS
@@ -283,7 +309,11 @@ def tutor_ai_chatbot():
283
  )
284
 
285
  # Clear chat history function
286
- clear_btn.click(clear_chat_history, inputs=None, outputs=[chatbot, audio_playback])
 
 
 
 
287
 
288
  # Also allow Enter key to submit
289
  msg.submit(
 
120
  results = vectorstore.similarity_search(query, k=3)
121
  return [doc.page_content for doc in results]
122
 
123
+ # Function to convert tuple format to message format
124
+ def convert_to_message_format(chat_history):
125
+ """Convert from [(user, bot)] format to [{"role": "user", "content": user}, {"role": "assistant", "content": bot}] format"""
126
+ message_format = []
127
+ for user_msg, bot_msg in chat_history:
128
+ message_format.append({"role": "user", "content": user_msg})
129
+ message_format.append({"role": "assistant", "content": bot_msg})
130
+ return message_format
131
+
132
+ # Function to convert message format to tuple format for processing
133
+ def convert_to_tuple_format(chat_history):
134
+ """Convert from message format back to tuple format for processing"""
135
+ tuple_format = []
136
+ for i in range(0, len(chat_history), 2):
137
+ if i+1 < len(chat_history):
138
+ user_msg = chat_history[i]["content"]
139
+ bot_msg = chat_history[i+1]["content"]
140
+ tuple_format.append((user_msg, bot_msg))
141
+ return tuple_format
142
+
143
  # Function to handle chatbot interactions with short-term memory
144
  def chat_with_groq(user_input, chat_history):
145
  try:
146
+ # Convert message format to tuple format for processing
147
+ tuple_history = convert_to_tuple_format(chat_history)
148
+
149
  # Retrieve relevant documents for additional context
150
  relevant_docs = retrieve_documents(user_input)
151
  context = "\n".join(relevant_docs) if relevant_docs else "No relevant documents found."
 
165
  chat_memory.append(f"User: {user_input}")
166
  chat_memory.append(f"AI: {cleaned_response_text}")
167
 
168
+ # Update chat history - add new messages in the correct format
169
+ chat_history.append({"role": "user", "content": user_input})
170
+ chat_history.append({"role": "assistant", "content": cleaned_response_text})
171
 
172
  # Convert response to speech
173
  audio_file = speech_playback(cleaned_response_text)
174
 
175
  return chat_history, "", audio_file
176
  except Exception as e:
177
+ error_msg = f"Error: {str(e)}"
178
+ chat_history.append({"role": "user", "content": user_input})
179
+ chat_history.append({"role": "assistant", "content": error_msg})
180
  return chat_history, "", None
181
 
182
  # Function to play response as speech using gTTS
 
309
  )
310
 
311
  # Clear chat history function
312
+ clear_btn.click(
313
+ lambda: [], # Return empty list in message format
314
+ inputs=None,
315
+ outputs=[chatbot]
316
+ )
317
 
318
  # Also allow Enter key to submit
319
  msg.submit(