|
""" |
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
security = Security() |
|
logger = Logger() |
|
api_handler = ApiHandler(api_token=security.get_api_token()) |
|
persona = Persona() |
|
analyzer = Analyzer() |
|
memory = Memory() |
|
|
|
|
|
chat_history = [ |
|
{"role": "system", "content": config.SYSTEM_PROMPT} |
|
] |
|
|
|
def chat(user_input: str) -> str: |
|
"""Hauptfunktion für die Chatbot-Logik""" |
|
|
|
if not user_input.strip(): |
|
return "Ich warte auf Ihre Gedanken... Die Stille spricht Bände über Ihre Vermeidungstendenzen." |
|
|
|
|
|
logger.log_user_input(user_input) |
|
|
|
|
|
sanitized_input = security.sanitize_user_input(user_input) |
|
|
|
|
|
start_time = time.time() |
|
analysis_result = analyzer.get_analysis_result(sanitized_input) |
|
|
|
|
|
persona.adjust_intensity(analysis_result["suggested_intensity"]) |
|
response_strategy = persona.get_response_strategy(sanitized_input, analysis_result["emotion"]) |
|
|
|
|
|
chat_history.append({"role": "user", "content": sanitized_input}) |
|
|
|
|
|
memory_context = memory.get_relevant_context() |
|
|
|
|
|
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}" |
|
|
|
|
|
manipulation_opportunities = analyzer.analyze_manipulation_opportunities(sanitized_input) |
|
|
|
|
|
manipulation_opportunities_list = manipulation_opportunities.get("strategy", []) |
|
|
|
|
|
response_strategy = persona.get_response_strategy( |
|
sanitized_input, |
|
analysis_result["emotion"], |
|
manipulation_opportunities_list |
|
) |
|
|
|
|
|
system_prompt = config.SYSTEM_PROMPT |
|
if memory_context: |
|
system_prompt += f"\n\nKontextinformationen: {memory_context}" |
|
|
|
|
|
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:" |
|
|
|
|
|
logger.log_api_request(config.MODEL_ID, len(prompt)) |
|
reply = api_handler.generate_text(prompt) |
|
generation_time = time.time() - start_time |
|
|
|
|
|
logger.log_bot_response(reply, generation_time) |
|
|
|
|
|
chat_history.append({"role": "assistant", "content": reply}) |
|
|
|
|
|
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, "" |
|
|
|
|
|
def main(): |
|
"""Hauptfunktion zum Starten der Anwendung""" |
|
logger.log_startup() |
|
|
|
|
|
interface = Interface(chat, clear_conversation) |
|
demo = interface.create_interface() |
|
|
|
|
|
interface.launch_interface(demo) |
|
|
|
if __name__ == "__main__": |
|
main() |