File size: 6,976 Bytes
a871214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9069696
a871214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bcb10e
a871214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bcb10e
a871214
 
ca9fb9c
4fe4765
ca9fb9c
a871214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d89155
a871214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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)