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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
import random
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# OpenAI GPT-4 Konfiguration
|
| 9 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
current_model = "gpt-4"
|
| 11 |
+
|
| 12 |
+
# OpenAI Client initialisieren
|
| 13 |
+
openai.api_key = OPENAI_API_KEY
|
| 14 |
+
|
| 15 |
+
conversation_history = []
|
| 16 |
+
|
| 17 |
+
def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
|
| 18 |
+
print("inside enhanced_chat_response")
|
| 19 |
+
if not user_input.strip():
|
| 20 |
+
return "", "Bitte gib eine Nachricht ein.", "", ""
|
| 21 |
+
|
| 22 |
+
# Einfacherer, direkterer System Prompt
|
| 23 |
+
system_prompt = {
|
| 24 |
+
"role": "system",
|
| 25 |
+
"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."
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Baue Conversation History auf
|
| 29 |
+
messages = [system_prompt]
|
| 30 |
+
|
| 31 |
+
# Füge vorherige Nachrichten hinzu (falls vorhanden)
|
| 32 |
+
for msg in conversation_history[-6:]: # Letzte 6 Nachrichten
|
| 33 |
+
messages.append(msg)
|
| 34 |
+
|
| 35 |
+
# Aktuelle Nachricht hinzufügen
|
| 36 |
+
current_message = {"role": "user", "content": user_input}
|
| 37 |
+
messages.append(current_message)
|
| 38 |
+
|
| 39 |
+
print("Messages sent to API:", messages)
|
| 40 |
+
|
| 41 |
+
# Test-Nachricht nur für Terminal-Output (nicht im Chat sichtbar)
|
| 42 |
+
test_message = {"role": "user", "content": "Was ist deine Rolle?"}
|
| 43 |
+
messages_test = [system_prompt, test_message]
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
# Rollentest - nur für Debug-Zwecke im Terminal
|
| 47 |
+
test_response = openai.ChatCompletion.create(
|
| 48 |
+
model=current_model,
|
| 49 |
+
messages=messages_test,
|
| 50 |
+
max_tokens=200,
|
| 51 |
+
temperature=0.8
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
test_content = test_response['choices'][0]['message']['content']
|
| 55 |
+
print("🔍 DEBUG - Rollentest Antwort:", test_content)
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"🔍 DEBUG - Test API Error: {e}")
|
| 59 |
+
|
| 60 |
+
response_text = ""
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
# Hauptanfrage - normale Chat-Antwort
|
| 64 |
+
response = openai.ChatCompletion.create(
|
| 65 |
+
model=current_model,
|
| 66 |
+
messages=messages,
|
| 67 |
+
max_tokens=min(max_tokens, 500),
|
| 68 |
+
temperature=temperature,
|
| 69 |
+
top_p=top_p,
|
| 70 |
+
frequency_penalty=0.1,
|
| 71 |
+
presence_penalty=0.1
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Antwort extrahieren
|
| 75 |
+
response_text = response['choices'][0]['message']['content']
|
| 76 |
+
|
| 77 |
+
# Bereinige die Antwort von Meta-Kommentaren
|
| 78 |
+
#response_text = clean_response(response_text)
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print(f"API Error: {e}")
|
| 82 |
+
response_text = f"*schweigt und starrt auf den Boden*"
|
| 83 |
+
|
| 84 |
+
print("Antwort des Modells:", response_text)
|
| 85 |
+
|
| 86 |
+
# Zur Conversation History hinzufügen
|
| 87 |
+
conversation_history.append(current_message)
|
| 88 |
+
conversation_history.append({"role": "assistant", "content": response_text})
|
| 89 |
+
|
| 90 |
+
# Chat Display erstellen
|
| 91 |
+
chat_display = ""
|
| 92 |
+
for i, msg in enumerate(conversation_history):
|
| 93 |
+
if msg["role"] == "user":
|
| 94 |
+
chat_display += f"**Du:** {msg['content']}\n"
|
| 95 |
+
elif msg["role"] == "assistant":
|
| 96 |
+
chat_display += f"**Teenager:** {msg['content']}\n\n"
|
| 97 |
+
|
| 98 |
+
return "", response_text, chat_display
|
| 99 |
+
|
| 100 |
+
def clean_response(text):
|
| 101 |
+
"""Entferne Meta-Kommentare und KI-Referenzen"""
|
| 102 |
+
# Entferne häufige KI/Rollen-Referenzen
|
| 103 |
+
bad_phrases = [
|
| 104 |
+
"als ki", "als ai", "rolle spiel", "ich spiele",
|
| 105 |
+
"meine rolle", "simulator", "programm", "model",
|
| 106 |
+
"assistent", "helfen zu können", "gerne helfen",
|
| 107 |
+
"vielen dank für", "leidenschaft ist", "glücklich ihnen zu helfen",
|
| 108 |
+
"künstliche intelligenz", "chatbot", "openai"
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
text_lower = text.lower()
|
| 112 |
+
for phrase in bad_phrases:
|
| 113 |
+
if phrase in text_lower:
|
| 114 |
+
# Wenn Meta-Kommentare gefunden werden, ersetze mit authentischer Antwort
|
| 115 |
+
alternatives = [
|
| 116 |
+
"Geht so... nichts besonderes halt",
|
| 117 |
+
"Bin müde, mehr gibt's nicht zu sagen",
|
| 118 |
+
"Keine Ahnung warum du fragst",
|
| 119 |
+
"Ist halt so wie immer - scheiße",
|
| 120 |
+
"Was soll ich dazu sagen..."
|
| 121 |
+
]
|
| 122 |
+
return random.choice(alternatives)
|
| 123 |
+
|
| 124 |
+
# Entferne höfliche Floskeln
|
| 125 |
+
text = text.replace("Vielen Dank", "").strip()
|
| 126 |
+
text = text.replace("Gerne", "").strip()
|
| 127 |
+
|
| 128 |
+
return text.strip()
|
| 129 |
+
|
| 130 |
+
def reset_conversation():
|
| 131 |
+
global conversation_history
|
| 132 |
+
conversation_history = []
|
| 133 |
+
return "Neues Gespräch gestartet.", "", ""
|
| 134 |
+
|
| 135 |
+
def test_api_connection():
|
| 136 |
+
"""Teste die OpenAI API Verbindung"""
|
| 137 |
+
try:
|
| 138 |
+
response = openai.ChatCompletion.create(
|
| 139 |
+
model=current_model,
|
| 140 |
+
messages=[{"role": "user", "content": "Hi"}],
|
| 141 |
+
max_tokens=10
|
| 142 |
+
)
|
| 143 |
+
return "✅ API Verbindung erfolgreich"
|
| 144 |
+
except Exception as e:
|
| 145 |
+
return f"❌ API Error: {str(e)}"
|
| 146 |
+
|
| 147 |
+
# UI mit Terminal-Debug Information
|
| 148 |
+
with gr.Blocks(title="Depression Training Simulator", theme=gr.themes.Soft()) as demo:
|
| 149 |
+
gr.Markdown("# 🧠 Depression Training Simulator (GPT-4)")
|
| 150 |
+
gr.Markdown("**Übe realistische Gespräche mit einem 16-jährigen Teenager mit Depressionen**")
|
| 151 |
+
gr.Markdown("🔍 *Debug-Informationen werden nur im Terminal angezeigt*")
|
| 152 |
+
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column(scale=1):
|
| 155 |
+
# Parameter
|
| 156 |
+
gr.Markdown("### ⚙️ Einstellungen")
|
| 157 |
+
max_tokens = gr.Slider(50, 200, value=100, step=10, label="Max. Antwortlänge")
|
| 158 |
+
temperature = gr.Slider(0.7, 1.3, value=1.0, step=0.1, label="Kreativität/Variabilität")
|
| 159 |
+
top_p = gr.Slider(0.8, 1.0, value=0.9, step=0.05, label="Fokus")
|
| 160 |
+
|
| 161 |
+
# API Test
|
| 162 |
+
gr.Markdown("### 🔧 API Status")
|
| 163 |
+
api_test_btn = gr.Button("API testen", variant="secondary")
|
| 164 |
+
api_status = gr.Textbox(label="Status", interactive=False)
|
| 165 |
+
|
| 166 |
+
# Actions
|
| 167 |
+
gr.Markdown("### 🔄 Teste: Hallo, wie geht es dir?")
|
| 168 |
+
gr.Markdown("### 🔄 Aktionen")
|
| 169 |
+
reset_btn = gr.Button("Neues Gespräch", variant="secondary")
|
| 170 |
+
|
| 171 |
+
with gr.Column(scale=2):
|
| 172 |
+
# Chat Interface
|
| 173 |
+
gr.Markdown("### 💬 Gespräch")
|
| 174 |
+
|
| 175 |
+
user_input = gr.Textbox(
|
| 176 |
+
label="Deine Nachricht",
|
| 177 |
+
placeholder="Hallo, wie geht es dir heute?",
|
| 178 |
+
lines=2
|
| 179 |
+
)
|
| 180 |
+
send_btn = gr.Button("📨 Senden", variant="primary")
|
| 181 |
+
|
| 182 |
+
bot_response = gr.Textbox(
|
| 183 |
+
label="Antwort",
|
| 184 |
+
interactive=False,
|
| 185 |
+
lines=3
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
chat_history = gr.Textbox(
|
| 189 |
+
label="Gesprächsverlauf",
|
| 190 |
+
interactive=False,
|
| 191 |
+
lines=15,
|
| 192 |
+
max_lines=20
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
# Event Bindings
|
| 196 |
+
send_btn.click(
|
| 197 |
+
fn=enhanced_chat_response,
|
| 198 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
| 199 |
+
outputs=[user_input, bot_response, chat_history]
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
user_input.submit(
|
| 203 |
+
fn=enhanced_chat_response,
|
| 204 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
| 205 |
+
outputs=[user_input, bot_response, chat_history]
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
reset_btn.click(
|
| 209 |
+
fn=reset_conversation,
|
| 210 |
+
outputs=[bot_response, chat_history]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
api_test_btn.click(
|
| 214 |
+
fn=test_api_connection,
|
| 215 |
+
outputs=[api_status]
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
if __name__ == "__main__":
|
| 219 |
+
# Überprüfe ob API Key gesetzt ist
|
| 220 |
+
if not OPENAI_API_KEY:
|
| 221 |
+
print("❌ FEHLER: OPENAI_API_KEY Umgebungsvariable ist nicht gesetzt!")
|
| 222 |
+
print("Setze sie mit: export OPENAI_API_KEY='dein-api-key-hier'")
|
| 223 |
+
else:
|
| 224 |
+
print("✅ OpenAI API Key gefunden")
|
| 225 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|