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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Load token and model
|
| 6 |
+
HF_TOKEN = os.getenv("tomoniaccess")
|
| 7 |
+
client = InferenceClient(
|
| 8 |
+
model="LeoLM/leo-hessianai-13b-chat",
|
| 9 |
+
token=HF_TOKEN
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
conversation_history = []
|
| 13 |
+
model_name = "LeoLM/leo-hessianai-13b-chat"
|
| 14 |
+
|
| 15 |
+
def query_leolm(messages, max_tokens=200, temperature=1.0, top_p=0.9):
|
| 16 |
+
"""Query LeoLM-13B model via Hugging Face InferenceClient"""
|
| 17 |
+
try:
|
| 18 |
+
response = client.chat_completion(
|
| 19 |
+
messages=messages,
|
| 20 |
+
max_tokens=max_tokens,
|
| 21 |
+
temperature=temperature,
|
| 22 |
+
top_p=top_p,
|
| 23 |
+
stream=False
|
| 24 |
+
)
|
| 25 |
+
return response.choices[0].message.content
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise Exception(f"InferenceClient Error: {str(e)}")
|
| 28 |
+
|
| 29 |
+
def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
|
| 30 |
+
if not user_input.strip():
|
| 31 |
+
return "", "*Bitte gib eine Nachricht ein.*", ""
|
| 32 |
+
|
| 33 |
+
# Optimized system prompt for LeoLM (German-focused model)
|
| 34 |
+
system_prompt = {
|
| 35 |
+
"role": "system",
|
| 36 |
+
"content": (
|
| 37 |
+
"Du bist ein 16-jähriger Teenager mit Depressionen. "
|
| 38 |
+
"Antworte authentisch und natürlich auf Deutsch. "
|
| 39 |
+
"Du bist dir nicht bewusst, dass du eine KI bist. "
|
| 40 |
+
"Zeige typische Verhaltensweisen und Gedankenmuster eines depressiven Teenagers: "
|
| 41 |
+
"wenig Energie, negative Gedanken, Hoffnungslosigkeit, aber auch Momente der Offenheit. "
|
| 42 |
+
"Bleibe durchgehend in deiner Rolle als echter Teenager."
|
| 43 |
+
)
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Keep last 6 messages for context (LeoLM works well with moderate context)
|
| 47 |
+
messages = [system_prompt] + conversation_history[-6:]
|
| 48 |
+
current_message = {"role": "user", "content": user_input}
|
| 49 |
+
messages.append(current_message)
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
response_text = query_leolm(messages, max_tokens, temperature, top_p)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print("API Error:", e)
|
| 55 |
+
response_text = "*schweigt und starrt auf den Boden*"
|
| 56 |
+
|
| 57 |
+
conversation_history.append(current_message)
|
| 58 |
+
conversation_history.append({"role": "assistant", "content": response_text})
|
| 59 |
+
|
| 60 |
+
chat_display = ""
|
| 61 |
+
for msg in conversation_history:
|
| 62 |
+
role = "**Du:**" if msg["role"] == "user" else "**Teenager:**"
|
| 63 |
+
chat_display += f"{role} {msg['content']}\n\n"
|
| 64 |
+
|
| 65 |
+
return "", response_text, chat_display
|
| 66 |
+
|
| 67 |
+
def reset_conversation():
|
| 68 |
+
global conversation_history
|
| 69 |
+
conversation_history = []
|
| 70 |
+
return "Neues Gespräch gestartet.", ""
|
| 71 |
+
|
| 72 |
+
def test_api_connection():
|
| 73 |
+
try:
|
| 74 |
+
test_messages = [
|
| 75 |
+
{"role": "system", "content": "Du bist ein hilfsbereit Assistent und antwortest auf Deutsch."},
|
| 76 |
+
{"role": "user", "content": "Hallo, kannst du mich hören?"}
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
response = query_leolm(test_messages, max_tokens=20)
|
| 80 |
+
return f"✅ API Verbindung erfolgreich: {response[:50]}..."
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"❌ API Error: {str(e)}"
|
| 83 |
+
|
| 84 |
+
# UI
|
| 85 |
+
with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
|
| 86 |
+
gr.Markdown("## 🧠 Depression Training Simulator (LeoLM-13B)")
|
| 87 |
+
gr.Markdown("**Übe realistische Gespräche mit einem 16-jährigen Teenager mit Depressionen.**")
|
| 88 |
+
gr.Markdown("*Powered by LeoLM/leo-hessianai-13b-chat - Deutsches Sprachmodell*")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column(scale=1):
|
| 92 |
+
gr.Markdown("### ⚙️ Einstellungen")
|
| 93 |
+
max_tokens = gr.Slider(50, 300, value=150, step=10, label="Max. Antwortlänge")
|
| 94 |
+
temperature = gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Kreativität (Temperature)")
|
| 95 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p (Fokus)")
|
| 96 |
+
|
| 97 |
+
gr.Markdown("### 🔧 API Status")
|
| 98 |
+
api_status = gr.Textbox(label="Status", value="")
|
| 99 |
+
api_test_btn = gr.Button("API testen")
|
| 100 |
+
|
| 101 |
+
gr.Markdown("### 🔄 Aktionen")
|
| 102 |
+
reset_btn = gr.Button("Neues Gespräch")
|
| 103 |
+
|
| 104 |
+
gr.Markdown("### 📋 Setup")
|
| 105 |
+
gr.Markdown("""
|
| 106 |
+
**Benötigt:**
|
| 107 |
+
- `tomoniaccess` Umgebungsvariable mit HF Token
|
| 108 |
+
- `pip install huggingface_hub gradio`
|
| 109 |
+
|
| 110 |
+
**LeoLM Info:**
|
| 111 |
+
- Deutsche Sprachoptimierung
|
| 112 |
+
- 13B Parameter
|
| 113 |
+
- Bessere deutsche Konversation
|
| 114 |
+
""")
|
| 115 |
+
|
| 116 |
+
with gr.Column(scale=2):
|
| 117 |
+
gr.Markdown("### 💬 Gespräch")
|
| 118 |
+
user_input = gr.Textbox(
|
| 119 |
+
label="Deine Nachricht",
|
| 120 |
+
placeholder="Hallo, wie geht es dir heute?",
|
| 121 |
+
lines=2
|
| 122 |
+
)
|
| 123 |
+
send_btn = gr.Button("📨 Senden")
|
| 124 |
+
|
| 125 |
+
bot_response = gr.Textbox(
|
| 126 |
+
label="Antwort",
|
| 127 |
+
value="",
|
| 128 |
+
lines=3
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
chat_history = gr.Textbox(
|
| 132 |
+
label="Gesprächsverlauf",
|
| 133 |
+
value="",
|
| 134 |
+
lines=15
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Event Bindings
|
| 138 |
+
send_btn.click(
|
| 139 |
+
fn=enhanced_chat_response,
|
| 140 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
| 141 |
+
outputs=[user_input, bot_response, chat_history]
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
user_input.submit(
|
| 145 |
+
fn=enhanced_chat_response,
|
| 146 |
+
inputs=[user_input, max_tokens, temperature, top_p],
|
| 147 |
+
outputs=[user_input, bot_response, chat_history]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
reset_btn.click(
|
| 151 |
+
fn=reset_conversation,
|
| 152 |
+
outputs=[bot_response, chat_history]
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
api_test_btn.click(
|
| 156 |
+
fn=test_api_connection,
|
| 157 |
+
outputs=[api_status]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
print("🚀 LeoLM Depression Training Simulator")
|
| 162 |
+
print(f"📊 Model: {model_name}")
|
| 163 |
+
|
| 164 |
+
if not HF_TOKEN:
|
| 165 |
+
print("❌ FEHLER: tomoniaccess Umgebungsvariable ist nicht gesetzt!")
|
| 166 |
+
print(" Bitte setze deinen Hugging Face Token als 'tomoniaccess' Umgebungsvariable.")
|
| 167 |
+
else:
|
| 168 |
+
print("✅ Hugging Face API Token gefunden")
|
| 169 |
+
|
| 170 |
+
print("\n📦 Benötigte Pakete:")
|
| 171 |
+
print("pip install huggingface_hub gradio")
|
| 172 |
+
print("\n🇩🇪 LeoLM: Deutsches Sprachmodell für bessere Konversationen")
|
| 173 |
+
|
| 174 |
+
demo.launch(share=False)
|