Spaces:
Running
Running
| from typing import Dict, Any | |
| def analyze_context(message: str) -> Dict[str, Any]: | |
| # Text normalisieren und für Wort-Splitting vorbereiten | |
| text_raw = message.lower() | |
| # Satzzeichen entfernen für exaktes Word-Matching | |
| clean_text = text_raw | |
| for char in [".", ",", "!", "?", ";", ":", "-", "_", "(", ")", "[", "]", "\n", "\r"]: | |
| clean_text = clean_text.replace(char, " ") | |
| # In einzelne Wörter splitten, um Sub-String-Fehler (z.B. "ich" in "sicherlich") zu vermeiden | |
| words_set = set(clean_text.split()) | |
| scores = { | |
| "Business": 0.0, | |
| "Technik": 0.0, | |
| "Kreativ": 0.0, | |
| "Persönlich": 0.0 | |
| } | |
| business_words = [ | |
| "business", "umsatz", "kunden", "angebot", "markt", "preis", | |
| "verkauf", "agentur", "strategie", "monetarisierung", "framework" | |
| ] | |
| tech_words = [ | |
| "code", "api", "router", "fastapi", "gradio", "python", | |
| "github", "hugging face", "hf", "mcp", "server", "datenbank" | |
| ] | |
| creative_words = [ | |
| "bild", "video", "content", "design", "persona", "prompt", | |
| "reel", "shorts", "style", "look", "story" | |
| ] | |
| personal_words = [ | |
| "ich", "mein", "problem", "hilfe", "stress", "plan", | |
| "ziel", "entscheidung", "gedanke" | |
| ] | |
| # Abgleich auf echte Wort-Treffer oder exakte Phrasen | |
| for word in business_words: | |
| if word in words_set or (" " in word and word in text_raw): | |
| scores["Business"] += 1 | |
| for word in tech_words: | |
| if word in words_set or (" " in word and word in text_raw): | |
| scores["Technik"] += 1 | |
| for word in creative_words: | |
| if word in words_set or (" " in word and word in text_raw): | |
| scores["Kreativ"] += 1 | |
| for word in personal_words: | |
| if word in words_set: | |
| scores["Persönlich"] += 0.5 | |
| total = sum(scores.values()) or 1 | |
| distribution = { | |
| key: round(value / total, 2) | |
| for key, value in scores.items() | |
| } | |
| raw_primary = max(distribution, key=distribution.get) | |
| actual_confidence = distribution.get(raw_primary, 0.5) | |
| # Wenn der stärkste Intent unter 40% liegt, deklarieren wir es als gemischten Kontext | |
| if distribution[raw_primary] < 0.4: | |
| primary = "Mixed" | |
| else: | |
| primary = raw_primary | |
| intent = detect_intent(text_raw) | |
| complexity = detect_complexity(text_raw) | |
| return { | |
| "primary_context": { | |
| "type": primary, | |
| "confidence": actual_confidence if primary != "Mixed" else round(actual_confidence, 2) | |
| }, | |
| "context_distribution": distribution, | |
| "intent_layer": intent, | |
| "complexity_level": complexity, | |
| "routing_hint": build_routing_hint(primary, intent, complexity), | |
| "reasoning": "Kontext wurde anhand exakter Schlüsselwörter und Signalgruppen bestimmt." | |
| } | |
| def detect_intent(text: str) -> Dict[str, Any]: | |
| if any(w in text for w in ["bauen", "erstellen", "setup", "einbauen", "implementieren"]): | |
| return {"type": "Aufbau", "confidence": 0.85} | |
| if any(w in text for w in ["analysieren", "bewerten", "prüfen", "vergleich"]): | |
| return {"type": "Analyse", "confidence": 0.8} | |
| if any(w in text for w in ["idee", "konzept", "vision"]): | |
| return {"type": "Idee", "confidence": 0.75} | |
| if any(w in text for w in ["problem", "fehler", "geht nicht", "kaputt"]): | |
| return {"type": "Problem", "confidence": 0.85} | |
| if any(w in text for w in ["plan", "nächste schritte", "roadmap"]): | |
| return {"type": "Planung", "confidence": 0.8} | |
| return {"type": "Umsetzung", "confidence": 0.6} | |
| def detect_complexity(text: str) -> str: | |
| length = len(text) | |
| if length > 1500: | |
| return "high" | |
| if length > 500: | |
| return "medium" | |
| return "low" | |
| def build_routing_hint(primary: str, intent: Dict[str, Any], complexity: str) -> Dict[str, Any]: | |
| if primary == "Technik": | |
| target = "DeepSeek" | |
| elif primary == "Business": | |
| target = "Hermes" | |
| elif primary == "Kreativ": | |
| target = "JoyAI" | |
| elif primary == "Persönlich": | |
| target = "Hermes" | |
| else: | |
| target = "Orchestrator" | |
| priority = "high" if complexity == "high" else "medium" | |
| return { | |
| "target_system": target, | |
| "priority": priority, | |
| "intent": intent["type"] | |
| } | |