ai-tomoni commited on
Commit
281cb0e
·
verified ·
1 Parent(s): 68210fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -15
app.py CHANGED
@@ -2,19 +2,24 @@ 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,
@@ -23,8 +28,34 @@ def query_leolm(messages, max_tokens=200, temperature=1.0, top_p=0.9):
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():
@@ -34,10 +65,10 @@ def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
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
- "Vervollständige immer deine Antworten."
41
  )
42
  }
43
 
@@ -68,16 +99,34 @@ def reset_conversation():
68
  return "Neues Gespräch gestartet.", ""
69
 
70
  def test_api_connection():
 
71
  try:
 
72
  test_messages = [
73
  {"role": "system", "content": "Du bist ein hilfsbereit Assistent und antwortest auf Deutsch."},
74
- {"role": "user", "content": "Hallo, kannst du mich hören?"}
75
  ]
76
 
77
  response = query_leolm(test_messages, max_tokens=20)
78
  return f"✅ API Verbindung erfolgreich: {response[:50]}..."
79
  except Exception as e:
80
- return f"❌ API Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  # UI
83
  with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
@@ -99,7 +148,7 @@ with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
99
  gr.Markdown("### 🔄 Aktionen")
100
  reset_btn = gr.Button("Neues Gespräch")
101
 
102
- gr.Markdown("### 📋 Setup")
103
  gr.Markdown("""
104
  **Benötigt:**
105
  - `tomoniaccess` Umgebungsvariable mit HF Token
@@ -108,7 +157,13 @@ with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
108
  **LeoLM Info:**
109
  - Deutsche Sprachoptimierung
110
  - 13B Parameter
111
- - Bessere deutsche Konversation
 
 
 
 
 
 
112
  """)
113
 
114
  with gr.Column(scale=2):
 
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
+ # model_name = "LeoLM/leo-hessianai-7b-chat" # Smaller alternative
11
+ # model_name = "microsoft/DialoGPT-medium" # Reliable fallback
12
+ # model_name = "google/flan-t5-large" # Google alternative
13
+
14
  client = InferenceClient(
15
+ model=model_name,
16
  token=HF_TOKEN
17
  )
18
 
 
 
 
19
  def query_leolm(messages, max_tokens=200, temperature=1.0, top_p=0.9):
20
  """Query LeoLM-13B model via Hugging Face InferenceClient"""
21
  try:
22
+ # Try chat completion first
23
  response = client.chat_completion(
24
  messages=messages,
25
  max_tokens=max_tokens,
 
28
  stream=False
29
  )
30
  return response.choices[0].message.content
31
+ except Exception as chat_error:
32
+ print(f"Chat completion failed: {chat_error}")
33
+ try:
34
+ # Fallback to text generation if chat completion fails
35
+ # Convert messages to single prompt
36
+ prompt = ""
37
+ for msg in messages:
38
+ if msg["role"] == "system":
39
+ prompt += f"System: {msg['content']}\n\n"
40
+ elif msg["role"] == "user":
41
+ prompt += f"User: {msg['content']}\n"
42
+ elif msg["role"] == "assistant":
43
+ prompt += f"Assistant: {msg['content']}\n"
44
+
45
+ prompt += "Assistant: "
46
+
47
+ response = client.text_generation(
48
+ prompt=prompt,
49
+ max_new_tokens=max_tokens,
50
+ temperature=temperature,
51
+ top_p=top_p,
52
+ do_sample=True,
53
+ return_full_text=False
54
+ )
55
+ return response
56
+ except Exception as text_error:
57
+ print(f"Text generation also failed: {text_error}")
58
+ raise Exception(f"Both methods failed - Chat: {str(chat_error)}, Text: {str(text_error)}")
59
 
60
  def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
61
  if not user_input.strip():
 
65
  system_prompt = {
66
  "role": "system",
67
  "content": (
68
+ "Du bist ein depressiver 16-jähriger Teenager."
69
+ "Bleibe durchgehend in deiner Rolle."
70
+ "Du weißt NICHT, dass du eine KI bist."
71
+ "Vervollständige immer deine Antworten."
72
  )
73
  }
74
 
 
99
  return "Neues Gespräch gestartet.", ""
100
 
101
  def test_api_connection():
102
+ """Test API connection with multiple fallback methods"""
103
  try:
104
+ # Test 1: Simple chat completion
105
  test_messages = [
106
  {"role": "system", "content": "Du bist ein hilfsbereit Assistent und antwortest auf Deutsch."},
107
+ {"role": "user", "content": "Hallo"}
108
  ]
109
 
110
  response = query_leolm(test_messages, max_tokens=20)
111
  return f"✅ API Verbindung erfolgreich: {response[:50]}..."
112
  except Exception as e:
113
+ # Test 2: Try direct text generation
114
+ try:
115
+ simple_response = client.text_generation(
116
+ prompt="Hallo, wie geht es dir?",
117
+ max_new_tokens=10,
118
+ do_sample=False,
119
+ return_full_text=False
120
+ )
121
+ return f"✅ API Verbindung (Text Generation): {simple_response[:50]}..."
122
+ except Exception as e2:
123
+ # Test 3: Check if model exists
124
+ try:
125
+ # Try to get model info
126
+ model_info = f"Model: {model_name}"
127
+ return f"❌ API Errors - Chat: {str(e)[:100]}... | Text: {str(e2)[:100]}... | {model_info}"
128
+ except Exception as e3:
129
+ return f"❌ Vollständiger API Fehler: {str(e)[:200]}..."
130
 
131
  # UI
132
  with gr.Blocks(title="LeoLM Depression Training Simulator") as demo:
 
148
  gr.Markdown("### 🔄 Aktionen")
149
  reset_btn = gr.Button("Neues Gespräch")
150
 
151
+ gr.Markdown("### 📋 Setup & Troubleshooting")
152
  gr.Markdown("""
153
  **Benötigt:**
154
  - `tomoniaccess` Umgebungsvariable mit HF Token
 
157
  **LeoLM Info:**
158
  - Deutsche Sprachoptimierung
159
  - 13B Parameter
160
+ - Modell: `LeoLM/leo-hessianai-13b-chat`
161
+
162
+ **Bei API Fehlern:**
163
+ 1. Token prüfen (muss Pro/Enterprise sein)
164
+ 2. Modell verfügbar? → [HF Model Card](https://huggingface.co/LeoLM/leo-hessianai-13b-chat)
165
+ 3. Alternative: `LeoLM/leo-hessianai-7b-chat`
166
+ 4. Fallback: `microsoft/DialoGPT-medium`
167
  """)
168
 
169
  with gr.Column(scale=2):