import gradio as gr from huggingface_hub import InferenceClient import os # Deutsche LLM Konfiguration HF_TOKEN = os.getenv("tomoniaccess") current_model = "LeoLM/leo-hessianai-13b-chat" client = InferenceClient(model=current_model, token=HF_TOKEN) conversation_history = [] def enhanced_chat_response(user_input, max_tokens, temperature, top_p): if not user_input.strip(): return "", "Bitte gib eine Nachricht ein.", "", "" messages = [{"role": "user", "content": user_input}] response_text = "" try: for message in client.chat_completion( messages=messages, max_tokens=min(max_tokens, 100), stream=True, temperature=temperature, top_p=top_p ): token = message.choices[0].delta.content if token: response_text += token except Exception as e: print(f"API Error: {e}") # This will show in console print(f"Error type: {type(e)}") response_text = f"API Fehler: {str(e)}" # Show actual error to user response_text = response_text.strip() chat_display = f"**Du:** {user_input}\n**Assistant:** {response_text}\n\n" return "", response_text, chat_display, "" def reset_conversation(): return "Neues Gespräch gestartet.", "", "" 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 depressiven Jugendlichen und erhalte Feedback**") with gr.Row(): with gr.Column(scale=1): # Parameter gr.Markdown("### ⚙️ Einstellungen") max_tokens = gr.Slider(50, 150, value=80, step=10, label="Antwortlänge") temperature = gr.Slider(0.5, 1.2, value=0.9, step=0.1, label="Variabilität") top_p = gr.Slider(0.7, 1.0, value=0.95, step=0.05, label="Fokus") # Actions 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="Beginne das Gespräch...", 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=12 ) # Feedback Panel (empty, no persona feedback) with gr.Accordion("📈 Trainer-Feedback", open=True): feedback_display = gr.Markdown("Starte ein Gespräch, um Feedback zu erhalten.") # Event Bindings send_btn.click( fn=enhanced_chat_response, inputs=[user_input, max_tokens, temperature, top_p], outputs=[user_input, bot_response, chat_history, feedback_display] ) user_input.submit( fn=enhanced_chat_response, inputs=[user_input, max_tokens, temperature, top_p], outputs=[user_input, bot_response, chat_history, feedback_display] ) reset_btn.click( fn=reset_conversation, outputs=[bot_response, chat_history, feedback_display] ) if __name__ == "__main__": demo.launch()