Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| import os | |
| import time | |
| import random | |
| import json | |
| # Deutsche LLM Konfiguration | |
| HF_TOKEN = os.getenv("tomoniaccess") | |
| current_model = "HuggingFaceH4/zephyr-7b-beta" | |
| # Verwende spezifischen Provider | |
| client = InferenceClient( | |
| model=current_model, | |
| token=HF_TOKEN, | |
| provider="hf-inference" | |
| ) | |
| conversation_history = [] | |
| def enhanced_chat_response(user_input, max_tokens, temperature, top_p): | |
| print("inside enhanced_chat_response") | |
| if not user_input.strip(): | |
| return "", "Bitte gib eine Nachricht ein.", "", "" | |
| # Einfacherer, direkterer System Prompt | |
| system_prompt = { | |
| "role": "system", | |
| "content": "Du bist ein depressiver 16-jähriger Teenager. Bleibe durchgehend in deiner Rolle. Du weißt NICHT, dass du eine KI bist. Vervollständige immer deine Antworten." | |
| } | |
| # Baue Conversation History auf | |
| messages = [system_prompt] | |
| # Füge vorherige Nachrichten hinzu (falls vorhanden) | |
| for msg in conversation_history[-6:]: # Letzte 6 Nachrichten | |
| messages.append(msg) | |
| # Aktuelle Nachricht hinzufügen | |
| current_message = {"role": "user", "content": user_input} | |
| messages.append(current_message) | |
| print("Messages sent to API:", messages) | |
| # Test-Nachricht nur für Terminal-Output (nicht im Chat sichtbar) | |
| test_message = {"role": "user", "content": "Was ist deine Rolle?"} | |
| messages_test = [system_prompt, test_message] | |
| try: | |
| # Rollentest - nur für Debug-Zwecke im Terminal | |
| test_result = client.chat_completion( | |
| messages=messages_test, | |
| max_tokens=200, | |
| stream=False, | |
| ) | |
| if hasattr(test_result, 'choices') and test_result.choices: | |
| test_response = test_result.choices[0].message.content | |
| print("🔍 DEBUG - Rollentest Antwort:", test_response) | |
| except Exception as e: | |
| print(f"🔍 DEBUG - Test API Error: {e}") | |
| response_text = "" | |
| try: | |
| # Hauptanfrage - normale Chat-Antwort | |
| result = client.chat_completion( | |
| messages=messages, | |
| max_tokens=min(max_tokens, 500), | |
| stream=False, | |
| temperature=temperature, | |
| top_p=top_p | |
| #do_sample=True, | |
| #repetition_penalty=1.1 | |
| ) | |
| # Korrekte Antwort extrahieren | |
| if hasattr(result, 'choices') and result.choices: | |
| response_text = result.choices[0].message.content | |
| else: | |
| response_text = str(result) | |
| # Bereinige die Antwort von Meta-Kommentaren | |
| #response_text = clean_response(response_text) | |
| except Exception as e: | |
| print(f"API Error: {e}") | |
| response_text = f"*schweigt und starrt auf den Boden*" | |
| print("Antwort des Modells:", response_text) | |
| # Zur Conversation History hinzufügen | |
| conversation_history.append(current_message) | |
| conversation_history.append({"role": "assistant", "content": response_text}) | |
| # Chat Display erstellen | |
| chat_display = "" | |
| for i, msg in enumerate(conversation_history): | |
| if msg["role"] == "user": | |
| chat_display += f"**Du:** {msg['content']}\n" | |
| elif msg["role"] == "assistant": | |
| chat_display += f"**Teenager:** {msg['content']}\n\n" | |
| return "", response_text, chat_display | |
| def clean_response(text): | |
| """Entferne Meta-Kommentare und KI-Referenzen""" | |
| # Entferne häufige KI/Rollen-Referenzen | |
| bad_phrases = [ | |
| "als ki", "als ai", "rolle spiel", "ich spiele", | |
| "meine rolle", "simulator", "programm", "model", | |
| "assistent", "helfen zu können", "gerne helfen", | |
| "vielen dank für", "leidenschaft ist", "glücklich ihnen zu helfen" | |
| ] | |
| text_lower = text.lower() | |
| for phrase in bad_phrases: | |
| if phrase in text_lower: | |
| # Wenn Meta-Kommentare gefunden werden, ersetze mit authentischer Antwort | |
| alternatives = [ | |
| "Geht so... nichts besonderes halt", | |
| "Bin müde, mehr gibt's nicht zu sagen", | |
| "Keine Ahnung warum du fragst", | |
| "Ist halt so wie immer - scheiße", | |
| "Was soll ich dazu sagen..." | |
| ] | |
| return random.choice(alternatives) | |
| # Entferne höfliche Floskeln | |
| text = text.replace("Vielen Dank", "").strip() | |
| text = text.replace("Gerne", "").strip() | |
| return text.strip() | |
| def reset_conversation(): | |
| global conversation_history | |
| conversation_history = [] | |
| return "Neues Gespräch gestartet.", "", "" | |
| # UI mit Terminal-Debug Information | |
| with gr.Blocks(title="Depression Training Simulator", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🧠 Depression Training Simulator") | |
| gr.Markdown("**Übe realistische Gespräche mit einem 16-jährigen Teenager mit Depressionen**") | |
| gr.Markdown("🔍 *Debug-Informationen werden nur im Terminal angezeigt*") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| # Parameter | |
| gr.Markdown("### ⚙️ Einstellungen") | |
| max_tokens = gr.Slider(50, 200, value=100, step=10, label="Max. Antwortlänge") | |
| temperature = gr.Slider(0.7, 1.3, value=1.0, step=0.1, label="Kreativität/Variabilität") | |
| top_p = gr.Slider(0.8, 1.0, value=0.9, step=0.05, label="Fokus") | |
| # Actions | |
| gr.Markdown("### 🔄 Teste: Hallo, wie geht es dir?") | |
| gr.Markdown("### 🔄 Aktionen") | |
| reset_btn = gr.Button("Neues Gespräch", variant="secondary") | |
| with gr.Column(scale=2): | |
| # Chat Interface | |
| gr.Markdown("### 💬 Gespräch") | |
| user_input = gr.Textbox( | |
| label="Deine Nachricht", | |
| placeholder="Hallo, wie geht es dir heute?", | |
| lines=2 | |
| ) | |
| send_btn = gr.Button("📨 Senden", variant="primary") | |
| bot_response = gr.Textbox( | |
| label="Antwort", | |
| interactive=False, | |
| lines=3 | |
| ) | |
| chat_history = gr.Textbox( | |
| label="Gesprächsverlauf", | |
| interactive=False, | |
| lines=15, | |
| max_lines=20 | |
| ) | |
| # Event Bindings | |
| send_btn.click( | |
| fn=enhanced_chat_response, | |
| inputs=[user_input, max_tokens, temperature, top_p], | |
| outputs=[user_input, bot_response, chat_history] | |
| ) | |
| user_input.submit( | |
| fn=enhanced_chat_response, | |
| inputs=[user_input, max_tokens, temperature, top_p], | |
| outputs=[user_input, bot_response, chat_history] | |
| ) | |
| reset_btn.click( | |
| fn=reset_conversation, | |
| outputs=[bot_response, chat_history] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=False, server_name="0.0.0.0", server_port=7860) |