Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Deutsche LLM Konfiguration
|
| 6 |
+
HF_TOKEN = os.getenv("tomoniaccess")
|
| 7 |
+
current_model = "dbmdz/german-gpt2"
|
| 8 |
+
client = InferenceClient(model=current_model, token=HF_TOKEN)
|
| 9 |
+
|
| 10 |
+
conversation_history = []
|
| 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 |
+
messages = [{"role": "user", "content": user_input}]
|
| 17 |
+
|
| 18 |
+
response_text = ""
|
| 19 |
+
try:
|
| 20 |
+
for message in client.chat_completion(
|
| 21 |
+
messages=messages,
|
| 22 |
+
max_tokens=min(max_tokens, 100),
|
| 23 |
+
stream=True,
|
| 24 |
+
temperature=temperature,
|
| 25 |
+
top_p=top_p
|
| 26 |
+
):
|
| 27 |
+
token = message.choices[0].delta.content
|
| 28 |
+
if token:
|
| 29 |
+
response_text += token
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
response_text = "Entschuldigung, da ist ein Fehler aufgetreten."
|
| 33 |
+
|
| 34 |
+
response_text = response_text.strip()
|
| 35 |
+
|
| 36 |
+
chat_display = f"**Du:** {user_input}\n**Assistant:** {response_text}\n\n"
|
| 37 |
+
|
| 38 |
+
return "", response_text, chat_display, ""
|
| 39 |
+
|
| 40 |
+
def reset_conversation():
|
| 41 |
+
return "Neues Gespräch gestartet.", "", ""
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(title="Depression Training Simulator", theme=gr.themes.Soft()) as demo:
|
| 44 |
+
gr.Markdown("# 🧠 Depression Training Simulator")
|
| 45 |
+
gr.Markdown("**Übe realistische Gespräche mit depressiven Jugendlichen und erhalte Feedback**")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
# Parameter
|
| 50 |
+
gr.Markdown("### ⚙️ Einstellungen")
|
| 51 |
+
max_tokens = gr.Slider(50, 150, value=80, step=10, label="Antwortlänge")
|
| 52 |
+
temperature = gr.Slider(0.5, 1.2, value=0.9, step=0.1, label="Variabilität")
|
| 53 |
+
top_p = gr.Slider(0.7, 1.0, value=0.95, step=0.05, label="Fokus")
|
| 54 |
+
|
| 55 |
+
# Actions
|
| 56 |
+
gr.Markdown("### 🔄 Aktionen")
|
| 57 |
+
reset_btn = gr.Button("Neues Gespräch", variant="secondary")
|
| 58 |
+
|
| 59 |
+
with gr.Column(scale=2):
|
| 60 |
+
# Chat Interface
|
| 61 |
+
gr.Markdown("### 💬 Gespräch")
|
| 62 |
+
|
| 63 |
+
user_input = gr.Textbox(
|
| 64 |
+
label="Deine Nachricht",
|
| 65 |
+
placeholder="Beginne das Gespräch...",
|
| 66 |
+
lines=2
|
| 67 |
+
)
|
| 68 |
+
send_btn = gr.Button("📨 Senden", variant="primary")
|
| 69 |
+
|
| 70 |
+
bot_response = gr.Textbox(
|
| 71 |
+
label="Antwort",
|
| 72 |
+
interactive=False,
|
| 73 |
+
lines=3
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
chat_history = gr.Textbox(
|
| 77 |
+
label="Gesprächsverlauf",
|
| 78 |
+
interactive=False,
|
| 79 |
+
lines=12
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Feedback Panel (empty, no persona feedback)
|
| 83 |
+
with gr.Accordion("📈 Trainer-Feedback", open=True):
|
| 84 |
+
feedback_display = gr.Markdown("Starte ein Gespräch, um Feedback zu erhalten.")
|
| 85 |
+
|
| 86 |
+
# Event Bindings
|
| 87 |
+
send_btn.click(
|
| 88 |
+
fn=enhanced_chat_response,
|
| 89 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
| 90 |
+
outputs=[user_input, bot_response, chat_history, feedback_display]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
user_input.submit(
|
| 94 |
+
fn=enhanced_chat_response,
|
| 95 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
| 96 |
+
outputs=[user_input, bot_response, chat_history, feedback_display]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
reset_btn.click(
|
| 100 |
+
fn=reset_conversation,
|
| 101 |
+
outputs=[bot_response, chat_history, feedback_display]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
demo.launch(share=True, debug=True)
|