Spaces:
Sleeping
Sleeping
Fix: Add proper Gradio interface and error handling to app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,100 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Example: scoring a batch of leads for dashboard queue
|
| 8 |
-
# Replace with your lead/profile loading logic
|
| 9 |
def score_leads_batch(leads: list):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
results = []
|
| 11 |
for lead in leads:
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
return results
|
| 15 |
|
| 16 |
def filter_leads(leads, min_score=0.7, readiness='ready'):
|
|
|
|
| 17 |
scored = score_leads_batch(leads)
|
| 18 |
# Filter for dashboard UI/queue
|
| 19 |
-
return [l for l, s in zip(leads, scored) if s
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sys
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
|
| 5 |
+
# Try to import lead scoring agent with error handling
|
| 6 |
+
try:
|
| 7 |
+
from lead_scoring_agent import LeadScoringAgent
|
| 8 |
+
lead_scoring_agent = LeadScoringAgent()
|
| 9 |
+
AGENT_AVAILABLE = True
|
| 10 |
+
except Exception as e:
|
| 11 |
+
print(f"Warning: LeadScoringAgent not available: {e}")
|
| 12 |
+
AGENT_AVAILABLE = False
|
| 13 |
+
lead_scoring_agent = None
|
| 14 |
|
| 15 |
# Example: scoring a batch of leads for dashboard queue
|
|
|
|
| 16 |
def score_leads_batch(leads: list):
|
| 17 |
+
"""Score a batch of leads using the lead scoring agent."""
|
| 18 |
+
if not AGENT_AVAILABLE or lead_scoring_agent is None:
|
| 19 |
+
return [{"error": "LeadScoringAgent not available", "intent_score": 0, "readiness": "unknown"} for _ in leads]
|
| 20 |
+
|
| 21 |
results = []
|
| 22 |
for lead in leads:
|
| 23 |
+
try:
|
| 24 |
+
result = lead_scoring_agent.score_lead(lead)
|
| 25 |
+
results.append(result)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
results.append({"error": str(e), "intent_score": 0, "readiness": "error"})
|
| 28 |
return results
|
| 29 |
|
| 30 |
def filter_leads(leads, min_score=0.7, readiness='ready'):
|
| 31 |
+
"""Filter leads based on minimum score and readiness."""
|
| 32 |
scored = score_leads_batch(leads)
|
| 33 |
# Filter for dashboard UI/queue
|
| 34 |
+
return [l for l, s in zip(leads, scored) if s.get('intent_score', 0) >= min_score and s.get('readiness') == readiness]
|
| 35 |
|
| 36 |
+
# Simple demo function for Gradio interface
|
| 37 |
+
def demo_score_lead(lead_name: str, lead_company: str, lead_title: str) -> str:
|
| 38 |
+
"""Demo function to score a single lead."""
|
| 39 |
+
if not lead_name:
|
| 40 |
+
return "Please provide a lead name."
|
| 41 |
+
|
| 42 |
+
lead = {
|
| 43 |
+
"name": lead_name,
|
| 44 |
+
"company": lead_company,
|
| 45 |
+
"title": lead_title
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
if AGENT_AVAILABLE:
|
| 49 |
+
try:
|
| 50 |
+
result = lead_scoring_agent.score_lead(lead)
|
| 51 |
+
return f"""Lead Scoring Result:
|
| 52 |
+
- Intent Score: {result.get('intent_score', 'N/A')}
|
| 53 |
+
- Readiness: {result.get('readiness', 'N/A')}
|
| 54 |
+
- Details: {result}"""
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error scoring lead: {str(e)}"
|
| 57 |
+
else:
|
| 58 |
+
return f"""Lead Scoring Agent Demo (Agent Not Available)
|
| 59 |
|
| 60 |
+
Lead Information:
|
| 61 |
+
- Name: {lead_name}
|
| 62 |
+
- Company: {lead_company}
|
| 63 |
+
- Title: {lead_title}
|
| 64 |
+
|
| 65 |
+
Note: The actual LeadScoringAgent is not available in this environment.
|
| 66 |
+
This is a placeholder demonstration."""
|
| 67 |
+
|
| 68 |
+
# Create Gradio interface
|
| 69 |
+
with gr.Blocks(title="Lead Scoring Agent") as demo:
|
| 70 |
+
gr.Markdown("# Lead Scoring Agent")
|
| 71 |
+
gr.Markdown("""This application scores leads based on their information.
|
| 72 |
+
Enter lead details below to see the scoring results.""")
|
| 73 |
+
|
| 74 |
+
if not AGENT_AVAILABLE:
|
| 75 |
+
gr.Markdown("⚠️ **Note:** LeadScoringAgent module is not available. Running in demo mode.")
|
| 76 |
+
|
| 77 |
+
with gr.Row():
|
| 78 |
+
with gr.Column():
|
| 79 |
+
lead_name = gr.Textbox(label="Lead Name", placeholder="John Doe")
|
| 80 |
+
lead_company = gr.Textbox(label="Company", placeholder="Acme Corp")
|
| 81 |
+
lead_title = gr.Textbox(label="Job Title", placeholder="CTO")
|
| 82 |
+
submit_btn = gr.Button("Score Lead", variant="primary")
|
| 83 |
+
|
| 84 |
+
with gr.Column():
|
| 85 |
+
output = gr.Textbox(label="Scoring Result", lines=10)
|
| 86 |
+
|
| 87 |
+
submit_btn.click(fn=demo_score_lead, inputs=[lead_name, lead_company, lead_title], outputs=output)
|
| 88 |
+
|
| 89 |
+
gr.Markdown("""---
|
| 90 |
+
### Troubleshooting Guide
|
| 91 |
+
If you encounter errors:
|
| 92 |
+
1. Check that all required dependencies are installed (see requirements.txt)
|
| 93 |
+
2. Verify that the lead_scoring_agent module is available
|
| 94 |
+
3. Check container logs for detailed error messages
|
| 95 |
+
4. See README.md for complete troubleshooting documentation
|
| 96 |
+
""")
|
| 97 |
+
|
| 98 |
+
# Launch the interface
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
demo.launch()
|