|
|
import gradio as gr |
|
|
import os |
|
|
from dotenv import load_dotenv |
|
|
from simple_salesforce import Salesforce |
|
|
|
|
|
from scorer import get_lead_score, calculate_score, calculate_confidence, calculate_risk |
|
|
from recommender import generate_recommendation |
|
|
from insights import explain_score |
|
|
from logger import log_submission |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
sf_username = os.getenv("SF_USERNAME") |
|
|
sf_password = os.getenv("SF_PASSWORD") |
|
|
sf_security_token = os.getenv("SF_SECURITY_TOKEN") |
|
|
sf_domain = os.getenv("SF_DOMAIN", "login") |
|
|
|
|
|
|
|
|
sf = Salesforce( |
|
|
username=sf_username, |
|
|
password=sf_password, |
|
|
security_token=sf_security_token, |
|
|
domain=sf_domain |
|
|
) |
|
|
|
|
|
def push_to_salesforce(data: dict) -> str: |
|
|
try: |
|
|
response = sf.qualification_engine__c.create({ |
|
|
"Deal_Amount__c": data.get("amount"), |
|
|
"Stage__c": data.get("stage"), |
|
|
"Industry__c": data.get("industry"), |
|
|
"Emails_7_Days__c": data.get("emails"), |
|
|
"Meetings_30_Days__c": data.get("meetings"), |
|
|
"Days_Until_Close__c": data.get("gap"), |
|
|
"Rep_Feedback__c": data.get("feedback"), |
|
|
"Lead_Score__c": data.get("lead_score"), |
|
|
"AI_Score__c": data.get("score"), |
|
|
"Confidence__c": data.get("confidence"), |
|
|
"Risk_Level__c": data.get("risk"), |
|
|
"AI_Recommendation__c": data.get("recommendation"), |
|
|
"Explanation__c": data.get("explanation") |
|
|
}) |
|
|
return f"β
Pushed to Salesforce with ID: {response['id']}" |
|
|
except Exception as e: |
|
|
return f"β Salesforce Error: {str(e)}" |
|
|
|
|
|
def run_engine(amount, stage, industry, emails, meetings, close_gap, feedback=""): |
|
|
try: |
|
|
lead_score = get_lead_score(stage, emails, meetings, close_gap, amount) |
|
|
ai_score = calculate_score(lead_score, emails, meetings, close_gap, amount) |
|
|
confidence = calculate_confidence(ai_score) |
|
|
risk = calculate_risk(ai_score, confidence, emails, meetings) |
|
|
recommendation = generate_recommendation(stage, emails, meetings, risk) |
|
|
explanation = explain_score(lead_score, ai_score, confidence, risk, stage, close_gap, emails, meetings) |
|
|
|
|
|
log_submission({ |
|
|
"amount": amount, "stage": stage, "industry": industry, |
|
|
"emails": emails, "meetings": meetings, "gap": close_gap, |
|
|
"lead_score": lead_score, "score": ai_score, "confidence": confidence, |
|
|
"risk": risk, "feedback": feedback |
|
|
}) |
|
|
|
|
|
sf_status = push_to_salesforce({ |
|
|
"amount": amount, "stage": stage, "industry": industry, |
|
|
"emails": emails, "meetings": meetings, "gap": close_gap, |
|
|
"feedback": feedback, "lead_score": lead_score, "score": ai_score, |
|
|
"confidence": confidence, "risk": risk, |
|
|
"recommendation": recommendation, "explanation": explanation |
|
|
}) |
|
|
|
|
|
return lead_score, ai_score, confidence, risk, recommendation, explanation, sf_status |
|
|
|
|
|
except Exception as e: |
|
|
return 0, 0, 0.0, "Error", "N/A", f"Error occurred: {str(e)}", f"β Error: {str(e)}" |
|
|
|
|
|
|
|
|
share_app = os.getenv("GRADIO_SHARE", "false").lower() == "true" |
|
|
|
|
|
|
|
|
with gr.Blocks(title="AI Deal Qualification Engine") as app: |
|
|
gr.Markdown("## π€ AI-Powered Deal Qualification Engine") |
|
|
gr.Markdown("Intelligently qualify sales deals using engagement and pipeline signals.") |
|
|
|
|
|
with gr.Tab("π₯ Input"): |
|
|
with gr.Row(): |
|
|
amount = gr.Number(label="π° Deal Amount (USD)", value=50000) |
|
|
stage = gr.Dropdown( |
|
|
["Prospecting", "Proposal/Price Quote", "Negotiation", "Closed Won", "Closed Lost"], |
|
|
label="π Stage" |
|
|
) |
|
|
industry = gr.Textbox(label="π Industry", value="Software") |
|
|
|
|
|
with gr.Row(): |
|
|
emails = gr.Number(label="βοΈ Emails (Last 7 Days)", value=3) |
|
|
meetings = gr.Number(label="π
Meetings (Last 30 Days)", value=2) |
|
|
close_gap = gr.Number(label="π Days Until Close", value=14) |
|
|
|
|
|
feedback = gr.Textbox(label="π¬ Optional: Rep Feedback", placeholder="Add any qualitative insights...") |
|
|
|
|
|
submit = gr.Button("π Run AI Scoring") |
|
|
|
|
|
with gr.Tab("π Results"): |
|
|
with gr.Accordion("AI Scoring Output", open=True): |
|
|
lead_score_out = gr.Number(label="π’ Lead Score", interactive=False) |
|
|
ai_score_out = gr.Number(label="π AI Score (0β100)", interactive=False) |
|
|
confidence_out = gr.Number(label="π Confidence", interactive=False) |
|
|
|
|
|
risk_out = gr.Textbox(label="β οΈ Risk Level", lines=1, interactive=False) |
|
|
reco_out = gr.Textbox(label="π‘ AI Recommendation", lines=2, interactive=False) |
|
|
explain_out = gr.Textbox(label="π§ Explanation", lines=5, interactive=False) |
|
|
|
|
|
status = gr.Markdown("") |
|
|
|
|
|
submit.click( |
|
|
fn=run_engine, |
|
|
inputs=[amount, stage, industry, emails, meetings, close_gap, feedback], |
|
|
outputs=[lead_score_out, ai_score_out, confidence_out, risk_out, reco_out, explain_out, status] |
|
|
) |
|
|
|
|
|
app.launch(share=share_app) |
|
|
|