import gradio as gr import pandas as pd import plotly.graph_objects as go import time import random import os import asyncio from dotenv import load_dotenv from typing import List, Dict import itertools # --- Core Backend Logic (Adapted from your FastAPI code) --- from langchain.agents import AgentExecutor, create_openai_tools_agent from langchain.prompts import ChatPromptTemplate from langchain_community.tools.tavily_search import TavilySearchResults from langchain_openai import ChatOpenAI from unidecode import unidecode from openai import OpenAI import re from utils import * from prompt import * from mongo import * from LLMClient import * # Initialize loader (no need to pass db/collection separately) mongo_loader = MongoDataFrameLoader(os.getenv("URI"), os.getenv("DATABASE_NAME"), os.getenv("COLLECTION_NAME")) mongo_loader_results = MongoDataFrameLoader(os.getenv("URI"), os.getenv("DATABASE_NAME"), os.getenv("COLLECTION_NAME_RESULTS")) # --- Core Generation Logic (reusable by single and bulk handlers) --- async def generate_sequence_for_prospect(email, product_desc, llm_provider, llm_model, progress=None): llm = None # llm_provider = "OpenAI" # llm_model = "gpt-4o" # try: # if llm_provider == "OpenAI": llm = ChatOpenAI(model=llm_model, api_key=os.getenv("OPENAI_API_KEY")) # elif llm_provider == "Deepseek": llm = ChatOpenAI(model=llm_model, api_key=os.getenv("DEEPSEEK_API_KEY"), base_url="https://api.deepseek.com/v1") # if not llm: raise ValueError("LLM could not be initialized.") # except Exception as e: return f"Error initializing LLM: {e}", "" # llm = OpenRouterLLM(llm_provider, llm_model , openrouter_api_key) llm = create_openrouter_llm(openrouter_api_key, llm_provider, llm_model) prompt_template = prompt tools = [ TavilySearchResults(max_results=3, description="A standard search engine tool."), custom_web_researcher, ] agent = create_openai_tools_agent(llm, tools, prompt_template) agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True) first_name = get_name_from_email(email) or "Team" company_name = get_company_name_from_email(email) if progress: progress(0.5, desc=f"Researching {company_name}...") try: response = await agent_executor.ainvoke({ "company_name": company_name, "first_name": first_name, "product_description": product_desc or OUR_PRODUCT_DESCRIPTION, "input": f"Research {company_name} and write the 8-email sales sequence for {first_name}." }) if progress: progress(1, desc="Complete.") return None, response['output'] except Exception as e: if progress: progress(1, desc="Error.") return f"Error during generation: {e}", "" # --- Gradio UI Definition --- custom_css = """ #csv-upload { border: 2px dashed #00a696; border-radius: 10px; padding: 20px; text-align: center; background-color: #f8f9fa; } #generate-bulk-btn { font-size: 16px; font-weight: bold; border-radius: 25px; padding: 12px 24px; background-color: #00a696; color: white; } #status-output { background-color: #f8f9fa; border-left: 4px solid #00a696; padding: 10px; color: #333333; } #results-dataframe { border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); border: 1px solid #00a696; } #download-file { background-color: #e8f5e8; border-radius: 8px; padding: 15px; border: 1px solid #00a696; } #download-info { color: #00a696; font-style: italic; } """ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.utils.colors.emerald), title="Advanced Sales Email Generator", css=custom_css) as demo: gr.Markdown("# Advanced Sales Email Generator") with gr.Tabs(): # ================================================================= # TAB 1: Email Generation (NEW PRIORITY 1) # ================================================================= with gr.TabItem("Email Generation", id=0): with gr.Row(): with gr.Column(scale=1): # --- INPUT & CONFIG --- gr.Markdown("### 1. Configuration") llm_provider_dropdown = gr.Dropdown(label="LLM Provider", choices=["openai", "deepseek", "google"], value="openai") llm_model_dropdown = gr.Dropdown(label="LLM Model", choices=["o3", "gpt-4o", "gpt-4o-mini"], value="gpt-4o", interactive=True) product_desc_input = gr.Textbox(label="Product Description", lines=5, value=OUR_PRODUCT_DESCRIPTION) def update_models(provider): if provider == "openai": return gr.Dropdown(choices=["o3", "gpt-4o", "gpt-4o-mini"], value="gpt-4o", interactive=True) elif provider == "deepseek": return gr.Dropdown(choices=["deepseek-chat"], value="deepseek-chat", interactive=True) elif provider == "google": return gr.Dropdown(choices=["gemini-2.5-pro"], value="gemini-2.5-pro", interactive=True) llm_provider_dropdown.change(fn=update_models, inputs=llm_provider_dropdown, outputs=llm_model_dropdown) with gr.Column(scale=2): # --- GENERATION INPUT & OUTPUT --- with gr.Tabs(): with gr.TabItem("Single Email"): gr.Markdown("### 2. Enter an Email Address") single_email_input = gr.Textbox(label="Email", placeholder="e.g., jane.doe@acme-corp.com") generate_single_btn = gr.Button("Generate Sequence", variant="primary") gr.Markdown("### 3. Generated Sequence") # Define the output components here, but make them invisible initially single_status_output = gr.Textbox(label="Processing Status", interactive=False, visible=False) # Create accordion with actual content components inside with gr.Accordion(label="Generated Emails", visible=False) as single_email_output_accordion: single_email_content = gr.HTML(value="", visible=True) with gr.TabItem("Bulk CSV Upload"): gr.Markdown("### 📁 2. Upload CSV File") with gr.Row(): with gr.Column(scale=2): csv_upload = gr.File( label="Upload CSV with emails in first column", file_types=[".csv"], elem_id="csv-upload" ) with gr.Column(scale=1): gr.Markdown(""" **📋 CSV Format Requirements:** - First column must contain email addresses - Headers are recommended - UTF-8 encoding preferred """) gr.Markdown("### ⚙️ Processing Settings") with gr.Row(): with gr.Column(): concurrent_tasks_slider = gr.Slider( minimum=1, maximum=10, value=2, step=1, label="🔄 Concurrent Tasks", info="Higher values = faster processing but more resource intensive" ) with gr.Column(): display_rows_slider = gr.Slider( minimum=1, maximum=50, value=5, step=1, label="👁️ Rows to Preview", info="Number of rows to display in results preview" ) generate_bulk_btn = gr.Button( "🚀 Generate from CSV", variant="primary", size="lg", elem_id="generate-bulk-btn" ) gr.Markdown("### 📊 Results Preview & Download") # Status section with better styling with gr.Group(): bulk_status_output = gr.Textbox( label="📈 Processing Status", interactive=False, lines=2, elem_id="status-output" ) # Results preview with enhanced styling with gr.Group(): gr.Markdown("#### 🔍 Generated Data Preview") bulk_df_output = gr.DataFrame( label="Results Table", interactive=False, wrap=True, elem_id="results-dataframe", column_widths=["15%", "85%"] # Adjust based on your columns ) # Download section with gr.Row(): bulk_file_download = gr.File( label="💾 Download Full CSV", interactive=False, elem_id="download-file" ) download_info = gr.Markdown( "📝 *Full results will appear here after processing completes*", visible=True, elem_id="download-info" ) async def handle_generate_single(email, prod_desc, provider, model, progress=gr.Progress(track_tqdm=True)): """ Proper async generator that yields intermediate updates and final result """ # Path 1: Input validation if not email or not email.strip(): yield ( gr.update(value="Error: Please enter an email address.", visible=True), gr.update(visible=False), gr.update(value="") ) return # Show initial loading message yield ( gr.update(value="📡 Connecting to language model...", visible=True), gr.update(visible=False), gr.update(value="") ) # Define loading messages with estimated time for each stage loading_stages = [ {"message": "📡 Connecting to language model...", "time": 3}, {"message": "🔍 Searching with web tool...", "time": 8}, {"message": "🧠 Analyzing company information...", "time": 5}, {"message": "✍️ Drafting email body...", "time": 10}, {"message": "📝 Crafting subject lines...", "time": 6}, {"message": "🔄 Refining email sequence...", "time": 7}, {"message": "⚙️ Finalizing generation...", "time": 4} ] # Start background task for email generation generation_task = asyncio.create_task( generate_sequence_for_prospect(email, prod_desc, provider, model, progress)) # Track the start time of generation start_time = time.time() # Show loading animation with progressive stages based on elapsed time current_stage = 0 elapsed_time = 0 stage_complete = False while not generation_task.done(): # Calculate elapsed time elapsed_time = time.time() - start_time # Determine current stage based on elapsed time cumulative_time = 0 for i, stage in enumerate(loading_stages): cumulative_time += stage["time"] if elapsed_time < cumulative_time or i == len(loading_stages) - 1: current_stage = i break # Display current stage message yield ( gr.update(value=loading_stages[current_stage]["message"], visible=True), gr.update(visible=False), gr.update(value="") ) # Wait before updating again await asyncio.sleep(1) try: # Get the result from the completed task error, output_text = await generation_task # Path 2: Generation failed if error: yield ( gr.update(value=f"Error: {error}", visible=True), gr.update(visible=False), gr.update(value="") ) return # Path 3: Success if output_text: email_bodies = output_text.split('---EMAIL_SEPARATOR---') html_content = "" for i, body in enumerate(email_bodies): if body.strip(): sanitized_body = sanitize_text(body.strip()) html_content += f"""

