ai-tomoni commited on
Commit
a292072
·
verified ·
1 Parent(s): b0d0d98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -108
app.py CHANGED
@@ -2,23 +2,25 @@ import gradio as gr
2
  import os
3
  from huggingface_hub import InferenceClient
4
 
5
- # Load token and model - Try different models if one fails
6
  HF_TOKEN = os.getenv("tomoniaccess")
7
 
8
- # Model options (uncomment one that works):
9
- model_name = "LeoLM/leo-hessianai-13b-chat" # Primary choice
 
 
10
  client = InferenceClient(
11
  model=model_name,
12
  token=HF_TOKEN
13
  )
14
 
15
- # Initialize conversation history as global variable
16
  conversation_history = []
17
 
18
  def query_leolm(messages, max_tokens=200, temperature=1.0, top_p=0.9):
19
- """Query LeoLM-13B model via Hugging Face InferenceClient"""
20
  try:
21
- # Try chat completion first
22
  response = client.chat_completion(
23
  messages=messages,
24
  max_tokens=max_tokens,
@@ -30,39 +32,38 @@ def query_leolm(messages, max_tokens=200, temperature=1.0, top_p=0.9):
30
  except Exception as chat_error:
31
  print(f"Chat completion failed: {chat_error}")
32
  try:
33
- # Fallback to text generation if chat completion fails
34
- # Convert messages to single prompt
35
  prompt = ""
36
  for msg in messages:
37
  if msg["role"] == "system":
38
- prompt += f"System: {msg['content']}\n\n"
39
  elif msg["role"] == "user":
40
- prompt += f"User: {msg['content']}\n"
41
  elif msg["role"] == "assistant":
42
- prompt += f"Assistant: {msg['content']}\n"
43
-
44
- prompt += "Assistant: "
45
-
46
  response = client.text_generation(
47
  prompt=prompt,
48
  max_new_tokens=max_tokens,
49
  temperature=temperature,
50
  top_p=top_p,
51
  do_sample=True,
 
 
52
  return_full_text=False
53
  )
54
- return response
55
  except Exception as text_error:
56
  print(f"Text generation also failed: {text_error}")
57
  raise Exception(f"Both methods failed - Chat: {str(chat_error)}, Text: {str(text_error)}")
58
 
59
  def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
60
- global conversation_history # Declare global variable
61
-
62
  if not user_input.strip():
63
  return "", "*Bitte gib eine Nachricht ein.*", ""
64
 
65
- # Optimized system prompt for LeoLM (German-focused model)
66
  system_prompt = {
67
  "role": "system",
68
  "content": (
@@ -73,7 +74,7 @@ def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
73
  )
74
  }
75
 
76
- # Keep last 6 messages for context (LeoLM works well with moderate context)
77
  messages = [system_prompt] + conversation_history[-6:]
78
  current_message = {"role": "user", "content": user_input}
79
  messages.append(current_message)
@@ -100,129 +101,64 @@ def reset_conversation():
100
  return "Neues Gespräch gestartet.", ""
101
 
102
  def test_api_connection():
103
- """Test API connection with multiple fallback methods"""
104
  try:
105
- # Test 1: Simple chat completion
106
  test_messages = [
107
- {"role": "system", "content": "Du bist ein hilfsbereit Assistent und antwortest auf Deutsch."},
108
  {"role": "user", "content": "Hallo"}
109
  ]
110
-
111
  response = query_leolm(test_messages, max_tokens=20)
112
  return f"✅ API Verbindung erfolgreich: {response[:50]}..."
113
  except Exception as e:
114
- # Test 2: Try direct text generation
115
  try:
116
  simple_response = client.text_generation(
117
  prompt="Hallo, wie geht es dir?",
118
  max_new_tokens=10,
119
- do_sample=False,
120
  return_full_text=False
121
  )
122
  return f"✅ API Verbindung (Text Generation): {simple_response[:50]}..."
123
  except Exception as e2:
124
- # Test 3: Check if model exists
125
- try:
126
- # Try to get model info
127
- model_info = f"Model: {model_name}"
128
- return f"❌ API Errors - Chat: {str(e)[:100]}... | Text: {str(e2)[:100]}... | {model_info}"
129
- except Exception as e3:
130
- return f"❌ Vollständiger API Fehler: {str(e)[:200]}..."
131
-
132
- # UI
133
  with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
134
  gr.Markdown("## 🧠 Depression Training Simulator (LeoLM-13B)")
135
- gr.Markdown("**Übe realistische Gespräche mit einem 16-jährigen Teenager mit Depressionen.**")
136
- gr.Markdown("*Powered by LeoLM/leo-hessianai-13b-chat - Deutsches Sprachmodell*")
137
 
138
  with gr.Row():
139
  with gr.Column(scale=1):
140
- gr.Markdown("### ⚙️ Einstellungen")
141
  max_tokens = gr.Slider(50, 300, value=150, step=10, label="Max. Antwortlänge")
142
  temperature = gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Kreativität (Temperature)")
143
  top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p (Fokus)")
144
 
145
- gr.Markdown("### 🔧 API Status")
146
  api_status = gr.Textbox(label="Status", value="")
147
  api_test_btn = gr.Button("API testen")
148
-
149
- gr.Markdown("### 🔄 Aktionen")
150
  reset_btn = gr.Button("Neues Gespräch")
151
-
152
- gr.Markdown("### 📋 Setup & Troubleshooting")
153
- gr.Markdown("""
154
- **Benötigt:**
155
- - `tomoniaccess` Umgebungsvariable mit HF Token
156
- - `pip install huggingface_hub gradio`
157
-
158
- **LeoLM Info:**
159
- - Deutsche Sprachoptimierung
160
- - 13B Parameter
161
- - Modell: `LeoLM/leo-hessianai-13b-chat`
162
-
163
- **Bei API Fehlern:**
164
- 1. Token prüfen (muss Pro/Enterprise sein)
165
- 2. Modell verfügbar? → [HF Model Card](https://huggingface.co/LeoLM/leo-hessianai-13b-chat)
166
- 3. Alternative: `LeoLM/leo-hessianai-7b-chat`
167
- 4. Fallback: `microsoft/DialoGPT-medium`
168
- """)
169
 
170
  with gr.Column(scale=2):
171
- gr.Markdown("### 💬 Gespräch")
172
- user_input = gr.Textbox(
173
- label="Deine Nachricht",
174
- placeholder="Hallo, wie geht es dir heute?",
175
- lines=2
176
- )
177
  send_btn = gr.Button("📨 Senden")
 
 
178
 
179
- bot_response = gr.Textbox(
180
- label="Antwort",
181
- value="",
182
- lines=3
183
- )
184
 
185
- chat_history = gr.Textbox(
186
- label="Gesprächsverlauf",
187
- value="",
188
- lines=15
189
- )
 
190
 
191
- # Event Bindings
192
- send_btn.click(
193
- fn=enhanced_chat_response,
194
- inputs=[user_input, max_tokens, temperature, top_p],
195
- outputs=[user_input, bot_response, chat_history]
196
- )
197
-
198
- user_input.submit(
199
- fn=enhanced_chat_response,
200
- inputs=[user_input, max_tokens, temperature, top_p],
201
- outputs=[user_input, bot_response, chat_history]
202
- )
203
-
204
- reset_btn.click(
205
- fn=reset_conversation,
206
- outputs=[bot_response, chat_history]
207
- )
208
-
209
- api_test_btn.click(
210
- fn=test_api_connection,
211
- outputs=[api_status]
212
- )
213
 
214
  if __name__ == "__main__":
215
- print("🚀 LeoLM Depression Training Simulator")
216
- print(f"📊 Model: {model_name}")
217
-
218
  if not HF_TOKEN:
219
- print("❌ FEHLER: tomoniaccess Umgebungsvariable ist nicht gesetzt!")
220
- print(" Bitte setze deinen Hugging Face Token als 'tomoniaccess' Umgebungsvariable.")
221
  else:
222
- print("✅ Hugging Face API Token gefunden")
223
-
224
- print("\n📦 Benötigte Pakete:")
225
- print("pip install huggingface_hub gradio")
226
- print("\n🇩🇪 LeoLM: Deutsches Sprachmodell für bessere Konversationen")
227
-
228
- demo.launch(share=False)
 
2
  import os
3
  from huggingface_hub import InferenceClient
4
 
5
+ # HF Token aus Umgebungsvariable laden
6
  HF_TOKEN = os.getenv("tomoniaccess")
7
 
8
+ # Modellname definieren
9
+ model_name = "LeoLM/leo-hessianai-13b-chat"
10
+
11
+ # Client initialisieren
12
  client = InferenceClient(
13
  model=model_name,
14
  token=HF_TOKEN
15
  )
16
 
17
+ # Globale Konversationshistorie
18
  conversation_history = []
19
 
20
  def query_leolm(messages, max_tokens=200, temperature=1.0, top_p=0.9):
21
+ """Versuche Chat-Completion, falle auf Text-Generation zurück."""
22
  try:
23
+ # Versuch über chat_completion
24
  response = client.chat_completion(
25
  messages=messages,
26
  max_tokens=max_tokens,
 
32
  except Exception as chat_error:
33
  print(f"Chat completion failed: {chat_error}")
34
  try:
35
+ # Fallback: Prompt manuell zusammensetzen
 
36
  prompt = ""
37
  for msg in messages:
38
  if msg["role"] == "system":
39
+ prompt += f"<|system|>\n{msg['content'].strip()}\n"
40
  elif msg["role"] == "user":
41
+ prompt += f"<|user|>\n{msg['content'].strip()}\n"
42
  elif msg["role"] == "assistant":
43
+ prompt += f"<|assistant|>\n{msg['content'].strip()}\n"
44
+ prompt += "<|assistant|>\n"
45
+
 
46
  response = client.text_generation(
47
  prompt=prompt,
48
  max_new_tokens=max_tokens,
49
  temperature=temperature,
50
  top_p=top_p,
51
  do_sample=True,
52
+ repetition_penalty=1.1,
53
+ stop_sequences=["<|user|>", "<|system|>"],
54
  return_full_text=False
55
  )
56
+ return response.strip()
57
  except Exception as text_error:
58
  print(f"Text generation also failed: {text_error}")
59
  raise Exception(f"Both methods failed - Chat: {str(chat_error)}, Text: {str(text_error)}")
60
 
61
  def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
62
+ global conversation_history
63
+
64
  if not user_input.strip():
65
  return "", "*Bitte gib eine Nachricht ein.*", ""
66
 
 
67
  system_prompt = {
68
  "role": "system",
69
  "content": (
 
74
  )
75
  }
76
 
77
+ # Kürze History falls nötig
78
  messages = [system_prompt] + conversation_history[-6:]
79
  current_message = {"role": "user", "content": user_input}
80
  messages.append(current_message)
 
101
  return "Neues Gespräch gestartet.", ""
102
 
103
  def test_api_connection():
 
104
  try:
 
105
  test_messages = [
106
+ {"role": "system", "content": "Du bist ein Assistent."},
107
  {"role": "user", "content": "Hallo"}
108
  ]
 
109
  response = query_leolm(test_messages, max_tokens=20)
110
  return f"✅ API Verbindung erfolgreich: {response[:50]}..."
111
  except Exception as e:
 
112
  try:
113
  simple_response = client.text_generation(
114
  prompt="Hallo, wie geht es dir?",
115
  max_new_tokens=10,
 
116
  return_full_text=False
117
  )
118
  return f"✅ API Verbindung (Text Generation): {simple_response[:50]}..."
119
  except Exception as e2:
120
+ return f"❌ Fehler: {str(e)[:100]} | {str(e2)[:100]}"
121
+
122
+ # Gradio UI
 
 
 
 
 
 
123
  with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
124
  gr.Markdown("## 🧠 Depression Training Simulator (LeoLM-13B)")
125
+ gr.Markdown("**Simuliere Gespräche mit einem 16-jährigen Teenager mit Depressionen.**")
126
+ gr.Markdown("*Sprachmodell: `LeoLM/leo-hessianai-13b-chat`*")
127
 
128
  with gr.Row():
129
  with gr.Column(scale=1):
 
130
  max_tokens = gr.Slider(50, 300, value=150, step=10, label="Max. Antwortlänge")
131
  temperature = gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Kreativität (Temperature)")
132
  top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p (Fokus)")
133
 
 
134
  api_status = gr.Textbox(label="Status", value="")
135
  api_test_btn = gr.Button("API testen")
 
 
136
  reset_btn = gr.Button("Neues Gespräch")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  with gr.Column(scale=2):
139
+ user_input = gr.Textbox(label="Deine Nachricht", placeholder="Wie fühlst du dich heute?", lines=2)
 
 
 
 
 
140
  send_btn = gr.Button("📨 Senden")
141
+ bot_response = gr.Textbox(label="Antwort", value="", lines=3)
142
+ chat_history = gr.Textbox(label="Gesprächsverlauf", value="", lines=15)
143
 
144
+ send_btn.click(fn=enhanced_chat_response,
145
+ inputs=[user_input, max_tokens, temperature, top_p],
146
+ outputs=[user_input, bot_response, chat_history])
 
 
147
 
148
+ user_input.submit(fn=enhanced_chat_response,
149
+ inputs=[user_input, max_tokens, temperature, top_p],
150
+ outputs=[user_input, bot_response, chat_history])
151
+
152
+ reset_btn.click(fn=reset_conversation,
153
+ outputs=[bot_response, chat_history])
154
 
155
+ api_test_btn.click(fn=test_api_connection,
156
+ outputs=[api_status])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  if __name__ == "__main__":
159
+ print("🚀 Starte LeoLM Depression Simulator")
 
 
160
  if not HF_TOKEN:
161
+ print("❌ Umgebungsvariable 'tomoniaccess' nicht gesetzt.")
 
162
  else:
163
+ print("✅ Token erkannt")
164
+ demo.launch()