Spaces:
Sleeping
Sleeping
Create lead_scoring_agent.py
Browse files- lead_scoring_agent.py +73 -0
lead_scoring_agent.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
lead_scoring_agent.py
|
| 3 |
+
---
|
| 4 |
+
ML/transformer+rule agent that ingests full lead/profile asset and outputs:
|
| 5 |
+
- intent_score: Float
|
| 6 |
+
- readiness: Category (e.g., 'ready', 'researching', 'cold')
|
| 7 |
+
- segment: Category (e.g., 'SMB', 'Enterprise', 'Freelancer')
|
| 8 |
+
- rationale/feature_importance: Dict[str, float] (reasons/score contributions)
|
| 9 |
+
|
| 10 |
+
Integrates with enrichment, research, outreach, analytics, validation agents. See README for pipeline, hooks, and agent call documentation.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
import numpy as np
|
| 15 |
+
|
| 16 |
+
class LeadScoringAgent:
|
| 17 |
+
def __init__(self, model_name: str = 'distilbert-base-uncased'):
|
| 18 |
+
self.classifier = pipeline('text-classification', model=model_name)
|
| 19 |
+
|
| 20 |
+
def score_lead(self, profile: dict) -> dict:
|
| 21 |
+
text = self.profile_to_text(profile)
|
| 22 |
+
model_result = self.classifier(text)[0]
|
| 23 |
+
intent_score = float(model_result['score'])
|
| 24 |
+
label = model_result['label'].lower()
|
| 25 |
+
|
| 26 |
+
# Rule-based logic (customize as needed)
|
| 27 |
+
readiness = 'ready' if intent_score > 0.8 and 'purchase' in label else 'researching' if intent_score > 0.5 else 'cold'
|
| 28 |
+
segment = self.segment_from_profile(profile)
|
| 29 |
+
feature_importance = self.estimate_feature_importance(profile)
|
| 30 |
+
rationale = {k: v for k, v in feature_importance.items() if abs(v) > 0.1}
|
| 31 |
+
|
| 32 |
+
return {
|
| 33 |
+
'intent_score': intent_score,
|
| 34 |
+
'readiness': readiness,
|
| 35 |
+
'segment': segment,
|
| 36 |
+
'rationale': rationale,
|
| 37 |
+
'feature_importance': feature_importance
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def profile_to_text(self, profile: dict) -> str:
|
| 41 |
+
# Join relevant fields into text
|
| 42 |
+
fields = ['bio', 'recent_activity', 'interests', 'company', 'role']
|
| 43 |
+
return ' '.join([str(profile.get(f, '')) for f in fields])
|
| 44 |
+
|
| 45 |
+
def segment_from_profile(self, profile: dict) -> str:
|
| 46 |
+
size = profile.get('company_size', '').lower()
|
| 47 |
+
if 'enterprise' in size or profile.get('employees', 0) > 1000:
|
| 48 |
+
return 'Enterprise'
|
| 49 |
+
elif 'small' in size or profile.get('employees', 0) < 50:
|
| 50 |
+
return 'SMB'
|
| 51 |
+
return 'Freelancer' if 'freelancer' in profile.get('role', '').lower() else 'SMB'
|
| 52 |
+
|
| 53 |
+
def estimate_feature_importance(self, profile: dict) -> dict:
|
| 54 |
+
imp = {}
|
| 55 |
+
imp['bio_len'] = len(profile.get('bio', '')) / 100.0
|
| 56 |
+
imp['activity'] = len(profile.get('recent_activity', '')) / 50.0
|
| 57 |
+
imp['interest_match'] = 1.0 if 'AI' in profile.get('interests', '') else 0.0
|
| 58 |
+
imp['company_rep'] = 1.0 if 'Google' in profile.get('company', '') else 0.2
|
| 59 |
+
return imp
|
| 60 |
+
|
| 61 |
+
# Example usage:
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
agent = LeadScoringAgent()
|
| 64 |
+
profile = {
|
| 65 |
+
'bio': 'Experienced AI practitioner looking to purchase cloud solutions.',
|
| 66 |
+
'recent_activity': 'Demo requested.',
|
| 67 |
+
'interests': 'AI, Machine Learning, Cloud',
|
| 68 |
+
'company': 'Google',
|
| 69 |
+
'role': 'Manager',
|
| 70 |
+
'company_size': 'enterprise',
|
| 71 |
+
'employees': 1200
|
| 72 |
+
}
|
| 73 |
+
print(agent.score_lead(profile))
|