📧 Email {i + 1}

{sanitized_body}
""" yield ( gr.update(value="✅ Generation complete!", visible=True), gr.update(visible=True), gr.update(value=html_content) ) else: yield ( gr.update(value="⚠️ Warning: No content generated.", visible=True), gr.update(visible=False), gr.update(value="") ) except Exception as e: yield ( gr.update(value=f"❌ Error: {str(e)}", visible=True), gr.update(visible=False), gr.update(value="") ) async def handle_generate_bulk(file_obj, prod_desc, provider, model, concurrency, n_rows, progress=gr.Progress()): if not file_obj: return "❌ Please upload a CSV file.", None, None try: progress(0, desc="📖 Reading CSV...") df = pd.read_csv(file_obj.name) if df.empty: return "❌ The uploaded CSV file is empty.", None, None email_column = df.columns[0] total_rows = len(df) progress(0.1, desc=f"🔄 Processing {total_rows} rows...") # Create tasks with concurrency control tasks = [ generate_sequence_for_prospect(row[email_column], prod_desc, provider, model) for _, row in df.iterrows() ] results = [] completed = 0 # Process with progress tracking for i, task in enumerate(asyncio.as_completed(tasks)): error, output_text = await task row_data = df.iloc[i].to_dict() if not error and output_text: email_bodies = output_text.split('---EMAIL_SEPARATOR---') # for j, body in enumerate(email_bodies): # if j < 8: # Limit to 8 emails # row_data[f'Email_{j+1}'] = sanitize_text(body.strip()) for j, block in enumerate(email_bodies): if j < 8: # Limit to 8 emails block = block.strip() if block: lines = block.splitlines() subject_line = '' body_lines = [] for line in lines: if line.startswith('Subject:'): subject_line = line.replace('Subject:', '').strip() elif line.startswith('**Subject:**'): subject_line = line.replace('**Subject:**', '').strip() else: if line.startswith('**Email') or line.startswith( 'Email') or line.startswith('### Email') or line.startswith( '## Email') or ('Based on the research' in line): continue body_lines.append(line) subject = sanitize_text(subject_line) body = sanitize_text('\n'.join(body_lines).strip()) row_data[f'Email_{j + 1}_Subject'] = subject row_data[f'Email_{j + 1}'] = body row_data["LLM_Provider"] = provider row_data["LLM_Model"] = model row_data['Error'] = error if error else '' results.append(row_data) completed += 1 # Update progress progress_percent = completed / total_rows progress( progress_percent, desc=f"✅ Processed {completed}/{total_rows} ({progress_percent * 100:.1f}%)" ) # Create output dataframe output_df = pd.DataFrame(results) # Generate timestamped filename timestamp = int(time.time()) output_path = f"generated_sequence_{timestamp}.csv" output_df.to_csv(output_path, index=False) # Save to MongoDB try: mongo_loader.load_dataframe(output_df) db_status = "✅ Saved to database" except Exception as e: db_status = f"⚠️ Database save warning: {str(e)}" # Prepare preview dataframe with better formatting preview_df = output_df.head(n_rows).copy() # Truncate long email content for preview email_cols = [col for col in preview_df.columns if col.startswith('Email_')] for col in email_cols: if col in preview_df.columns: preview_df[col] = preview_df[col].astype(str).apply( lambda x: x[:100] + "..." if len(str(x)) > 100 else x ) success_count = len([r for r in results if not r.get('Error')]) error_count = total_rows - success_count status_msg = f""" 🎉 **Generation Complete!** 📊 **Summary:** - Total processed: {total_rows} - Successful: {success_count} - Errors: {error_count} - Preview showing: {min(n_rows, len(output_df))} rows {db_status} """ return status_msg.strip(), preview_df, output_path except Exception as e: error_msg = f"❌ **Error during processing:** {str(e)}" return error_msg, None, None generate_single_btn.click( fn=handle_generate_single, # Use handle_generate_single for generator version inputs=[single_email_input, product_desc_input, llm_provider_dropdown, llm_model_dropdown], outputs=[single_status_output, single_email_output_accordion, single_email_content], show_progress=True ) # Event binding with correct number of outputs generate_bulk_btn.click( fn=handle_generate_bulk, inputs=[ csv_upload, product_desc_input, llm_provider_dropdown, llm_model_dropdown, concurrent_tasks_slider, display_rows_slider ], outputs=[ bulk_status_output, bulk_df_output, bulk_file_download ] # Exactly 3 outputs to match the function returns ) # Optional: Add custom CSS for better styling css = """ .gradio-container { background-color: #000000 !important; color: white !important; } #csv-upload { border: 2px dashed #ccc; border-radius: 10px; padding: 20px; text-align: center; } #generate-bulk-btn { font-size: 16px; font-weight: bold; border-radius: 25px; padding: 12px 24px; } #status-output { background-color: #f8f9fa; border-left: 4px solid #00a696; padding: 10px; } #results-dataframe { border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } #download-file { background-color: #e8f5e8; border-radius: 8px; padding: 15px; } #download-info { color: #666; font-style: italic; } """ # ================================================================= # TAB 2: A/B Testing # ================================================================= with gr.TabItem("A/B Testing", id=1): gr.Markdown( "## Side-by-Side Email Comparison\nSelect the email you think is more effective. Your choice is anonymous and helps improve our models.") with gr.Row(): with gr.Column(scale=1): start_test_btn = gr.Button("Start New A/B Test", variant="primary") with gr.Column(scale=2): test_status_text = gr.Markdown("Click the button to load a new comparison.") with gr.Row(): with gr.Column(scale=1): gr.Textbox(label="Option 1", interactive=False) # ANONYMOUS LABEL email_a_content = gr.Markdown(elem_id="md-left") with gr.Column(scale=1): gr.Textbox(label="Option 2", interactive=False) # ANONYMOUS LABEL email_b_content = gr.Markdown(elem_id="md-right") with gr.Row(): choose_left_btn = gr.Button("⬅️ Choose Option 1", variant="secondary") skip_btn = gr.Button("Skip / Can't Decide", variant="stop") choose_right_btn = gr.Button("Choose Option 2 ➡️", variant="secondary") current_test_data = gr.State({}) def start_new_test(): # comparison = db_service.get_ab_test_comparison() comparison = mongo_loader.get_random_email_pair() while comparison is None: comparison = mongo_loader.get_random_email_pair() if comparison is None: return "No more prospects to test.", "", "", {} status = f"**Comparing Email Position:** `{comparison['email_position']}` for company **{comparison['prospect_company']}**" return status, comparison['email_a_content'], comparison['email_b_content'], comparison def record_vote(choice: str, data: dict): if not data: return "Please start a test first.", "", "", {} # db_service.save_ab_test_result(data, choice) # change in mongodb mongo_loader_results.save_ab_test_result(data, choice) return start_new_test() start_test_btn.click(fn=start_new_test, outputs=[test_status_text, email_a_content, email_b_content, current_test_data]) choose_left_btn.click(lambda data: record_vote("left", data), inputs=[current_test_data], outputs=[test_status_text, email_a_content, email_b_content, current_test_data]) choose_right_btn.click(lambda data: record_vote("right", data), inputs=[current_test_data], outputs=[test_status_text, email_a_content, email_b_content, current_test_data]) skip_btn.click(lambda data: record_vote("skip", data), inputs=[current_test_data], outputs=[test_status_text, email_a_content, email_b_content, current_test_data]) # ================================================================= # TAB 3: Analytics Dashboard # ================================================================= with gr.TabItem("Analytics Dashboard", id=2): gr.Markdown( "## Performance Metrics\nReview how different AI models are performing based on A/B test results.") refresh_analytics_btn = gr.Button("Refresh Analytics", variant="primary") with gr.Row(): win_rate_plot = gr.Plot(label="Overall Model Win Rate") one_v_one_plot = gr.Plot(label="1-vs-1 Head-to-Head Win Rates") with gr.Row(): position_perf_plot = gr.Plot(label="Performance by Email Position") def update_analytics(): # analytics = db_service.get_analytics_data() # change in mongodb analytics = mongo_loader_results.get_analytics_data() # Plot 1: Overall Win Rate wr = analytics.get("win_rates", {}) fig1 = go.Figure([go.Bar(x=list(wr.keys()), y=list(wr.values()), text=[f'{r:.1f}%' for r in wr.values()], textposition='auto')]) fig1.update_layout(title_text='Overall Model Win Rate (%)', yaxis_title='Win Rate') # Plot 2: Position Performance pp = analytics.get("position_performance", {}) fig2 = go.Figure() for m, d in pp.items(): fig2.add_trace( go.Scatter(x=[f"Email {i}" for i in range(1, 9)], y=d, mode='lines+markers', name=m)) fig2.update_layout(title_text='Win Rate by Email Position', yaxis_title='Win Rate (%)') # Plot 3: 1-vs-1 Heatmap ovo = analytics.get("one_v_one", {}) models = analytics.get("all_models", []) heatmap_data = [] for m1 in models: row = [] for m2 in models: if m1 == m2: row.append(None) # No self-comparison else: total = ovo.get(m1, {}).get(m2, {}).get("total", 0) wins = ovo.get(m1, {}).get(m2, {}).get("wins", 0) rate = (wins / total * 100) if total > 0 else 0 row.append(rate) heatmap_data.append(row) fig3 = go.Figure(data=go.Heatmap(z=heatmap_data, x=models, y=models, colorscale='Viridis', text=[[f'{v:.1f}%' if v is not None else '' for v in row] for row in heatmap_data], texttemplate="%{text}", textfont={"size": 10})) fig3.update_layout(title="1-vs-1 Win Rate Matrix (Row model vs. Column model)") return fig1, fig2, fig3 refresh_analytics_btn.click(fn=update_analytics, outputs=[win_rate_plot, position_perf_plot, one_v_one_plot]) # This is the entry point to run the Gradio app if __name__ == "__main__": demo.launch(pwa=True)