Gamortsey commited on
Commit
ff81c49
·
verified ·
1 Parent(s): c4b8b95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -9,6 +9,7 @@ import urllib.parse
9
  from bs4 import BeautifulSoup
10
  import json
11
  from pathlib import Path
 
12
 
13
 
14
  import torch
@@ -580,25 +581,34 @@ def send_ally_ai_email(to_email, subject, body, user_email,
580
 
581
  def run_search(story, country):
582
  """
583
- Robust search wrapper: returns (summary, table_records, dropdown_options, anonymized_text).
584
- Avoids returning gr.update(...) to prevent KeyError during serialization.
585
  """
 
 
 
 
586
  try:
587
  out = find_professionals_from_story(story, country=country, results_per_query=RESULTS_PER_QUERY)
 
 
 
588
  except Exception as e:
589
- err_msg = f"Search failed: {e}"
590
- placeholder = ["0 — No results (search failed)"]
591
- return err_msg, [], placeholder, ""
 
592
 
593
  pros = out.get("professionals", []) or []
594
 
595
- # build table records
596
  try:
597
  records = pd.DataFrame(pros).to_dict(orient="records") if pros else []
598
- except Exception:
 
599
  records = []
600
 
601
- # build dropdown options as list of strings (guarantee at least one)
602
  options = []
603
  for i, r in enumerate(pros):
604
  label_contact = r.get("email") if r.get("email") and r.get("email") != "Not found" else (r.get("phone", "No contact"))
@@ -607,37 +617,33 @@ def run_search(story, country):
607
  options.append(label)
608
 
609
  if not options:
610
- options = ["0 — No results (try a different country/query)"]
611
 
612
- # anonymize safely
613
  try:
614
- anon = anonymize_story(story) or "I am seeking confidential support regarding gender-based violence."
615
  except Exception as e:
616
- print("[anonymize error]", e)
617
- anon = "I am seeking confidential support regarding gender-based violence."
618
 
619
- summary = out.get("summary", "No results found.")
620
  return summary, records, options, anon
621
 
622
 
623
  def _on_search(story, country):
624
  """
625
- Function wired to the search button.
626
- Returns exactly 5 outputs to match:
627
  [summary_out, results_table, dropdown_sel, anon_out, message_in]
628
  """
629
  summary, records, options, anon = run_search(story, country)
630
 
631
- # pre-fill message body with anonymized text (user email left empty for now)
632
  prefill = make_body(anon, story, True, "")
633
 
634
  # Return plain serializable values (not gr.update)
635
- # summary -> str
636
- # records -> list[dict] (or [])
637
- # options -> list[str] for dropdown (Gradio will accept it)
638
- # anon -> str
639
- # prefill -> str (message body)
640
  return summary, records, options, anon, prefill
 
641
 
642
 
643
  def make_body(anon_text, full_story, use_anon, user_email):
 
9
  from bs4 import BeautifulSoup
10
  import json
11
  from pathlib import Path
12
+ import traceback
13
 
14
 
15
  import torch
 
581
 
582
  def run_search(story, country):
583
  """
584
+ Robust wrapper around find_professionals_from_story.
585
+ Always returns: (summary:str, records:list[dict], options:list[str], anonymized_text:str)
586
  """
587
+ # default safe return
588
+ default_summary = "No results found (an error occurred or no matches)."
589
+ default_records, default_options, default_anon = [], ["0 — No results (try again)"], "I am seeking confidential support."
590
+
591
  try:
592
  out = find_professionals_from_story(story, country=country, results_per_query=RESULTS_PER_QUERY)
593
+ # If the function returned None or invalid type, replace with defaults
594
+ if not isinstance(out, dict):
595
+ raise ValueError("find_professionals_from_story returned non-dict or None")
596
  except Exception as e:
597
+ # Log the full traceback so you can inspect logs in the Space
598
+ print("[run_search] find_professionals_from_story error:", e)
599
+ traceback.print_exc()
600
+ out = {"summary": f"Search failed: {str(e)}", "professionals": [], "queries_used": []}
601
 
602
  pros = out.get("professionals", []) or []
603
 
604
+ # Build table records safely
605
  try:
606
  records = pd.DataFrame(pros).to_dict(orient="records") if pros else []
607
+ except Exception as e:
608
+ print("[run_search] DataFrame conversion error:", e)
609
  records = []
610
 
611
+ # Build dropdown options as plain list
612
  options = []
613
  for i, r in enumerate(pros):
614
  label_contact = r.get("email") if r.get("email") and r.get("email") != "Not found" else (r.get("phone", "No contact"))
 
617
  options.append(label)
618
 
619
  if not options:
620
+ options = default_options
621
 
622
+ # Anonymize safely (fallback if anonymizer fails)
623
  try:
624
+ anon = anonymize_story(story) or default_anon
625
  except Exception as e:
626
+ print("[run_search] anonymize_story error:", e)
627
+ anon = default_anon
628
 
629
+ summary = out.get("summary", default_summary)
630
  return summary, records, options, anon
631
 
632
 
633
  def _on_search(story, country):
634
  """
635
+ Function wired to search_btn.click(...)
636
+ Must return exactly these outputs:
637
  [summary_out, results_table, dropdown_sel, anon_out, message_in]
638
  """
639
  summary, records, options, anon = run_search(story, country)
640
 
641
+ # Prefill message body (user email left blank for now)
642
  prefill = make_body(anon, story, True, "")
643
 
644
  # Return plain serializable values (not gr.update)
 
 
 
 
 
645
  return summary, records, options, anon, prefill
646
+ # ---------- end replacements ----------
647
 
648
 
649
  def make_body(anon_text, full_story, use_anon, user_email):