naman1102 commited on
Commit
d4ec8a0
·
1 Parent(s): aee35a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -3
app.py CHANGED
@@ -248,7 +248,7 @@ def create_ui() -> gr.Blocks:
248
  return history
249
 
250
  def handle_end_chat(history: List[Dict[str, str]]) -> Tuple[str, str]:
251
- """Ends the chat and extracts keywords from the conversation."""
252
  if not history:
253
  return "", "Status: Chat is empty, nothing to analyze."
254
 
@@ -257,9 +257,24 @@ def create_ui() -> gr.Blocks:
257
  if not tuple_history:
258
  return "", "Status: No completed conversations to analyze."
259
 
260
- keywords_str = extract_keywords_from_conversation(tuple_history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  status = "Status: Keywords extracted. You can now use them to search."
262
- return keywords_str, status
263
 
264
  # --- Component Event Wiring ---
265
 
 
248
  return history
249
 
250
  def handle_end_chat(history: List[Dict[str, str]]) -> Tuple[str, str]:
251
+ """Ends the chat, extracts and sanitizes keywords from the conversation."""
252
  if not history:
253
  return "", "Status: Chat is empty, nothing to analyze."
254
 
 
257
  if not tuple_history:
258
  return "", "Status: No completed conversations to analyze."
259
 
260
+ # Get raw keywords string from the LLM
261
+ raw_keywords_str = extract_keywords_from_conversation(tuple_history)
262
+
263
+ # Sanitize the LLM output to extract only keyword-like parts.
264
+ # A keyword can contain letters, numbers, underscores, spaces, and hyphens.
265
+ cleaned_keywords = re.findall(r'[\w\s-]+', raw_keywords_str)
266
+
267
+ # Trim whitespace from each found keyword and filter out any empty strings
268
+ cleaned_keywords = [kw.strip() for kw in cleaned_keywords if kw.strip()]
269
+
270
+ if not cleaned_keywords:
271
+ return "", f"Status: Could not extract valid keywords. Raw LLM output: '{raw_keywords_str}'"
272
+
273
+ # Join them into a clean, comma-separated string for the search tool
274
+ final_keywords_str = ", ".join(cleaned_keywords)
275
+
276
  status = "Status: Keywords extracted. You can now use them to search."
277
+ return final_keywords_str, status
278
 
279
  # --- Component Event Wiring ---
280