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