Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 1. Verbindung zur KI
|
| 6 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 7 |
+
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=hf_token)
|
| 8 |
+
|
| 9 |
+
SYSTEM_PROMPT = "Du bist CocoAi von CocoEntertainment. Du bist loyal, schlau und nutzt Katzen-Emojis 🐱. Antworte immer auf Deutsch."
|
| 10 |
+
|
| 11 |
+
def chat_fn(message, history):
|
| 12 |
+
# In Gradio 6 ist die History standardmäßig eine Liste von Dictionaries
|
| 13 |
+
api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 14 |
+
|
| 15 |
+
for msg_obj in history:
|
| 16 |
+
api_messages.append(msg_obj)
|
| 17 |
+
|
| 18 |
+
api_messages.append({"role": "user", "content": str(message)})
|
| 19 |
+
|
| 20 |
+
# Neue Nachricht zur History für die Anzeige hinzufügen
|
| 21 |
+
history.append({"role": "user", "content": message})
|
| 22 |
+
|
| 23 |
+
response = ""
|
| 24 |
+
try:
|
| 25 |
+
for msg in client.chat_completion(api_messages, max_tokens=1000, stream=True, temperature=0.7):
|
| 26 |
+
if msg.choices and msg.choices[0].delta.content:
|
| 27 |
+
token = msg.choices[0].delta.content
|
| 28 |
+
response += token
|
| 29 |
+
# Wir geben die aktualisierte Liste zurück
|
| 30 |
+
yield history + [{"role": "assistant", "content": response}]
|
| 31 |
+
except Exception as e:
|
| 32 |
+
yield history + [{"role": "assistant", "content": f"Fehler: {str(e)}"}]
|
| 33 |
+
|
| 34 |
+
# 2. Design & Styling (CSS)
|
| 35 |
+
custom_css = """
|
| 36 |
+
.gradio-container { background-color: #f0f4ff !important; font-family: 'Arial', sans-serif !important; }
|
| 37 |
+
.message { border-radius: 15px !important; box-shadow: 0px 4px 15px rgba(0,0,0,0.05) !important; }
|
| 38 |
+
#input-box { background-color: #ffffff !important; border-radius: 30px !important; border: 2px solid #3b82f6 !important; }
|
| 39 |
+
.primary-btn { border-radius: 30px !important; background-color: #3b82f6 !important; color: white !important; font-weight: bold !important; border: none !important; }
|
| 40 |
+
footer { display: none !important; }
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
# 3. GUI Aufbau (Gradio 6 Style)
|
| 44 |
+
# 'theme' und 'css' wurden hier entfernt laut deiner Fehlermeldung
|
| 45 |
+
with gr.Blocks(title="CocoAi Smooth") as demo:
|
| 46 |
+
with gr.Column():
|
| 47 |
+
gr.Markdown("<h1 style='text-align: center; color: #1e40af;'>🐱 CocoAi</h1>")
|
| 48 |
+
gr.Markdown("<p style='text-align: center; color: #1f2937;'>Intelligent • Loyal • CocoEntertainment</p>")
|
| 49 |
+
|
| 50 |
+
# 'type="messages"' entfernt, da Gradio 6 dies nun intern anders oder als Standard handhabt
|
| 51 |
+
chatbot = gr.Chatbot(
|
| 52 |
+
elem_id="chat-box",
|
| 53 |
+
show_label=False,
|
| 54 |
+
height=450
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
msg = gr.Textbox(
|
| 59 |
+
show_label=False,
|
| 60 |
+
placeholder="Frag CocoAi etwas...",
|
| 61 |
+
container=True,
|
| 62 |
+
elem_id="input-box",
|
| 63 |
+
scale=7
|
| 64 |
+
)
|
| 65 |
+
btn = gr.Button("Senden", variant="primary", elem_classes="primary-btn", scale=1)
|
| 66 |
+
|
| 67 |
+
# Events
|
| 68 |
+
submit_event = msg.submit(chat_fn, [msg, chatbot], [chatbot])
|
| 69 |
+
submit_event.then(lambda: "", None, [msg])
|
| 70 |
+
|
| 71 |
+
btn.click(chat_fn, [msg, chatbot], [chatbot]).then(lambda: "", None, [msg])
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
# WICHTIG: In Gradio 6 müssen theme und css hier in launch()
|
| 75 |
+
demo.launch(
|
| 76 |
+
theme=gr.themes.Soft(),
|
| 77 |
+
css=custom_css
|
| 78 |
+
)
|
| 79 |
+
|