Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,9 @@ import json
|
|
| 9 |
# Deutsche LLM Konfiguration
|
| 10 |
HF_TOKEN = os.getenv("tomoniaccess")
|
| 11 |
current_model = "HuggingFaceH4/zephyr-7b-beta"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
provider="auto", # <- automatisch wählen
|
| 16 |
-
token=HF_TOKEN
|
| 17 |
-
)
|
| 18 |
|
| 19 |
conversation_history = []
|
| 20 |
|
|
@@ -35,46 +32,50 @@ def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
|
|
| 35 |
# Hier printen wir die messages vor dem API-Aufruf
|
| 36 |
print("Messages sent to API:", messages)
|
| 37 |
|
| 38 |
-
|
| 39 |
# Testfrage an Modell, ob es die Rolle kennt:
|
| 40 |
test_message = {"role": "user", "content": "Was bist du für eine Rolle?"}
|
| 41 |
messages_test = [system_prompt, test_message]
|
| 42 |
test_response = ""
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
#f"User: {user_input}\nAssistant:"
|
| 49 |
-
#)
|
| 50 |
-
|
| 51 |
-
#messages = [{"role": "user", "content": role_instruction}]
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
for message in client.chat_completion(
|
| 56 |
-
messages=messages_test,
|
| 57 |
-
max_tokens=50,
|
| 58 |
-
stream=False,
|
| 59 |
-
):
|
| 60 |
-
test_response += message.choices[0].message.content
|
| 61 |
-
|
| 62 |
-
print("Modellantwort auf Rollentest:", test_response)
|
| 63 |
|
| 64 |
response_text = ""
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
|
|
|
|
| 68 |
messages=messages,
|
| 69 |
max_tokens=min(max_tokens, 100),
|
| 70 |
stream=False,
|
| 71 |
temperature=temperature,
|
| 72 |
top_p=top_p
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
print(f"API Error: {e}")
|
|
@@ -83,8 +84,7 @@ def enhanced_chat_response(user_input, max_tokens, temperature, top_p):
|
|
| 83 |
|
| 84 |
print("Antwort des Modells:", response_text)
|
| 85 |
|
| 86 |
-
response_text = response_text.strip()
|
| 87 |
-
|
| 88 |
|
| 89 |
chat_display = f"**Du:** {user_input}\n**Assistant:** {response_text}\n\n"
|
| 90 |
|
|
@@ -137,7 +137,6 @@ with gr.Blocks(title="Depression Training Simulator", theme=gr.themes.Soft()) as
|
|
| 137 |
# feedback_display = gr.Markdown("Starte ein Gespräch, um Feedback zu erhalten.")
|
| 138 |
|
| 139 |
# Event Bindings
|
| 140 |
-
|
| 141 |
send_btn.click(
|
| 142 |
fn=enhanced_chat_response,
|
| 143 |
inputs=[user_input, max_tokens, temperature, top_p],
|
|
|
|
| 9 |
# Deutsche LLM Konfiguration
|
| 10 |
HF_TOKEN = os.getenv("tomoniaccess")
|
| 11 |
current_model = "HuggingFaceH4/zephyr-7b-beta"
|
| 12 |
+
|
| 13 |
+
# Lösung 2: Ohne Provider Parameter (Standard HuggingFace)
|
| 14 |
+
client = InferenceClient(model=current_model, token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
conversation_history = []
|
| 17 |
|
|
|
|
| 32 |
# Hier printen wir die messages vor dem API-Aufruf
|
| 33 |
print("Messages sent to API:", messages)
|
| 34 |
|
|
|
|
| 35 |
# Testfrage an Modell, ob es die Rolle kennt:
|
| 36 |
test_message = {"role": "user", "content": "Was bist du für eine Rolle?"}
|
| 37 |
messages_test = [system_prompt, test_message]
|
| 38 |
test_response = ""
|
| 39 |
|
| 40 |
+
try:
|
| 41 |
+
# Erst den Rollentest
|
| 42 |
+
test_result = client.chat_completion(
|
| 43 |
+
messages=messages_test,
|
| 44 |
+
max_tokens=50,
|
| 45 |
+
stream=False,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Korrigiere den Zugriff auf die Antwort
|
| 49 |
+
if hasattr(test_result, 'choices') and test_result.choices:
|
| 50 |
+
test_response = test_result.choices[0].message.content
|
| 51 |
+
else:
|
| 52 |
+
# Fallback für andere Antwortformate
|
| 53 |
+
test_response = str(test_result)
|
| 54 |
+
|
| 55 |
+
print("Modellantwort auf Rollentest:", test_response)
|
| 56 |
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Test API Error: {e}")
|
| 59 |
+
test_response = "Test fehlgeschlagen"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
response_text = ""
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
# Hauptanfrage - korrigiere auch hier den Zugriff
|
| 65 |
+
result = client.chat_completion(
|
| 66 |
messages=messages,
|
| 67 |
max_tokens=min(max_tokens, 100),
|
| 68 |
stream=False,
|
| 69 |
temperature=temperature,
|
| 70 |
top_p=top_p
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Korrigiere den Zugriff auf die Antwort
|
| 74 |
+
if hasattr(result, 'choices') and result.choices:
|
| 75 |
+
response_text = result.choices[0].message.content
|
| 76 |
+
else:
|
| 77 |
+
# Fallback für andere Antwortformate
|
| 78 |
+
response_text = str(result)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
print(f"API Error: {e}")
|
|
|
|
| 84 |
|
| 85 |
print("Antwort des Modells:", response_text)
|
| 86 |
|
| 87 |
+
response_text = response_text.strip() if response_text else ""
|
|
|
|
| 88 |
|
| 89 |
chat_display = f"**Du:** {user_input}\n**Assistant:** {response_text}\n\n"
|
| 90 |
|
|
|
|
| 137 |
# feedback_display = gr.Markdown("Starte ein Gespräch, um Feedback zu erhalten.")
|
| 138 |
|
| 139 |
# Event Bindings
|
|
|
|
| 140 |
send_btn.click(
|
| 141 |
fn=enhanced_chat_response,
|
| 142 |
inputs=[user_input, max_tokens, temperature, top_p],
|