Update app_1.py
Browse files
app_1.py
CHANGED
|
@@ -1,94 +1,64 @@
|
|
| 1 |
-
# pip install -U gradio huggingface_hub
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
"""
|
| 15 |
-
Gradio
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
- oder gemischte Formen
|
| 19 |
-
Wir normalisieren auf OpenAI/HF chat messages: [{"role": "...", "content": "..."}]
|
| 20 |
"""
|
|
|
|
|
|
|
| 21 |
messages = []
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
messages.append({"role": "user", "content": str(u)})
|
| 29 |
-
if a:
|
| 30 |
-
messages.append({"role": "assistant", "content": str(a)})
|
| 31 |
-
|
| 32 |
-
# Fall 2: dict-style messages
|
| 33 |
-
elif isinstance(item, dict):
|
| 34 |
-
role = item.get("role")
|
| 35 |
-
content = item.get("content", "")
|
| 36 |
-
if role in ("user", "assistant") and content:
|
| 37 |
-
messages.append({"role": role, "content": str(content)})
|
| 38 |
-
|
| 39 |
-
# Unbekannt: ignorieren
|
| 40 |
-
return messages
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def chat(user_msg, history):
|
| 44 |
-
messages = history_to_messages(history)
|
| 45 |
-
messages.append({"role": "user", "content": user_msg})
|
| 46 |
-
|
| 47 |
-
resp = client.chat.completions.create(
|
| 48 |
-
messages=messages,
|
| 49 |
-
max_tokens=512,
|
| 50 |
-
temperature=0.2,
|
| 51 |
-
)
|
| 52 |
-
return resp.choices[0].message["content"]
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
def chat_stream(user_msg, history):
|
| 56 |
-
"""
|
| 57 |
-
Optional: Streaming (falls vom Provider/Backend unterstützt).
|
| 58 |
-
Fällt sonst automatisch auf nicht-streaming zurück, wenn es knallt.
|
| 59 |
-
"""
|
| 60 |
-
messages = history_to_messages(history)
|
| 61 |
-
messages.append({"role": "user", "content": user_msg})
|
| 62 |
|
| 63 |
try:
|
|
|
|
| 64 |
stream = client.chat.completions.create(
|
| 65 |
messages=messages,
|
| 66 |
-
max_tokens=
|
| 67 |
-
temperature=0.
|
| 68 |
stream=True,
|
| 69 |
)
|
| 70 |
-
out = ""
|
| 71 |
-
for chunk in stream:
|
| 72 |
-
# chunk format ist providerabhängig, meistens ähnlich OpenAI
|
| 73 |
-
delta = ""
|
| 74 |
-
try:
|
| 75 |
-
delta = chunk.choices[0].delta.get("content", "") or ""
|
| 76 |
-
except Exception:
|
| 77 |
-
# Fallback, wenn chunk anders strukturiert ist
|
| 78 |
-
delta = getattr(chunk, "delta", "") or ""
|
| 79 |
-
if delta:
|
| 80 |
-
out += delta
|
| 81 |
-
yield out
|
| 82 |
-
except Exception:
|
| 83 |
-
# Fallback: ohne streaming
|
| 84 |
-
yield chat(user_msg, history)
|
| 85 |
-
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
demo = gr.ChatInterface(
|
| 88 |
-
fn=
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Konfiguration
|
| 6 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct" # Beispiel ID (Qwen3 ist ggf. noch nicht public/verfügbar)
|
| 7 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
+
# Check für Token (Wichtig für Fehlervermeidung)
|
| 10 |
+
if not HF_TOKEN:
|
| 11 |
+
print("Warnung: Kein HF_TOKEN gefunden. Ratenbegrenzungen könnten strenger sein.")
|
| 12 |
|
| 13 |
+
# Client initialisieren
|
| 14 |
+
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 15 |
|
| 16 |
+
def chat_stream(message, history):
|
| 17 |
"""
|
| 18 |
+
Gradio 6.x übergibt:
|
| 19 |
+
- message: Der aktuelle User-Input (str)
|
| 20 |
+
- history: Die bisherige Konversation als list[dict] (dank type="messages")
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
+
|
| 23 |
+
# 1. Nachrichtenliste aufbauen (System-Prompt optional hier einfügen)
|
| 24 |
messages = []
|
| 25 |
+
# Optional: System-Prompt
|
| 26 |
+
# messages.append({"role": "system", "content": "Du bist ein hilfreicher Coding-Assistent."})
|
| 27 |
+
|
| 28 |
+
# Historie + aktuelle Nachricht anhängen
|
| 29 |
+
messages.extend(history)
|
| 30 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
try:
|
| 33 |
+
# 2. Anfrage an Hugging Face (Streaming aktiviert)
|
| 34 |
stream = client.chat.completions.create(
|
| 35 |
messages=messages,
|
| 36 |
+
max_tokens=1024, # Erhöht für Code-Generation
|
| 37 |
+
temperature=0.2,
|
| 38 |
stream=True,
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
partial_text = ""
|
| 42 |
+
for chunk in stream:
|
| 43 |
+
# Sicherer Zugriff auf das Delta-Objekt (Pydantic Model)
|
| 44 |
+
delta_content = chunk.choices[0].delta.content
|
| 45 |
+
|
| 46 |
+
if delta_content:
|
| 47 |
+
partial_text += delta_content
|
| 48 |
+
yield partial_text
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
# Fehlerbehandlung direkt im Chat-Fenster
|
| 52 |
+
yield f"⚠️ Ein Fehler ist aufgetreten: {str(e)}"
|
| 53 |
+
|
| 54 |
+
# Interface definieren
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
+
fn=chat_stream,
|
| 57 |
+
#type="messages", # WICHTIG: Aktiviert das neue Format für Gradio 5/6
|
| 58 |
+
title=f"Chat mit {MODEL_ID}",
|
| 59 |
+
description="Ein optimierter Coding-Assistent mit Gradio 6.2 und HF Inference.",
|
| 60 |
+
fill_height=True, # Nutzt den vollen Bildschirm besser
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|