Download app.py from Frajosgro/Dr_Frank: direct link, hf CLI and curl.
- Browser
- Download file 5.16 kB
-
https://huggingface.co/spaces/Frajosgro/Dr_Frank/resolve/main/app.py
- Command line
-
hf download hf://spaces/Frajosgro/Dr_Frank/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Frajosgro/Dr_Frank/resolve/main/app.py
5.16 kB
| """ | |
| app.py - Hauptanwendung für den Dr. Franz Psychochatbot (Integrierte Version) | |
| Diese Datei dient als Einstiegspunkt für den Chatbot und orchestriert alle Komponenten: | |
| - Initialisierung der Module | |
| - Aufbau des Gradio-Interfaces | |
| - Verarbeitung der Nutzereingaben | |
| - Generierung der Antworten via API | |
| """ | |
| import os | |
| import time | |
| from typing import List, Dict, Tuple, Optional, Any | |
| # Importieren der Konfiguration und Module | |
| import config | |
| from modules.persona import Persona | |
| from gradio import components | |
| from modules.analyzer import Analyzer | |
| from modules.memory import Memory | |
| from modules.interface import Interface | |
| from utils.api_handler import ApiHandler | |
| from utils.security import Security | |
| from utils.logger import Logger | |
| # Initialisierung der Komponenten | |
| security = Security() | |
| logger = Logger() | |
| api_handler = ApiHandler(api_token=security.get_api_token()) | |
| persona = Persona() | |
| analyzer = Analyzer() | |
| memory = Memory() | |
| # Globale Variablen | |
| chat_history = [ | |
| {"role": "system", "content": config.SYSTEM_PROMPT} | |
| ] | |
| def chat(user_input: str) -> str: | |
| """Hauptfunktion für die Chatbot-Logik""" | |
| # Leere Eingaben abfangen | |
| if not user_input.strip(): | |
| return "Ich warte auf Ihre Gedanken... Die Stille spricht Bände über Ihre Vermeidungstendenzen." | |
| # Logging der Nutzereingabe | |
| logger.log_user_input(user_input) | |
| # Sicherheitscheck und Bereinigung der Eingabe | |
| sanitized_input = security.sanitize_user_input(user_input) | |
| # Analyse der Nutzereingabe | |
| start_time = time.time() | |
| analysis_result = analyzer.get_analysis_result(sanitized_input) | |
| # Anpassung der Persönlichkeit basierend auf der Analyse | |
| persona.adjust_intensity(analysis_result["suggested_intensity"]) | |
| response_strategy = persona.get_response_strategy(sanitized_input, analysis_result["emotion"]) | |
| # Nutzereingabe zur Historie hinzufügen | |
| chat_history.append({"role": "user", "content": sanitized_input}) | |
| # Kontext aus dem Gedächtnis abrufen | |
| memory_context = memory.get_relevant_context() | |
| # Prompt mit Kontextlimitierung für die API | |
| system_prompt = config.SYSTEM_PROMPT | |
| if memory_context: | |
| system_prompt += f"\n\nKontextinformationen: {memory_context}" | |
| additional_context = persona.get_additional_context(response_strategy) | |
| if additional_context: | |
| system_prompt += f"\n\n{additional_context}" | |
| # Analyse der Manipulationschancen | |
| manipulation_opportunities = analyzer.analyze_manipulation_opportunities(sanitized_input) | |
| # Extrahiere die Manipulationsmöglichkeiten für die Persona | |
| manipulation_opportunities_list = manipulation_opportunities.get("strategy", []) | |
| # Bestimme die Antwortstrategie mit den Manipulationsmöglichkeiten | |
| response_strategy = persona.get_response_strategy( | |
| sanitized_input, | |
| analysis_result["emotion"], | |
| manipulation_opportunities_list | |
| ) | |
| # Prompt mit Kontextlimitierung für die API | |
| system_prompt = config.SYSTEM_PROMPT | |
| if memory_context: | |
| system_prompt += f"\n\nKontextinformationen: {memory_context}" | |
| # Hinzufügen der Manipulationschancen zum System-Prompt | |
| if manipulation_opportunities_list: | |
| system_prompt += f"\n\nManipulationschancen: {', '.join(manipulation_opportunities_list)}" | |
| system_prompt += f"\n\nIntensität: {manipulation_opportunities['intensity']}" | |
| system_prompt += f"\n\nVorgeschlagene Antwort: {manipulation_opportunities['suggested_response']}" | |
| prompt = system_prompt + "\n" + memory.format_for_prompt(config.MAX_HISTORY_LENGTH) + "\nDr. Franz:" | |
| # API-Anfrage senden und Antwort erhalten | |
| logger.log_api_request(config.MODEL_ID, len(prompt)) | |
| reply = api_handler.generate_text(prompt) | |
| generation_time = time.time() - start_time | |
| # Logging der Bot-Antwort | |
| logger.log_bot_response(reply, generation_time) | |
| # Antwort zur Historie hinzufügen | |
| chat_history.append({"role": "assistant", "content": reply}) | |
| # Austausch zum Gedächtnis hinzufügen | |
| memory.add_exchange(sanitized_input, reply, analysis_result) | |
| return reply | |
| def clear_conversation() -> Tuple[None, str]: | |
| """Setzt die Konversation zurück und bereitet eine neue Analyse vor""" | |
| global chat_history | |
| chat_history = [{"role": "system", "content": config.SYSTEM_PROMPT}] | |
| memory.conversation_history = [] | |
| memory.extracted_info = { | |
| "mentioned_people": set(), | |
| "mentioned_events": set(), | |
| "recurring_themes": {}, | |
| "emotional_patterns": [], | |
| "analyzed_symbols": {}, | |
| "transfer_patterns": [], | |
| "defense_mechanisms": {} | |
| } | |
| return None, "Die Konversation wurde zurückgesetzt. Sie können mit neuen Gedanken beginnen." | |
| return None, "" | |
| # Erstellen und Starten des Interfaces | |
| def main(): | |
| """Hauptfunktion zum Starten der Anwendung""" | |
| logger.log_startup() | |
| # Interface erstellen | |
| interface = Interface(chat, clear_conversation) | |
| demo = interface.create_interface() | |
| # Interface starten | |
| interface.launch_interface(demo) | |
| if __name__ == "__main__": | |
| main() |