import os import time import re import requests import phonenumbers import pandas as pd import urllib.parse from bs4 import BeautifulSoup import torch from transformers import ( AutoTokenizer, AutoModelForTokenClassification, AutoModelForSeq2SeqLM, pipeline ) import gradio as gr from concurrent.futures import ThreadPoolExecutor, as_completed from email.message import EmailMessage import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # ============================ # CONFIG # ============================ 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" RESULTS_PER_QUERY = int(os.environ.get("RESULTS_PER_QUERY", 4)) MAX_SCRAPE_WORKERS = int(os.environ.get("MAX_SCRAPE_WORKERS", 6)) ALLY_AI_NAME = os.environ.get("ALLY_AI_NAME", "Ally AI") ALLY_AI_LOGO_URL_DEFAULT = os.environ.get("ALLY_AI_LOGO_URL", "https://imgur.com/a/lVxnQke") 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,}") # ============================ # MODELS (lightweight & CPU-friendly) # ============================ DEVICE = "cuda" if torch.cuda.is_available() else "cpu" print("Device set to use", DEVICE) # NER model (people/orgs/locs) ner_model_id = "dslim/bert-base-NER" ner_tokenizer = AutoTokenizer.from_pretrained(ner_model_id) ner_model = AutoModelForTokenClassification.from_pretrained(ner_model_id) ner_pipe = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, aggregation_strategy="simple", device=0 if DEVICE=="cuda" else -1) # Summarizer / anonymizer text_model_id = "google/flan-t5-large" text_tokenizer = AutoTokenizer.from_pretrained(text_model_id) text_model = AutoModelForSeq2SeqLM.from_pretrained(text_model_id).to(DEVICE) # ============================ # TAXONOMY & HELPERS # ============================ PROFESSION_KEYWORDS = ["lawyer","therapist","doctor","counselor","social worker", "advocate","psychologist","psychiatrist","consultant","nurse","hotline","gbv"] PROBLEM_PROFESSION_MAP = { "rape": ["lawyer","therapist","counselor","doctor"], "sexual assault": ["lawyer","therapist","counselor"], "domestic violence": ["lawyer","social worker","therapist"], "abuse": ["counselor","social worker","therapist","lawyer"], "trauma": ["therapist","psychologist","psychiatrist"], "depression": ["therapist","psychologist","doctor"], "violence": ["lawyer","counselor","social worker"], } def get_region_for_country(country: str) -> str: return COUNTRY_REGION_MAP.get(country, "GH") def get_tld_for_country(country: str) -> str: return COUNTRY_TLD_MAP.get(country, "") def build_country_biased_query(core: str, country: str) -> str: tld = get_tld_for_country(country) suffix = f" in {country}" if tld: return f"{core}{suffix} site:.{tld} OR {country}" return f"{core}{suffix}" def dedup_by_url(items): seen, out = set(), [] for it in items: u = it.get("link") or it.get("url") if u and u not in seen: seen.add(u) out.append(it) return out # ============================ # SEARCH & SCRAPING # ============================ def google_search(query, num_results=5): if not API_KEY or not CX or "YOUR_GOOGLE_API_KEY" in API_KEY or "YOUR_CSE_ID" in CX: raise RuntimeError("Google API key and CSE ID must be set as environment variables.") url = "https://www.googleapis.com/customsearch/v1" params = {"q":query, "key":API_KEY, "cx":CX, "num":num_results} r = requests.get(url, params=params, timeout=20) r.raise_for_status() items = r.json().get("items", []) or [] return [{"title":i.get("title",""), "link":i.get("link",""), "snippet":i.get("snippet","")} for i in items] def extract_phones(text, region="GH"): phones = [] for match in phonenumbers.PhoneNumberMatcher(text, region): try: phones.append(phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)) except Exception: pass return list(set(phones)) def _domain_from_url(url): try: return urllib.parse.urlparse(url).netloc except Exception: return url def scrape_contacts(url, region="GH"): """ Extended scraping: returns emails, phones, and a guessed 'org' name extracted 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/NER text = soup.get_text(separator=" ") text = " ".join(text.split())[:300000] # emails & phones emails = list(set(EMAIL_REGEX.findall(text))) phones = extract_phones(text, region) # try meta og:site_name or twitter site meta org_name = None meta_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
{body}
Contact the survivor back at: {user_email}
This message was prepared with the help of {ai_name} — connecting survivors with help safely.
{message_text}
"""
text = f"To: {recipient}\nOrganization: {org_display}\nSubject: {subject}\n\n{message_text[:600]}{'...' if len(message_text)>600 else ''}"
return text, html
except 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.", "", None
# locate contact
try:
idx = int(str(dropdown_value).split(" — ")[0])
rows = pd.DataFrame(df_json)
contact = rows.iloc[idx].to_dict()
except Exception as e:
return f"❌ Selection error: {e}", "", None
scraped_recipient = contact.get("email")
# use manual if valid
recipient = None
if manual_email and EMAIL_REGEX.search(manual_email):
recipient = manual_email.strip()
elif scraped_recipient and scraped_recipient != "Not found":
recipient = scraped_recipient
if mode.startswith("Send"):
# Validate required fields
if not recipient:
return "❌ No recipient email found — either pick a contact with an email or provide a manual email.", "", None
if not user_email or "@" not in user_email:
return "❌ Please enter your email (so the organisation can contact you).", "", None
if not sender_email or not sender_password:
return "❌ Sender email and app password are required for SMTP sending.", "", None
status = send_ally_ai_email(
to_email=recipient,
subject=subject,
body=message_text,
user_email=user_email,
sender_email=sender_email,
sender_password=sender_password,
ai_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 None
return status, "", file_out
else:
# 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'Open draft in email client'
file_out = eml_path
return "✅ Draft created (no email sent).", html_link, file_out
elif eml_path and os.path.exists(eml_path):
# file exists but is empty
return "⚠️ Draft file created but it's empty. Check the message body or try manual email.", "", eml_path
else:
return "❌ Failed to create draft file.", "", None
with gr.Blocks() as demo:
gr.Markdown("## Ally AI — GBV Help Finder & Email Assistant\n"
"This tool searches local professionals/organizations lets you select a contact or enter an email manually, and creates an email draft or sends a branded email via Gmail"
"**Privacy tip:** Prefer anonymized summaries unless you’re comfortable sharing details.")
with gr.Row():
story_in = gr.Textbox(label="Your story (free text)", lines=6, placeholder="Describe your situation and the help you want...")
country_in = gr.Textbox(value=DEFAULT_COUNTRY, label="Country (to bias search)")
search_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 title
results_table = gr.Dataframe(headers=["org","url","email","phone","profession","source_query"], label="Search results")
dropdown_sel = gr.Dropdown(label="Select organization (from results)", choices=[])
with gr.Row():
use_anon = gr.Checkbox(value=True, label="Use anonymized summary (recommended)")
anon_out = gr.Textbox(label="Anonymized summary", lines=3)
user_email_in = gr.Textbox(label="Your email (for the organisation to reply to you)")
gr.Markdown("### Compose message")
subject_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)")
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 (Gmail account)")
sender_pass_in = gr.Textbox(label="Ally AI sender app password", type="password")
logo_url_in = gr.Textbox(value=ALLY_AI_LOGO_URL_DEFAULT, label="Ally AI logo URL")
with gr.Row():
preview_btn = gr.Button("Preview")
confirm_btn = gr.Button("Confirm (Create Draft or Send)")
preview_text_out = gr.Textbox(label="Preview (text)", interactive=False)
preview_html_out = gr.HTML()
status_out = gr.Textbox(label="Status", interactive=False)
mailto_html_out = gr.HTML()
eml_file_out = gr.File(label="Download .eml")
# Wire: Search
def _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, prefill
search_btn.click(_on_search,
inputs=[story_in, country_in],
outputs=[summary_out, results_table, dropdown_sel, anon_out, message_in])
# When user toggles anonymized vs full story, refresh the message body
def _refresh_body(use_anon_flag, anon_text, story, user_email):
return make_body(anon_text, story, use_anon_flag, user_email)
use_anon.change(_refresh_body, inputs=[use_anon, anon_out, story_in, user_email_in], outputs=message_in)
user_email_in.change(_refresh_body, inputs=[use_anon, anon_out, story_in, user_email_in], outputs=message_in)
anon_out.change(_refresh_body, inputs=[use_anon, anon_out, story_in, user_email_in], outputs=message_in)
story_in.change(_refresh_body, inputs=[use_anon, anon_out, story_in, user_email_in], outputs=message_in)
# Preview
preview_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 arg
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, manual_email_in],
outputs=[status_out, mailto_html_out, eml_file_out])
demo.launch(share=False)