Spaces:
Sleeping
Sleeping
Download backend/app/services/llm/analysis.py from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 3.63 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/app/services/llm/analysis.py
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/backend/app/services/llm/analysis.py
-
curl -L -o analysis.py https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/app/services/llm/analysis.py
3.63 kB
| """ | |
| Lead Analysis Module | |
| Functions for analyzing leads: psychotype detection, heat score calculation. | |
| """ | |
| import logging | |
| from typing import List, Dict, Any, Optional | |
| logger = logging.getLogger(__name__) | |
| # ============================================================================= | |
| # Psychotype Analysis Prompts | |
| # ============================================================================= | |
| PSYCHOTYPE_PROMPT = """Analyze these user messages and determine their DISC personality type. | |
| Messages: | |
| {messages} | |
| Rules: | |
| 1. Even a single "Hi" or greeting can indicate a type (usually Influential or Steady) | |
| 2. Short direct messages = Dominant | |
| 3. Friendly, emoji-filled = Influential | |
| 4. Polite, patient = Steady | |
| 5. Detailed, analytical = Conscientious | |
| Return ONLY one of: dominant, influential, steady, conscientious | |
| If truly unclear, return "steady" as the safest default. | |
| """ | |
| HEAT_SCORE_PROMPT = """Calculate lead heat score (0-100) based on: | |
| Conversation: | |
| {conversation} | |
| BANT Data: | |
| - Budget: {budget} | |
| - Authority: {authority} | |
| - Need: {need} | |
| - Timeline: {timeline} | |
| Scoring guide: | |
| - 80-100: Ready to buy, all BANT clear | |
| - 60-79: High interest, most BANT clear | |
| - 40-59: Moderate interest, exploring | |
| - 20-39: Low interest, just browsing | |
| - 0-19: Not qualified, spam, or hostile | |
| Return ONLY a number from 0 to 100. | |
| """ | |
| # ============================================================================= | |
| # Analysis Functions | |
| # ============================================================================= | |
| async def analyze_psychotype_impl(client, messages: List[str]) -> str: | |
| """ | |
| Detect user's DISC personality type from messages. | |
| Returns: dominant, influential, steady, or conscientious | |
| """ | |
| if not messages: | |
| return "steady" # Default for empty input | |
| messages_text = "\n".join(f"- {msg}" for msg in messages[-5:]) # Last 5 | |
| prompt = PSYCHOTYPE_PROMPT.format(messages=messages_text) | |
| try: | |
| response = await client.generate_response( | |
| prompt=prompt, | |
| temperature=0.1 # Low temperature for consistency | |
| ) | |
| result = response["text"].strip().lower() | |
| # Validate result | |
| valid_types = ["dominant", "influential", "steady", "conscientious"] | |
| for valid_type in valid_types: | |
| if valid_type in result: | |
| return valid_type | |
| return "steady" # Default if parsing fails | |
| except Exception as e: | |
| logger.error(f"Psychotype analysis failed: {e}") | |
| return "steady" | |
| async def calculate_heat_score_impl( | |
| client, | |
| conversation_text: str, | |
| bant_data: Dict[str, Optional[str]] | |
| ) -> int: | |
| """ | |
| Calculate lead heat score (0-100). | |
| """ | |
| prompt = HEAT_SCORE_PROMPT.format( | |
| conversation=conversation_text[:2000], # Limit length | |
| budget=bant_data.get("budget") or "Unknown", | |
| authority=bant_data.get("authority") or "Unknown", | |
| need=bant_data.get("need") or "Unknown", | |
| timeline=bant_data.get("timeline") or "Unknown" | |
| ) | |
| try: | |
| response = await client.generate_response( | |
| prompt=prompt, | |
| temperature=0.1 | |
| ) | |
| result = response["text"].strip() | |
| # Extract number | |
| import re | |
| numbers = re.findall(r'\d+', result) | |
| if numbers: | |
| score = int(numbers[0]) | |
| return min(100, max(0, score)) # Clamp to 0-100 | |
| return 30 # Default moderate score | |
| except Exception as e: | |
| logger.error(f"Heat score calculation failed: {e}") | |
| return 30 | |