Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| from huggingface_hub import InferenceClient | |
| # Load token and model - DeepSeek models for dialogue | |
| HF_TOKEN = os.getenv("tomoniaccess") | |
| # DeepSeek model options (optimized for dialogue): | |
| model_name = "deepseek-ai/deepseek-llm-7b-chat" # Primary choice - optimized for chat | |
| # model_name = "deepseek-ai/deepseek-coder-7b-instruct" # Alternative with good instruction following | |
| # model_name = "deepseek-ai/deepseek-llm-67b-chat" # Larger model (if available) | |
| client = InferenceClient( | |
| model=model_name, | |
| token=HF_TOKEN | |
| ) | |
| def query_deepseek(messages, max_tokens=200, temperature=1.0, top_p=0.9): | |
| """Query DeepSeek model via Hugging Face InferenceClient""" | |
| try: | |
| # Try chat completion first | |
| response = client.chat_completion( | |
| messages=messages, | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| stream=False | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as chat_error: | |
| print(f"Chat completion failed: {chat_error}") | |
| try: | |
| # Fallback to text generation if chat completion fails | |
| # Convert messages to single prompt with DeepSeek format | |
| prompt = "" | |
| for msg in messages: | |
| if msg["role"] == "system": | |
| prompt += f"System: {msg['content']}\n\n" | |
| elif msg["role"] == "user": | |
| prompt += f"User: {msg['content']}\n" | |
| elif msg["role"] == "assistant": | |
| prompt += f"Assistant: {msg['content']}\n" | |
| prompt += "Assistant: " | |
| response = client.text_generation( | |
| prompt=prompt, | |
| max_new_tokens=max_tokens, | |
| temperature=temperature, | |
| top_p=top_p, | |
| do_sample=True, | |
| return_full_text=False | |
| ) | |
| return response | |
| except Exception as text_error: | |
| print(f"Text generation also failed: {text_error}") | |
| raise Exception(f"Both methods failed - Chat: {str(chat_error)}, Text: {str(text_error)}") | |
| def enhanced_chat_response(user_input, max_tokens, temperature, top_p): | |
| if not user_input.strip(): | |
| return "", "*Bitte gib eine Nachricht ein.*", "" | |
| # Optimized system prompt for DeepSeek (supports German well) | |
| system_prompt = { | |
| "role": "system", | |
| "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." | |
| ) | |
| } | |
| # Keep last 8 messages for context (DeepSeek handles context well) | |
| messages = [system_prompt] + conversation_history[-8:] | |
| current_message = {"role": "user", "content": user_input} | |
| messages.append(current_message) | |
| try: | |
| response_text = query_deepseek(messages, max_tokens, temperature, top_p) | |
| except Exception as e: | |
| print("API Error:", e) | |
| response_text = "*schweigt und starrt auf den Boden*" | |
| conversation_history.append(current_message) | |
| conversation_history.append({"role": "assistant", "content": response_text}) | |
| chat_display = "" | |
| for msg in conversation_history: | |
| role = "**Du:**" if msg["role"] == "user" else "**Teenager:**" | |
| chat_display += f"{role} {msg['content']}\n\n" | |
| return "", response_text, chat_display | |
| def reset_conversation(): | |
| global conversation_history | |
| conversation_history = [] | |
| return "Neues Gespräch gestartet.", "" | |
| def test_api_connection(): | |
| """Test API connection with multiple fallback methods""" | |
| try: | |
| # Test 1: Simple chat completion | |
| test_messages = [ | |
| {"role": "system", "content": "Du bist ein hilfsbereit Assistent und antwortest auf Deutsch."}, | |
| {"role": "user", "content": "Hallo"} | |
| ] | |
| response = query_deepseek(test_messages, max_tokens=20) | |
| return f"✅ API Verbindung erfolgreich: {response[:50]}..." | |
| except Exception as e: | |
| # Test 2: Try direct text generation | |
| try: | |
| simple_response = client.text_generation( | |
| prompt="Hallo, wie geht es dir?", | |
| max_new_tokens=10, | |
| do_sample=False, | |
| return_full_text=False | |
| ) | |
| return f"✅ API Verbindung (Text Generation): {simple_response[:50]}..." | |
| except Exception as e2: | |
| # Test 3: Check if model exists | |
| try: | |
| # Try to get model info | |
| model_info = f"Model: {model_name}" | |
| return f"❌ API Errors - Chat: {str(e)[:100]}... | Text: {str(e2)[:100]}... | {model_info}" | |
| except Exception as e3: | |
| return f"❌ Vollständiger API Fehler: {str(e)[:200]}..." | |
| # Initialize conversation history | |
| conversation_history = [] | |
| # UI | |
| with gr.Blocks(title="DeepSeek Depression Training Simulator") as demo: | |
| gr.Markdown("## 🧠 Depression Training Simulator (DeepSeek-7B-Chat)") | |
| gr.Markdown("**Übe realistische Gespräche mit einem 16-jährigen Teenager mit Depressionen.**") | |
| gr.Markdown("*Powered by DeepSeek-LLM-7B-Chat - Optimiert für natürliche Dialoge*") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### ⚙️ Einstellungen") | |
| max_tokens = gr.Slider(50, 300, value=150, step=10, label="Max. Antwortlänge") | |
| temperature = gr.Slider(0.1, 1.5, value=0.9, step=0.1, label="Kreativität (Temperature)") | |
| top_p = gr.Slider(0.1, 1.0, value=0.85, step=0.05, label="Top-p (Fokus)") | |
| gr.Markdown("### 🔧 API Status") | |
| api_status = gr.Textbox(label="Status", value="") | |
| api_test_btn = gr.Button("API testen") | |
| gr.Markdown("### 🔄 Aktionen") | |
| reset_btn = gr.Button("Neues Gespräch") | |
| gr.Markdown("### 📋 Setup & Troubleshooting") | |
| gr.Markdown(""" | |
| **Benötigt:** | |
| - `tomoniaccess` Umgebungsvariable mit HF Token | |
| - `pip install huggingface_hub gradio` | |
| **DeepSeek Info:** | |
| - Optimiert für Dialoge und Konversationen | |
| - 7B Parameter, sehr effizient | |
| - Modell: `deepseek-ai/deepseek-llm-7b-chat` | |
| - Starke multilinguale Fähigkeiten (DE/EN) | |
| **Bei API Fehlern:** | |
| 1. Token prüfen (muss Pro/Enterprise sein) | |
| 2. Modell verfügbar? → [HF Model Card](https://huggingface.co/deepseek-ai/deepseek-llm-7b-chat) | |
| 3. Alternative: `deepseek-ai/deepseek-coder-7b-instruct` | |
| 4. Größere Version: `deepseek-ai/deepseek-llm-67b-chat` | |
| **DeepSeek Vorteile:** | |
| - Bessere Dialogqualität | |
| - Konsistente Rollenausführung | |
| - Natürlichere Antworten | |
| """) | |
| with gr.Column(scale=2): | |
| gr.Markdown("### 💬 Gespräch") | |
| user_input = gr.Textbox( | |
| label="Deine Nachricht", | |
| placeholder="Hallo, wie geht es dir heute?", | |
| lines=2 | |
| ) | |
| send_btn = gr.Button("📨 Senden") | |
| bot_response = gr.Textbox( | |
| label="Antwort", | |
| value="", | |
| lines=3 | |
| ) | |
| chat_history = gr.Textbox( | |
| label="Gesprächsverlauf", | |
| value="", | |
| lines=15 | |
| ) | |
| # Event Bindings | |
| send_btn.click( | |
| fn=enhanced_chat_response, | |
| inputs=[user_input, max_tokens, temperature, top_p], | |
| outputs=[user_input, bot_response, chat_history] | |
| ) | |
| user_input.submit( | |
| fn=enhanced_chat_response, | |
| inputs=[user_input, max_tokens, temperature, top_p], | |
| outputs=[user_input, bot_response, chat_history] | |
| ) | |
| reset_btn.click( | |
| fn=reset_conversation, | |
| outputs=[bot_response, chat_history] | |
| ) | |
| api_test_btn.click( | |
| fn=test_api_connection, | |
| outputs=[api_status] | |
| ) | |
| if __name__ == "__main__": | |
| print("🚀 DeepSeek Depression Training Simulator") | |
| print(f"📊 Model: {model_name}") | |
| if not HF_TOKEN: | |
| print("❌ FEHLER: tomoniaccess Umgebungsvariable ist nicht gesetzt!") | |
| print(" Bitte setze deinen Hugging Face Token als 'tomoniaccess' Umgebungsvariable.") | |
| else: | |
| print("✅ Hugging Face API Token gefunden") | |
| print("\n📦 Benötigte Pakete:") | |
| print("pip install huggingface_hub gradio") | |
| print("\n🤖 DeepSeek: Hochperformantes Modell für natürliche Dialoge") | |
| print(" - Bessere Konsistenz in Rollenspielen") | |
| print(" - Verbesserte multilinguale Fähigkeiten") | |
| print(" - Optimiert für Konversations-KI") | |
| demo.launch(share=False) |