Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,63 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
-
|
4 |
from simple_salesforce import Salesforce
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
from insights import explain_score
|
9 |
-
from logger import log_submission
|
10 |
-
|
11 |
-
# Load environment variables
|
12 |
-
load_dotenv()
|
13 |
|
14 |
-
# Salesforce credentials
|
15 |
sf_username = os.getenv("SF_USERNAME")
|
16 |
sf_password = os.getenv("SF_PASSWORD")
|
17 |
sf_security_token = os.getenv("SF_SECURITY_TOKEN")
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
password=sf_password,
|
24 |
-
security_token=sf_security_token,
|
25 |
-
domain=sf_domain
|
26 |
-
)
|
27 |
|
|
|
|
|
|
|
|
|
|
|
28 |
def push_to_salesforce(data: dict) -> str:
|
29 |
try:
|
30 |
response = sf.qualification_engine__c.create({
|
@@ -46,6 +79,7 @@ def push_to_salesforce(data: dict) -> str:
|
|
46 |
except Exception as e:
|
47 |
return f"β Salesforce Error: {str(e)}"
|
48 |
|
|
|
49 |
def run_engine(amount, stage, industry, emails, meetings, close_gap, feedback=""):
|
50 |
try:
|
51 |
lead_score = get_lead_score(stage, emails, meetings, close_gap, amount)
|
@@ -55,13 +89,6 @@ def run_engine(amount, stage, industry, emails, meetings, close_gap, feedback=""
|
|
55 |
recommendation = generate_recommendation(stage, emails, meetings, risk)
|
56 |
explanation = explain_score(lead_score, ai_score, confidence, risk, stage, close_gap, emails, meetings)
|
57 |
|
58 |
-
log_submission({
|
59 |
-
"amount": amount, "stage": stage, "industry": industry,
|
60 |
-
"emails": emails, "meetings": meetings, "gap": close_gap,
|
61 |
-
"lead_score": lead_score, "score": ai_score, "confidence": confidence,
|
62 |
-
"risk": risk, "feedback": feedback
|
63 |
-
})
|
64 |
-
|
65 |
sf_status = push_to_salesforce({
|
66 |
"amount": amount, "stage": stage, "industry": industry,
|
67 |
"emails": emails, "meetings": meetings, "gap": close_gap,
|
@@ -75,14 +102,11 @@ def run_engine(amount, stage, industry, emails, meetings, close_gap, feedback=""
|
|
75 |
except Exception as e:
|
76 |
return 0, 0, 0.0, "Error", "N/A", f"Error occurred: {str(e)}", f"β Error: {str(e)}"
|
77 |
|
78 |
-
# Read share flag from env
|
79 |
-
share_app = os.getenv("GRADIO_SHARE", "false").lower() == "true"
|
80 |
-
|
81 |
# Gradio UI
|
82 |
with gr.Blocks(title="AI Deal Qualification Engine") as app:
|
83 |
gr.Markdown("## π€ AI-Powered Deal Qualification Engine")
|
84 |
gr.Markdown("Intelligently qualify sales deals using engagement and pipeline signals.")
|
85 |
-
|
86 |
with gr.Tab("π₯ Input"):
|
87 |
with gr.Row():
|
88 |
amount = gr.Number(label="π° Deal Amount (USD)", value=50000)
|
@@ -98,7 +122,6 @@ with gr.Blocks(title="AI Deal Qualification Engine") as app:
|
|
98 |
close_gap = gr.Number(label="π Days Until Close", value=14)
|
99 |
|
100 |
feedback = gr.Textbox(label="π¬ Optional: Rep Feedback", placeholder="Add any qualitative insights...")
|
101 |
-
|
102 |
submit = gr.Button("π Run AI Scoring")
|
103 |
|
104 |
with gr.Tab("π Results"):
|
@@ -119,4 +142,4 @@ with gr.Blocks(title="AI Deal Qualification Engine") as app:
|
|
119 |
outputs=[lead_score_out, ai_score_out, confidence_out, risk_out, reco_out, explain_out, status]
|
120 |
)
|
121 |
|
122 |
-
app.launch(share=
|
|
|
1 |
import gradio as gr
|
2 |
+
import logging
|
3 |
import os
|
4 |
+
import numpy as np
|
5 |
from simple_salesforce import Salesforce
|
6 |
+
from dotenv import load_dotenv
|
7 |
|
8 |
+
# Load environment variables from .env file
|
9 |
+
load_dotenv() # Load the .env file
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Get the Salesforce credentials from environment variables
|
12 |
sf_username = os.getenv("SF_USERNAME")
|
13 |
sf_password = os.getenv("SF_PASSWORD")
|
14 |
sf_security_token = os.getenv("SF_SECURITY_TOKEN")
|
15 |
+
sf_instance_url = os.getenv("SF_INSTANCE_URL")
|
16 |
+
|
17 |
+
# Check if the environment variables are correctly set
|
18 |
+
if not sf_username or not sf_password or not sf_security_token or not sf_instance_url:
|
19 |
+
logger.error("β Salesforce credentials are missing from environment variables!")
|
20 |
+
raise ValueError("Salesforce credentials are not properly set.")
|
21 |
+
|
22 |
+
# Salesforce connection
|
23 |
+
try:
|
24 |
+
sf = Salesforce(
|
25 |
+
username=sf_username,
|
26 |
+
password=sf_password,
|
27 |
+
security_token=sf_security_token,
|
28 |
+
instance_url=sf_instance_url
|
29 |
+
)
|
30 |
+
logger.info("β
Connected to Salesforce")
|
31 |
+
except Exception as e:
|
32 |
+
logger.error(f"β Salesforce connection failed: {str(e)}")
|
33 |
+
raise
|
34 |
+
|
35 |
+
# AI functions (These should be implemented in your project)
|
36 |
+
def get_lead_score(stage, emails, meetings, close_gap, amount):
|
37 |
+
# Example logic for lead score calculation
|
38 |
+
return 0.8 * amount + 0.1 * emails + 0.1 * meetings
|
39 |
+
|
40 |
+
def calculate_score(lead_score, emails, meetings, close_gap, amount):
|
41 |
+
# Example AI score calculation
|
42 |
+
return lead_score * 0.5 + 0.3 * emails + 0.2 * meetings
|
43 |
+
|
44 |
+
def calculate_confidence(ai_score):
|
45 |
+
# Example confidence calculation
|
46 |
+
return ai_score * 100
|
47 |
+
|
48 |
+
def calculate_risk(ai_score, confidence, emails, meetings):
|
49 |
+
# Example risk calculation
|
50 |
+
return "Low" if confidence > 75 else "High"
|
51 |
|
52 |
+
def generate_recommendation(stage, emails, meetings, risk):
|
53 |
+
# Example recommendation generation
|
54 |
+
return "Proceed with caution" if risk == "High" else "Proceed"
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
def explain_score(lead_score, ai_score, confidence, risk, stage, close_gap, emails, meetings):
|
57 |
+
# Example explanation generation
|
58 |
+
return f"Lead score based on {emails} emails and {meetings} meetings is {lead_score}. AI score is {ai_score}, confidence is {confidence}%."
|
59 |
+
|
60 |
+
# --- Push to Salesforce ---
|
61 |
def push_to_salesforce(data: dict) -> str:
|
62 |
try:
|
63 |
response = sf.qualification_engine__c.create({
|
|
|
79 |
except Exception as e:
|
80 |
return f"β Salesforce Error: {str(e)}"
|
81 |
|
82 |
+
# --- Run Engine (Calculate and Push to Salesforce) ---
|
83 |
def run_engine(amount, stage, industry, emails, meetings, close_gap, feedback=""):
|
84 |
try:
|
85 |
lead_score = get_lead_score(stage, emails, meetings, close_gap, amount)
|
|
|
89 |
recommendation = generate_recommendation(stage, emails, meetings, risk)
|
90 |
explanation = explain_score(lead_score, ai_score, confidence, risk, stage, close_gap, emails, meetings)
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
sf_status = push_to_salesforce({
|
93 |
"amount": amount, "stage": stage, "industry": industry,
|
94 |
"emails": emails, "meetings": meetings, "gap": close_gap,
|
|
|
102 |
except Exception as e:
|
103 |
return 0, 0, 0.0, "Error", "N/A", f"Error occurred: {str(e)}", f"β Error: {str(e)}"
|
104 |
|
|
|
|
|
|
|
105 |
# Gradio UI
|
106 |
with gr.Blocks(title="AI Deal Qualification Engine") as app:
|
107 |
gr.Markdown("## π€ AI-Powered Deal Qualification Engine")
|
108 |
gr.Markdown("Intelligently qualify sales deals using engagement and pipeline signals.")
|
109 |
+
|
110 |
with gr.Tab("π₯ Input"):
|
111 |
with gr.Row():
|
112 |
amount = gr.Number(label="π° Deal Amount (USD)", value=50000)
|
|
|
122 |
close_gap = gr.Number(label="π Days Until Close", value=14)
|
123 |
|
124 |
feedback = gr.Textbox(label="π¬ Optional: Rep Feedback", placeholder="Add any qualitative insights...")
|
|
|
125 |
submit = gr.Button("π Run AI Scoring")
|
126 |
|
127 |
with gr.Tab("π Results"):
|
|
|
142 |
outputs=[lead_score_out, ai_score_out, confidence_out, risk_out, reco_out, explain_out, status]
|
143 |
)
|
144 |
|
145 |
+
app.launch(share=True)
|