Spaces:
Sleeping
Sleeping
Update app.py
Browse files123421 unmodified lines262728293031325 unmodified lines383940414243444546474875 unmodified lines12412512612712812913013113213313413513613713813914014114229 unmodified lines17217317417517617717851 unmodified lines2302312322332342352362372382392402412422432442452462472482492502512522532542552562572589 unmodified lines2682692702712722732742752762772782792802812822832842852862872882892902912922932 unmodified lines29629729829930030130230330430530630730830931031124 unmodified lines3363373383393403413423 unmodified lines3463473483493503513523533543553563573583 unmodified lines3623633643653663673683693703713723733743753763773783799 unmodified lines3893903913923933943953963973983994004014024034044054064074084094104114124134144154164174183 unmodified lines4224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514526 unmodified lines4594604614624634644654664674684699 unmodified lines4794804814824834844854864874884894904914924934944954964974984992 unmodified lines5025035045055065075086 unmodified lines51551651751851952013 unmodified lines53453553653753853954054111 unmodified lines553554555556557558559560561562563564565import osimport timeimport re21 unmodified lines# ============================# CONFIG (ENV VARS recommended)# ============================# IMPORTANT: set these as Space "Secrets" (see README below)API_KEY = os.environ.get("GOOGLE_API_KEY", "YOUR_GOOGLE_API_KEY")CX = os.environ.get("GOOGLE_CSE_ID", "YOUR_CSE_ID")DEFAULT_COUNTRY = "Ghana"5 unmodified linesALLY_AI_LOGO_URL_DEFAULT = os.environ.get("ALLY_AI_LOGO_URL","https://i.ibb.co/7nZqz0H/ai-logo.png")# Optional country maps for search bias & phone parsingCOUNTRY_TLD_MAP = {"Ghana":"gh","Nigeria":"ng","Kenya":"ke","South Africa":"za","USA":"us","United Kingdom":"uk"}COUNTRY_REGION_MAP= {"Ghana":"GH","Nigeria":"NG","Kenya":"KE","South Africa":"ZA","USA":"US","United Kingdom":"GB"}# HTTP + RegexHEADERS = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64)"}EMAIL_REGEX = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")75 unmodified linespassreturn list(set(phones))def scrape_contacts(url, region="GH"):try:res = requests.get(url, headers=HEADERS, timeout=12)if not res.ok or not res.text:return {"emails": [], "phones": []}text = BeautifulSoup(res.text, "html.parser").get_text(separator=" ")text = " ".join(text.split())[:300000]emails = list(set(EMAIL_REGEX.findall(text)))phones = extract_phones(text, region)return {"emails": emails, "phones": phones}except Exception as e:print(f"[scrape error] {url} -> {e}")return {"emails": [], "phones": []}# ============================# NER + STORY → PROFESSIONS29 unmodified linesif p == "gbv":cores += ["GBV support organizations", "gender based violence help"]else:cores += [f"{p} for GBV", f"{p} for sexual assault"]unique_cores, seen = [], set()for c in cores:if c not in seen:51 unmodified linesreturn {"summary":"No results found. Try a different country or wording.","professionals":[], "queries_used":queries}# NER on titles/snippetsall_people, all_orgs, all_locs = [], [], []for r in search_results:ctx = f"{r.get('title','')}. {r.get('snippet','')}"p,o,l = extract_entities(ctx)all_people += p; all_orgs += o; all_locs += l# Scrape contacts concurrentlyprofessionals = []with ThreadPoolExecutor(max_workers=MAX_SCRAPE_WORKERS) as ex:futures = {ex.submit(scrape_contacts, r["link"], region): r for r in search_results}for fut in as_completed(futures):r = futures[fut]contacts = {"emails": [], "phones": []}try:contacts = fut.result()except Exception as e:print("[scrape future error]", r["link"], e)professionals.append({"title": r.get("title",""),"url": r.get("link",""),"email": contacts["emails"][0] if contacts["emails"] else "Not found","phone": contacts["phones"][0] if contacts["phones"] else "Not found","source_query": r.get("query","")})9 unmodified lines# DRAFT (mailto + .eml)# ============================def build_mailto_and_eml(to_addr, subject, body, default_from="noreply@ally.ai"):from email.message import EmailMessageimport timemsg = EmailMessage()msg["From"] = default_frommsg["To"] = to_addrmsg["Subject"] = subjectmsg.set_content(body)# ✅ Save to a writable directory (current working dir or "tmp")os.makedirs("tmp", exist_ok=True)fname = os.path.join("tmp", f"email_draft_{int(time.time())}.eml")with open(fname, "wb") as f:f.write(msg.as_bytes())# Create mailto link (this part is fine)mailto = f"mailto:{to_addr}?subject={subject}&body={body}"return mailto, fname# ============================# SENDER (SMTP) — Ally AI branding2 unmodified linessender_email, sender_password,ai_name=ALLY_AI_NAME,logo_url=ALLY_AI_LOGO_URL_DEFAULT):"""Sends an HTML email branded as Ally AI.to_email: recipient (organization)subject: subject linebody: main message (already anonymized or full text)user_email: survivor's email (included for reply inside body)sender_email/sender_password: SMTP credentials (use Gmail App Password with Gmail)"""if not to_email or to_email == "Not found":return "❌ No recipient email found — choose a contact with an email."msg = MIMEMultipart("alternative")msg["Subject"] = subject or "Request for support"24 unmodified linestry:server = smtplib.SMTP("smtp.gmail.com", 587)server.starttls()server.login(sender_email, sender_password) # Gmail App Password recommendedserver.sendmail(sender_email, [to_email], msg.as_string())server.quit()return f"✅ Email sent successfully to {to_email}"3 unmodified lines# ============================# GRADIO UI# ============================# ------- Replace existing run_search and _on_search with these -------def run_search(story, country):"""Robust search wrapper: returns (summary, table_records, dropdown_options, anonymized_text).Avoids returning gr.update(...) to prevent KeyError during serialization."""try:out = find_professionals_from_story(story, country=country, results_per_query=RESULTS_PER_QUERY)except Exception as e:3 unmodified linespros = out.get("professionals", []) or []# build table recordstry:records = pd.DataFrame(pros).to_dict(orient="records") if pros else []except Exception:records = []# build dropdown options as list of strings (guarantee at least one)options = []for i, r in enumerate(pros):label_contact = r.get("email") if r.get("email") and r.get("email") != "Not found" else (r.get("phone", "No contact"))title = r.get("title") or r.get("url") or "(no title)"label = f"{i} — {title} ({label_contact})"options.append(label)if not options:9 unmodified linessummary = out.get("summary", "No results found.")return summary, records, options, anondef _on_search(story, country):"""Function wired to the search button.Returns exactly 5 outputs to match:[summary_out, results_table, dropdown_sel, anon_out, message_in]"""summary, records, options, anon = run_search(story, country)# pre-fill message body with anonymized text (user email left empty for now)prefill = make_body(anon, story, True, "")# Return plain serializable values (not gr.update)# summary -> str# records -> list[dict] (or [])# options -> list[str] for dropdown (Gradio will accept it)# anon -> str# prefill -> str (message body)return summary, records, options, anon, prefilldef make_body(anon_text, full_story, use_anon, user_email):core = (anon_text or "").strip() if use_anon else (full_story or "").strip()# polite template with user email included in bodylines = [core,"",3 unmodified lines]return "\n".join([l for l in lines if l is not None])def preview_contact(dropdown_value, df_json, subject, message_text):if not dropdown_value:return "No contact selected.", ""try:idx = int(str(dropdown_value).split(" — ")[0])rows = pd.DataFrame(df_json)contact = rows.iloc[idx].to_dict()recipient = contact.get("email") if contact.get("email") and contact.get("email")!="Not found" else "[no email]"html = f"""<h3>Preview</h3><b>To:</b> {recipient}<br/><b>Organization:</b> <a href="{contact.get('url')}" target="_blank" rel="noopener">{contact.get('title')}</a><br/><b>Subject:</b> {subject}<br/><hr/><pre style="white-space:pre-wrap;">{message_text}</pre>"""text = f"To: {recipient}\nSubject: {subject}\n\n{message_text[:600]}{'...' if len(message_text)>600 else ''}"return text, htmlexcept Exception as e:return f"Preview error: {e}", ""def confirm_action(mode, dropdown_value, df_json, subject, message_text,user_email, sender_email, sender_password, logo_url):"""mode: "Draft only" or "Send via SMTP (Gmail)""""if not dropdown_value:return "❌ No contact selected.", "", None6 unmodified linesexcept Exception as e:return f"❌ Selection error: {e}", "", Nonerecipient = contact.get("email")if mode.startswith("Send"):# Validate required fieldsif not recipient or recipient == "Not found":return "❌ This contact has no email address. Choose another contact.", "", Noneif not user_email or "@" not in user_email:return "❌ Please enter your email (so the organisation can contact you).", "", Noneif not sender_email or not sender_password:9 unmodified linesai_name=ALLY_AI_NAME,logo_url=logo_url or ALLY_AI_LOGO_URL_DEFAULT)# also provide an .eml draft copy (optional)_, eml_path = build_mailto_and_eml(recipient, subject, message_text, default_from=sender_email)file_out = eml_path if eml_path and os.path.exists(eml_path) else Nonereturn status, "", file_outelse:# Draft-only pathrecip_for_draft = recipient if (recipient and recipient!="Not found") else ""mailto, eml_path = build_mailto_and_eml(recip_for_draft, subject, message_text, default_from="noreply@ally.ai")html_link = f'<a href="{mailto}" target="_blank" rel="noopener">Open draft in email client</a>'file_out = eml_path if eml_path and os.path.exists(eml_path) else Nonereturn "✅ Draft created (no email sent).", html_link, file_outwith gr.Blocks() as demo:gr.Markdown("## Ally AI — GBV Help Finder & Email Assistant\n""This tool searches local organizations, lets you select a contact, and creates an email draft or sends a branded email via SMTP.\n""**Privacy tip:** Prefer anonymized summaries unless you’re comfortable sharing details.")with gr.Row():2 unmodified linessearch_btn = gr.Button("Search for professionals")summary_out = gr.Textbox(label="Search summary (AI)", interactive=False)results_table = gr.Dataframe(headers=["title","url","email","phone","source_query"], label="Search results")dropdown_sel = gr.Dropdown(label="Select organization (from results)", choices=[])6 unmodified linessubject_in = gr.Textbox(value="Request for GBV support", label="Email subject")message_in = gr.Textbox(label="Message body", lines=10)with gr.Accordion("Sending options (for automatic sending via Ally AI SMTP)", open=False):mode = gr.Radio(choices=["Draft only (mailto + .eml)", "Send via SMTP (Gmail)"], value="Draft only (mailto + .eml)", label="Delivery mode")sender_email_in = gr.Textbox(label="Ally AI sender email (SMTP account)")13 unmodified lines# Wire: Searchdef _on_search(story, country):s, records, options, anon = run_search(story, country)# set dropdown + anonymized text and prefill messageprefill = make_body(anon, story, True, "") # user email unknown yetreturn s, records, gr.update(choices=options, value=(options[0] if options else None)), anon, prefillsearch_btn.click(_on_search,11 unmodified lines# Previewpreview_btn.click(preview_contact,inputs=[dropdown_sel, results_table, subject_in, message_in],outputs=[preview_text_out, preview_html_out])# Confirm (create draft or send)confirm_btn.click(confirm_action,inputs=[mode, dropdown_sel, results_table, subject_in, message_in,user_email_in, sender_email_in, sender_pass_in, logo_url_in],outputs=[status_out, mailto_html_out, eml_file_out])demo.launch(share=False)123421 unmodified lines2627282930315 unmodified lines37383940414243444575 unmodified lines12112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618729 unmodified lines21721821922022122222351 unmodified lines2752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173189 unmodified lines3283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833842 unmodified lines38738838939039139239339424 unmodified lines4194204214224234244253 unmodified lines4294304314324334344353 unmodified lines4394404414424434444454464474484494504514524534544554569 unmodified lines4664674684694704714724733 unmodified lines4774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165176 unmodified lines5245255265275285295305315325335345355365375385395405419 unmodified lines5515525535545555565575585595605615625635645655665675685695705715725735745755765772 unmodified lines5805815825835845855865876 unmodified lines59459559659759859960060160213 unmodified lines61661761861962062162262311 unmodified lines635636637638639640641642643644645646647# (paste this into your existing file, replacing the old content)import osimport timeimport re21 unmodified lines# ============================# CONFIG (ENV VARS recommended)# ============================API_KEY = os.environ.get("GOOGLE_API_KEY", "YOUR_GOOGLE_API_KEY")CX = os.environ.get("GOOGLE_CSE_ID", "YOUR_CSE_ID")DEFAULT_COUNTRY = "Ghana"5 unmodified linesALLY_AI_LOGO_URL_DEFAULT = os.environ.get("ALLY_AI_LOGO_URL","https://i.ibb.co/7nZqz0H/ai-logo.png")COUNTRY_TLD_MAP = {"Ghana":"gh","Nigeria":"ng","Kenya":"ke","South Africa":"za","USA":"us","United Kingdom":"uk"}COUNTRY_REGION_MAP= {"Ghana":"GH","Nigeria":"NG","Kenya":"KE","South Africa":"ZA","USA":"US","United Kingdom":"GB"}HEADERS = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64)"}EMAIL_REGEX = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")75 unmodified linespassreturn list(set(phones))def _domain_from_url(url):try:return urllib.parse.urlparse(url).netlocexcept Exception:return urldef scrape_contacts(url, region="GH"):"""Extended scraping: returns emails, phones, and a guessed 'org' nameextracted from meta tags, headings, or via NER on page text."""try:res = requests.get(url, headers=HEADERS, timeout=12)if not res.ok or not res.text:return {"emails": [], "phones": [], "org": None}soup = BeautifulSoup(res.text, "html.parser")# raw text for phone/email/NERtext = soup.get_text(separator=" ")text = " ".join(text.split())[:300000]# emails & phonesemails = list(set(EMAIL_REGEX.findall(text)))phones = extract_phones(text, region)# try meta og:site_name or twitter site metaorg_name = Nonemeta_og = soup.find("meta", property="og:site_name") or soup.find("meta", attrs={"name":"og:site_name"})if meta_og and meta_og.get("content"):org_name = meta_og.get("content").strip()# fallback to <title> or first <h1>if not org_name:title_tag = soup.find("title")if title_tag and title_tag.get_text(strip=True):org_name = title_tag.get_text(strip=True)if not org_name:h1 = soup.find("h1")if h1 and h1.get_text(strip=True):org_name = h1.get_text(strip=True)# run NER to find ORG mentions in the page text and prefer that if short and cleantry:people, orgs, locs = extract_entities(text)if orgs:# choose first short/clean orgfor o in orgs:if len(o) > 1 and len(o) < 80:org_name = obreakexcept Exception:pass# final fallback: domainif not org_name:org_name = _domain_from_url(url)return {"emails": emails, "phones": phones, "org": org_name}except Exception as e:print(f"[scrape error] {url} -> {e}")return {"emails": [], "phones": [], "org": _domain_from_url(url)}# ============================# NER + STORY → PROFESSIONS29 unmodified linesif p == "gbv":cores += ["GBV support organizations", "gender based violence help"]else:cores += [f"{p} for GBV", f"{p} for sexual assault", f"{p} near me {p} {country}"]unique_cores, seen = [], set()for c in cores:if c not in seen:51 unmodified linesreturn {"summary":"No results found. Try a different country or wording.","professionals":[], "queries_used":queries}# NER on titles/snippets for contextall_people, all_orgs, all_locs = [], [], []for r in search_results:ctx = f"{r.get('title','')}. {r.get('snippet','')}"p,o,l = extract_entities(ctx)all_people += p; all_orgs += o; all_locs += l# Scrape contacts concurrently, extracting org names from page contentprofessionals = []with ThreadPoolExecutor(max_workers=MAX_SCRAPE_WORKERS) as ex:futures = {ex.submit(scrape_contacts, r["link"], region): r for r in search_results}for fut in as_completed(futures):r = futures[fut]contacts = {"emails": [], "phones": [], "org": None}try:contacts = fut.result()except Exception as e:print("[scrape future error]", r["link"], e)# choose a single email/phone if availableemail = contacts["emails"][0] if contacts.get("emails") else Nonephone = contacts["phones"][0] if contacts.get("phones") else Noneorg_name = contacts.get("org") or ""# attempt to choose profession tag from the query usedprof_tag = Noneqlower = (r.get("query") or "").lower()for p in professions_from_story(story):if p in qlower:prof_tag = pbreakprof_tag = prof_tag or (professions_from_story(story)[0] if professions_from_story(story) else "gbv")professionals.append({"org": org_name,"url": r.get("link",""),"email": email if email else "Not found","phone": phone if phone else "Not found","profession": prof_tag,"source_query": r.get("query","")})9 unmodified lines# DRAFT (mailto + .eml)# ============================def build_mailto_and_eml(to_addr, subject, body, default_from="noreply@ally.ai"):"""Creates a proper .eml file and returns (mailto_link, absolute_eml_path).Ensures the file is actually written and non-empty. If .eml fails, writes a .txt fallback."""# sanitize inputsto_addr = (to_addr or "").strip()subject = subject or ""body = body or ""# Create EmailMessage objectmsg = EmailMessage()msg["From"] = default_frommsg["To"] = to_addrmsg["Subject"] = subjectmsg.set_content(body)# ensure output dir exists and use absolute path (more robust for HF Spaces / Colab)out_dir = os.path.abspath("tmp")os.makedirs(out_dir, exist_ok=True)fname = os.path.join(out_dir, f"email_draft_{int(time.time())}.eml")try:# write byteswith open(fname, "wb") as f:f.write(msg.as_bytes())# verify file exists and is non-emptyif os.path.exists(fname) and os.path.getsize(fname) > 0:mailto = f"mailto:{urllib.parse.quote(to_addr)}?subject={urllib.parse.quote(subject)}&body={urllib.parse.quote(body)}"return mailto, fname# fallback: create a plain text copy (useful for debugging)fallback = fname + ".txt"with open(fallback, "w", encoding="utf-8") as f:f.write(f"To: {to_addr}\nSubject: {subject}\n\n{body}")mailto = f"mailto:{urllib.parse.quote(to_addr)}?subject={urllib.parse.quote(subject)}&body={urllib.parse.quote(body)}"return mailto, fallbackexcept Exception as e:# If writing .eml fails entirely, create a .txt fallback and return that pathfallback = os.path.join(out_dir, f"email_draft_{int(time.time())}.txt")try:with open(fallback, "w", encoding="utf-8") as f:f.write(f"Error writing .eml: {e}\n\nTo: {to_addr}\nSubject: {subject}\n\n{body}")mailto = f"mailto:{urllib.parse.quote(to_addr)}?subject={urllib.parse.quote(subject)}&body={urllib.parse.quote(body)}"return mailto, fallbackexcept Exception as e2:# ultimate fallback: return no file and an informative mailtomailto = f"mailto:{urllib.parse.quote(to_addr)}?subject={urllib.parse.quote(subject)}&body={urllib.parse.quote(body)}"return mailto, None# ============================# SENDER (SMTP) — Ally AI branding2 unmodified linessender_email, sender_password,ai_name=ALLY_AI_NAME,logo_url=ALLY_AI_LOGO_URL_DEFAULT):if not to_email or to_email == "Not found":return "❌ No recipient email found — choose a contact or provide a manual email."msg = MIMEMultipart("alternative")msg["Subject"] = subject or "Request for support"24 unmodified linestry:server = smtplib.SMTP("smtp.gmail.com", 587)server.starttls()server.login(sender_email, sender_password)server.sendmail(sender_email, [to_email], msg.as_string())server.quit()return f"✅ Email sent successfully to {to_email}"3 unmodified lines# ============================# GRADIO UI# ============================def run_search(story, country):try:out = find_professionals_from_story(story, country=country, results_per_query=RESULTS_PER_QUERY)except Exception as e:3 unmodified linespros = out.get("professionals", []) or []# build table records with org instead of article titletry:records = pd.DataFrame(pros).to_dict(orient="records") if pros else []except Exception:records = []# build dropdown options as list of stringsoptions = []for i, r in enumerate(pros):label_contact = r.get("email") if r.get("email") and r.get("email") != "Not found" else (r.get("phone", "No contact"))org_label = r.get("org") or r.get("url") or "(no org)"label = f"{i} — {org_label} ({label_contact})"options.append(label)if not options:9 unmodified linessummary = out.get("summary", "No results found.")return summary, records, options, anondef make_body(anon_text, full_story, use_anon, user_email):core = (anon_text or "").strip() if use_anon else (full_story or "").strip()lines = [core,"",3 unmodified lines]return "\n".join([l for l in lines if l is not None])def preview_contact(dropdown_value, df_json, subject, message_text, manual_email):if not dropdown_value:return "No contact selected.", ""try:idx = int(str(dropdown_value).split(" — ")[0])rows = pd.DataFrame(df_json)contact = rows.iloc[idx].to_dict()# choose recipient from manual_email if provided & valid, else scraped emailrecipient = Noneif manual_email and EMAIL_REGEX.search(manual_email):recipient = manual_email.strip()else:recipient = contact.get("email") if contact.get("email") and contact.get("email")!="Not found" else "[no email]"org_display = contact.get('org') or contact.get('url') or "(no org)"html = f"""<h3>Preview</h3><b>To:</b> {recipient}<br/><b>Organization:</b> <a href="{contact.get('url')}" target="_blank" rel="noopener">{org_display}</a><br/><b>Profession tag:</b> {contact.get('profession')}<br/><b>Subject:</b> {subject}<br/><hr/><pre style="white-space:pre-wrap;">{message_text}</pre>"""text = f"To: {recipient}\nOrganization: {org_display}\nSubject: {subject}\n\n{message_text[:600]}{'...' if len(message_text)>600 else ''}"return text, htmlexcept Exception as e:return f"Preview error: {e}", ""def confirm_action(mode, dropdown_value, df_json, subject, message_text,user_email, sender_email, sender_password, logo_url, manual_email):"""mode: "Draft only" or "Send via SMTP (Gmail)"manual_email: optional override to use when scraped email not found"""if not dropdown_value:return "❌ No contact selected.", "", None6 unmodified linesexcept Exception as e:return f"❌ Selection error: {e}", "", Nonescraped_recipient = contact.get("email")# use manual if validrecipient = Noneif manual_email and EMAIL_REGEX.search(manual_email):recipient = manual_email.strip()elif scraped_recipient and scraped_recipient != "Not found":recipient = scraped_recipientif mode.startswith("Send"):# Validate required fieldsif not recipient:return "❌ No recipient email found — either pick a contact with an email or provide a manual email.", "", Noneif not user_email or "@" not in user_email:return "❌ Please enter your email (so the organisation can contact you).", "", Noneif not sender_email or not sender_password:9 unmodified linesai_name=ALLY_AI_NAME,logo_url=logo_url or ALLY_AI_LOGO_URL_DEFAULT)_, eml_path = build_mailto_and_eml(recipient, subject, message_text, default_from=sender_email)file_out = eml_path if eml_path and os.path.exists(eml_path) else Nonereturn status, "", file_outelse:# Draft-only path (mailto + .eml)recip_for_draft = recipient or ""mailto, eml_path = build_mailto_and_eml(recip_for_draft, subject, message_text, default_from="noreply@ally.ai")if eml_path and os.path.exists(eml_path) and os.path.getsize(eml_path) > 0:html_link = f'<a href="{mailto}" target="_blank" rel="noopener">Open draft in email client</a>'file_out = eml_pathreturn "✅ Draft created (no email sent).", html_link, file_outelif eml_path and os.path.exists(eml_path):# file exists but is emptyreturn "⚠️ Draft file created but it's empty. Check the message body or try manual email.", "", eml_pathelse:return "❌ Failed to create draft file.", "", Nonewith gr.Blocks() as demo:gr.Markdown("## Ally AI — GBV Help Finder & Email Assistant\n""This tool searches local professionals/organizations (not article pages), lets you select a contact or enter an email manually, and creates an email draft or sends a branded email via SMTP.\n""**Privacy tip:** Prefer anonymized summaries unless you’re comfortable sharing details.")with gr.Row():2 unmodified linessearch_btn = gr.Button("Search for professionals")summary_out = gr.Textbox(label="Search summary (AI)", interactive=False)# updated headers: use org (organization name) instead of article titleresults_table = gr.Dataframe(headers=["org","url","email","phone","profession","source_query"], label="Search results")dropdown_sel = gr.Dropdown(label="Select organization (from results)", choices=[])6 unmodified linessubject_in = gr.Textbox(value="Request for GBV support", label="Email subject")message_in = gr.Textbox(label="Message body", lines=10)# Manual override for organization email (new)manual_email_in = gr.Textbox(label="Manual org email (optional) - used when a scraped email isn't available")with gr.Accordion("Sending options (for automatic sending via Ally AI SMTP)", open=False):mode = gr.Radio(choices=["Draft only (mailto + .eml)", "Send via SMTP (Gmail)"], value="Draft only (mailto + .eml)", label="Delivery mode")sender_email_in = gr.Textbox(label="Ally AI sender email (SMTP account)")13 unmodified lines# Wire: Searchdef _on_search(story, country):s, records, options, anon = run_search(story, country)prefill = make_body(anon, story, True, "") # user email unknown yet# return updated dropdown choices (value is first option)return s, records, gr.update(choices=options, value=(options[0] if options else None)), anon, prefillsearch_btn.click(_on_search,11 unmodified lines# Previewpreview_btn.click(preview_contact,inputs=[dropdown_sel, results_table, subject_in, message_in, manual_email_in],outputs=[preview_text_out, preview_html_out])# Confirm (create draft or send) - manual_email_in passed as last argconfirm_btn.click(confirm_action,inputs=[mode, dropdown_sel, results_table, subject_in, message_in,user_email_in, sender_email_in, sender_pass_in, logo_url_in, manual_email_in],outputs=[status_out, mailto_html_out, eml_file_out])demo.launch(share=False)
app.py+170-88