import streamlit as st import pandas as pd from db import get_leads, init_db, reset_lead import json import re import os from enrich import run_sync, refresh_ce_data, sync_single_lead from generate_reply import generate_email_draft def clean_html_to_text(html_content): """Simple helper to convert HTML email body to readable plain text.""" if not html_content: return "" # Remove head and style tags entirely clean = re.sub(r'.*?', '', html_content, flags=re.DOTALL | re.IGNORECASE) clean = re.sub(r'.*?', '', clean, flags=re.DOTALL | re.IGNORECASE) # Replace
and

with newlines clean = re.sub(r'', '\n', clean, flags=re.IGNORECASE) clean = re.sub(r'

', '\n', clean, flags=re.IGNORECASE) # Remove all other tags clean = re.sub(r'<.*?>', '', clean) # Decode some common entities clean = clean.replace(' ', ' ').replace('&', '&').replace('"', '"') # Cleanup multiple newlines clean = re.sub(r'\n\s*\n+', '\n\n', clean).strip() return clean st.set_page_config(page_title="TradingTwins Lead Engine", layout="wide") st.title("🚀 Lead Engine: TradingTwins") # Sidebar Actions st.sidebar.header("Actions") if st.sidebar.button("1. Ingest Emails (Mock)"): from ingest import ingest_mock_leads init_db() count = ingest_mock_leads() st.sidebar.success(f"Ingested {count} new leads.") st.rerun() if st.sidebar.button("2. Ingest Real Emails (Graph API)"): try: from trading_twins_ingest import process_leads with st.spinner("Fetching emails from Microsoft Graph..."): count = process_leads() if count > 0: st.sidebar.success(f"Successfully ingested {count} new leads form inbox!") else: st.sidebar.info("No new leads found in inbox.") st.rerun() except Exception as e: st.sidebar.error(f"Ingest failed: {e}") if st.sidebar.button("3. Sync to Company Explorer"): with st.spinner("Syncing with Company Explorer API..."): # Capture output for debugging try: # We redirect stdout to capture prints import io from contextlib import redirect_stdout f = io.StringIO() with redirect_stdout(f): run_sync() output = f.getvalue() st.success("Sync finished!") with st.expander("See Process Log", expanded=True): st.code(output) except Exception as e: st.error(f"Sync Failed: {e}") if st.sidebar.checkbox("Show System Debug"): st.sidebar.subheader("System Diagnostics") # 1. API Key Check from lookup_role import get_gemini_key key = get_gemini_key() if key: st.sidebar.success(f"Gemini Key found ({key[:5]}...)") else: st.sidebar.error("Gemini Key NOT found!") # 2. SerpAPI Check serp_key = os.getenv("SERP_API") if serp_key: st.sidebar.success(f"SerpAPI Key found ({serp_key[:5]}...)") else: st.sidebar.error("SerpAPI Key NOT found in Env!") # 3. Network Check try: import requests res = requests.get("https://generativelanguage.googleapis.com", timeout=2) st.sidebar.success(f"Gemini API Reachable ({res.status_code})") except Exception as e: st.sidebar.error(f"Network Error: {e}") # 4. Live Lookup Test if st.sidebar.button("Test Role Lookup (Georg Stahl)"): from lookup_role import lookup_person_role with st.sidebar.status("Running Lookup..."): res = lookup_person_role("Georg Stahl", "Klemm Bohrtechnik GmbH") if res: st.sidebar.success(f"Result: {res}") else: st.sidebar.error("Result: None") # Main View leads = get_leads() df = pd.DataFrame(leads) if not df.empty: col1, col2, col3 = st.columns(3) col1.metric("Total Leads", len(df)) col2.metric("New / Unsynced", len(df[df['status'] == 'new'])) col3.metric("Synced to CE", len(df[df['status'] == 'synced'])) st.subheader("Lead Pipeline") for index, row in df.iterrows(): # Format date for title date_str = "" if row.get('received_at'): try: dt = pd.to_datetime(row['received_at']) date_str = dt.strftime("%d.%m. %H:%M") except: pass with st.expander(f"{date_str} | {row['company_name']}"): # Metadata Parsing meta = {} if row.get('lead_metadata'): try: meta = json.loads(row['lead_metadata']) except: pass # --- TOP SECTION: QUALITY WARNING --- if meta.get('is_low_quality'): st.warning("⚠️ **Low Quality Lead detected** (Free-mail or missing company).") # --- SECTION 1: LEAD INFO (2 Columns) --- st.markdown("### 📋 Lead Data") c1, c2 = st.columns(2) with c1: st.write(f"**Contact:** {row['contact_name']}") st.write(f"**Email:** {row['email']}") role = meta.get('role') if role: st.info(f"**Role:** {role}") else: if st.button("🔍 Find Role", key=f"role_{row['id']}"): from enrich import enrich_contact_role with st.spinner("Searching..."): found_role = enrich_contact_role(row) if found_role: st.success(f"Found: {found_role}"); st.rerun() else: st.error("No role found.") with c2: st.write(f"**Area:** {meta.get('area', '-')}") st.write(f"**Purpose:** {meta.get('purpose', '-')}") st.write(f"**Location:** {meta.get('zip', '')} {meta.get('city', '')}") with st.expander("Original Body Preview"): st.text(clean_html_to_text(row['raw_body'])) if st.checkbox("Show HTML", key=f"raw_{row['id']}"): st.code(row['raw_body'], language="html") st.divider() # --- SECTION 2: INTELLIGENCE (CE) --- st.markdown("### 🔍 Intelligence (CE)") enrichment = json.loads(row['enrichment_data']) if row['enrichment_data'] else {} ce_id = enrichment.get('ce_id') if ce_id: st.success(f"✅ Linked to Company Explorer (ID: {ce_id})") ce_data = enrichment.get('ce_data', {}) vertical = ce_data.get('industry_ai') or ce_data.get('vertical') summary = ce_data.get('research_dossier') or ce_data.get('summary') intel_col1, intel_col2 = st.columns([1, 2]) with intel_col1: if vertical and vertical != 'None': st.info(f"**Industry:** {vertical}") else: st.warning("Industry Analysis pending...") if st.button("🔄 Refresh CE Data", key=f"refresh_{row['id']}"): with st.spinner("Fetching..."): refresh_ce_data(row['id'], ce_id) st.rerun() with intel_col2: if summary: with st.expander("Show AI Research Dossier", expanded=True): st.write(summary) else: st.warning("⚠️ Not synced with Company Explorer yet") if st.button("🚀 Sync to Company Explorer", key=f"sync_single_{row['id']}"): with st.spinner("Syncing..."): sync_single_lead(row['id']) st.rerun() st.divider() # --- SECTION 3: RESPONSE DRAFT --- st.markdown("### ✉️ Response Draft") if row['status'] != 'new' and ce_id: if st.button("✨ Generate Expert Reply", key=f"gen_{row['id']}", type="primary"): with st.spinner("Writing email..."): ce_data = enrichment.get('ce_data', {}) draft = generate_email_draft(row.to_dict(), ce_data) st.session_state[f"draft_{row['id']}"] = draft if f"draft_{row['id']}" in st.session_state: st.text_area("Email Entwurf", value=st.session_state[f"draft_{row['id']}"], height=400) st.button("📋 Copy to Clipboard", key=f"copy_{row['id']}", on_click=lambda: st.write("Copy functionality simulated")) else: st.info("Sync with Company Explorer first to generate a response.") if row['status'] != 'new': st.markdown("---") if st.button("🔄 Reset Lead Status", key=f"reset_{row['id']}", help="Back to 'new' status"): reset_lead(row['id']) st.rerun() else: st.info("No leads found. Click 'Ingest Emails' in the sidebar.")