Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,138 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
|
|
7 |
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
|
|
|
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
from openai import OpenAI
|
5 |
|
6 |
+
# Initialisiere OpenAI-Client mit API Key
|
7 |
+
client = OpenAI(api_key=os.getenv("openai"))
|
8 |
|
9 |
+
conversation_history = []
|
10 |
+
model_name = "gpt-4" #"gpt-3.5-turbo"
|
11 |
|
12 |
+
def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
|
13 |
+
if not user_input.strip():
|
14 |
+
return "", "*Bitte gib eine Nachricht ein.*", ""
|
15 |
|
16 |
+
system_prompt = {
|
17 |
+
"role": "system",
|
18 |
+
"content": (
|
19 |
+
"Du bist ein depressiver 16-jähriger Teenager. "
|
20 |
+
"Bleibe durchgehend in deiner Rolle. "
|
21 |
+
"Du weißt NICHT, dass du eine KI bist."
|
22 |
+
)
|
23 |
+
}
|
24 |
|
25 |
+
messages = [system_prompt] + conversation_history[-6:]
|
26 |
+
current_message = {"role": "user", "content": user_input}
|
27 |
+
messages.append(current_message)
|
28 |
|
29 |
+
try:
|
30 |
+
response = client.chat.completions.create(
|
31 |
+
model=model_name,
|
32 |
+
messages=messages,
|
33 |
+
max_tokens=min(max_tokens, 500),
|
34 |
+
temperature=temperature,
|
35 |
+
top_p=top_p,
|
36 |
+
frequency_penalty=0.1,
|
37 |
+
presence_penalty=0.1
|
38 |
+
)
|
39 |
+
response_text = response.choices[0].message.content
|
40 |
+
except Exception as e:
|
41 |
+
print("API Error:", e)
|
42 |
+
response_text = "*schweigt und starrt auf den Boden*"
|
43 |
+
|
44 |
+
conversation_history.append(current_message)
|
45 |
+
conversation_history.append({"role": "assistant", "content": response_text})
|
46 |
+
|
47 |
+
chat_display = ""
|
48 |
+
for msg in conversation_history:
|
49 |
+
role = "**Du:**" if msg["role"] == "user" else "**Teenager:**"
|
50 |
+
chat_display += f"{role} {msg['content']}\n\n"
|
51 |
+
|
52 |
+
return "", response_text, chat_display
|
53 |
+
|
54 |
+
def reset_conversation():
|
55 |
+
global conversation_history
|
56 |
+
conversation_history = []
|
57 |
+
return "Neues Gespräch gestartet.", ""
|
58 |
+
|
59 |
+
def test_api_connection():
|
60 |
+
try:
|
61 |
+
response = client.chat.completions.create(
|
62 |
+
model=model_name,
|
63 |
+
messages=[{"role": "user", "content": "Hi"}],
|
64 |
+
max_tokens=10
|
65 |
+
)
|
66 |
+
return "✅ API Verbindung erfolgreich"
|
67 |
+
except Exception as e:
|
68 |
+
return f"❌ API Error: {str(e)}"
|
69 |
+
|
70 |
+
# UI
|
71 |
+
with gr.Blocks() as demo:
|
72 |
+
gr.Markdown("## 🧠 Depression Training Simulator")
|
73 |
+
gr.Markdown("**Übe realistische Gespräche mit einem 16-jährigen Teenager mit Depressionen.**")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
gr.Markdown("### ⚙️ Einstellungen")
|
78 |
+
max_tokens = gr.Slider(50, 500, value=200, step=10, label="Max. Antwortlänge")
|
79 |
+
temperature = gr.Slider(0.7, 1.3, value=1.0, step=0.1, label="Kreativität (Temperature)")
|
80 |
+
top_p = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Top-p (Fokus)")
|
81 |
+
|
82 |
+
gr.Markdown("### 🔧 API Status")
|
83 |
+
api_status = gr.Textbox(label="Status", value="")
|
84 |
+
api_test_btn = gr.Button("API testen")
|
85 |
+
|
86 |
+
gr.Markdown("### 🔄 Aktionen")
|
87 |
+
reset_btn = gr.Button("Neues Gespräch")
|
88 |
+
|
89 |
+
with gr.Column(scale=2):
|
90 |
+
gr.Markdown("### 💬 Gespräch")
|
91 |
+
user_input = gr.Textbox(
|
92 |
+
label="Deine Nachricht",
|
93 |
+
placeholder="Hallo, wie geht es dir heute?",
|
94 |
+
lines=2
|
95 |
+
)
|
96 |
+
send_btn = gr.Button("📨 Senden")
|
97 |
+
|
98 |
+
bot_response = gr.Textbox(
|
99 |
+
label="Antwort",
|
100 |
+
value="",
|
101 |
+
lines=3
|
102 |
+
)
|
103 |
+
|
104 |
+
chat_history = gr.Textbox(
|
105 |
+
label="Gesprächsverlauf",
|
106 |
+
value="",
|
107 |
+
lines=15
|
108 |
+
)
|
109 |
+
|
110 |
+
# Event Bindings
|
111 |
+
send_btn.click(
|
112 |
+
fn=enhanced_chat_response,
|
113 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
114 |
+
outputs=[user_input, bot_response, chat_history]
|
115 |
+
)
|
116 |
+
|
117 |
+
user_input.submit(
|
118 |
+
fn=enhanced_chat_response,
|
119 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
120 |
+
outputs=[user_input, bot_response, chat_history]
|
121 |
+
)
|
122 |
+
|
123 |
+
reset_btn.click(
|
124 |
+
fn=reset_conversation,
|
125 |
+
outputs=[bot_response, chat_history]
|
126 |
+
)
|
127 |
+
|
128 |
+
api_test_btn.click(
|
129 |
+
fn=test_api_connection,
|
130 |
+
outputs=[api_status]
|
131 |
+
)
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
if not os.getenv("openai"):
|
135 |
+
print("❌ FEHLER: openai Umgebungsvariable ist nicht gesetzt!")
|
136 |
+
else:
|
137 |
+
print("✅ OpenAI API Key gefunden")
|
138 |
+
demo.launch(share=False)
|