Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -343,17 +343,58 @@ def send_ally_ai_email(to_email, subject, body, user_email,
|
|
| 343 |
# GRADIO UI
|
| 344 |
# ============================
|
| 345 |
def run_search(story, country):
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
options = []
|
| 350 |
-
for i, r in enumerate(
|
| 351 |
-
label_contact = r.get("email") if r.get("email") and r.get("email")!="Not found" else (r.get("phone","No contact"))
|
| 352 |
-
|
|
|
|
| 353 |
options.append(label)
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
def make_body(anon_text, full_story, use_anon, user_email):
|
| 359 |
core = (anon_text or "").strip() if use_anon else (full_story or "").strip()
|
|
|
|
| 343 |
# GRADIO UI
|
| 344 |
# ============================
|
| 345 |
def run_search(story, country):
|
| 346 |
+
"""Robust search wrapper: returns (summary, table_records, dropdown_update, anonymized_text)"""
|
| 347 |
+
try:
|
| 348 |
+
out = find_professionals_from_story(story, country=country, results_per_query=RESULTS_PER_QUERY)
|
| 349 |
+
except Exception as e:
|
| 350 |
+
# If the backend search crashed, return a friendly error and placeholder dropdown
|
| 351 |
+
err_msg = f"Search failed: {e}"
|
| 352 |
+
placeholder = "0 — No results (search failed)"
|
| 353 |
+
return err_msg, [], gr.update(choices=[placeholder], value=placeholder), ""
|
| 354 |
+
|
| 355 |
+
# Build dataframe records (list of dicts)
|
| 356 |
+
pros = out.get("professionals", []) or []
|
| 357 |
+
try:
|
| 358 |
+
df_records = pd.DataFrame(pros).to_dict(orient="records")
|
| 359 |
+
except Exception:
|
| 360 |
+
# fallback to empty list
|
| 361 |
+
df_records = []
|
| 362 |
+
|
| 363 |
+
# Build dropdown options safely (guarantee at least one option)
|
| 364 |
options = []
|
| 365 |
+
for i, r in enumerate(pros):
|
| 366 |
+
label_contact = r.get("email") if r.get("email") and r.get("email") != "Not found" else (r.get("phone", "No contact"))
|
| 367 |
+
title = r.get("title") or r.get("url") or "(no title)"
|
| 368 |
+
label = f"{i} — {title} ({label_contact})"
|
| 369 |
options.append(label)
|
| 370 |
+
|
| 371 |
+
if not options:
|
| 372 |
+
options = ["0 — No results (try a different country/query)"]
|
| 373 |
+
|
| 374 |
+
# Anonymize story in a safe way (if anonymizer fails, fallback to short template)
|
| 375 |
+
try:
|
| 376 |
+
anon = anonymize_story(story) or "I am seeking confidential support regarding gender-based violence."
|
| 377 |
+
except Exception as e:
|
| 378 |
+
print("[anonymize error]", e)
|
| 379 |
+
anon = "I am seeking confidential support regarding gender-based violence."
|
| 380 |
+
|
| 381 |
+
# Return summary, table records, dropdown update and anonymized text
|
| 382 |
+
summary = out.get("summary", "No results found.")
|
| 383 |
+
return summary, df_records, gr.update(choices=options, value=options[0]), anon
|
| 384 |
+
|
| 385 |
+
# -------------------------
|
| 386 |
+
# If you used a _on_search wrapper, replace it similarly (example below)
|
| 387 |
+
# This is the function wired to the search button in your Gradio UI.
|
| 388 |
+
# -------------------------
|
| 389 |
+
def _on_search(story, country):
|
| 390 |
+
"""
|
| 391 |
+
This wrapper simply calls run_search and also returns a safe prefilled message body.
|
| 392 |
+
It is the function connected to search_btn.click(...)
|
| 393 |
+
"""
|
| 394 |
+
summary, records, dropdown_update, anon = run_search(story, country)
|
| 395 |
+
# Prefill message body with anonymized summary (user email blank for now)
|
| 396 |
+
prefill = make_body(anon, story, True, "")
|
| 397 |
+
return summary, records, dropdown_update, anon, prefill
|
| 398 |
|
| 399 |
def make_body(anon_text, full_story, use_anon, user_email):
|
| 400 |
core = (anon_text or "").strip() if use_anon else (full_story or "").strip()
|