Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,123 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# 1.
|
| 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,
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def
|
| 12 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
for interaction in history:
|
| 17 |
-
# Prüfen, ob die Interaktion ein Dictionary oder eine Liste ist
|
| 18 |
-
if isinstance(interaction, dict):
|
| 19 |
-
messages.append({"role": "user", "content": interaction.get("content", "")})
|
| 20 |
-
elif isinstance(interaction, (list, tuple)) and len(interaction) >= 2:
|
| 21 |
-
messages.append({"role": "user", "content": interaction[0]})
|
| 22 |
-
messages.append({"role": "assistant", "content": interaction[1]})
|
| 23 |
-
|
| 24 |
-
# Aktuelle Nachricht
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
response = ""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. Verbindung & Prompting
|
| 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, die offizielle KI von CocoEntertainment.
|
| 10 |
+
Du bist loyal, schlau und nutzt Katzen-Emojis 🐱.
|
| 11 |
+
Dein Wissen umfasst CocoGames, CocoCity und die Inspiration durch die Katze Coco."""
|
| 12 |
|
| 13 |
+
def respond(message, chat_history):
|
| 14 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 15 |
+
for user, assistant in chat_history:
|
| 16 |
+
messages.append({"role": "user", "content": user})
|
| 17 |
+
messages.append({"role": "assistant", "content": assistant})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
response = ""
|
| 21 |
+
for msg in client.chat_completion(messages, max_tokens=1000, stream=True, temperature=0.8):
|
| 22 |
+
if msg.choices and len(msg.choices) > 0:
|
| 23 |
+
token = msg.choices[0].delta.content
|
| 24 |
+
if token:
|
| 25 |
+
response += token
|
| 26 |
+
yield response
|
| 27 |
+
|
| 28 |
+
# 2. Das "Smooth & Round" Design
|
| 29 |
+
custom_css = """
|
| 30 |
+
/* Ganze Seite auf Weiß umstellen */
|
| 31 |
+
.gradio-container {
|
| 32 |
+
background-color: #ffffff !important;
|
| 33 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
/* Chatbot-Fenster: Keine harten Kanten, weicher Schatten */
|
| 37 |
+
#custom-chatbot {
|
| 38 |
+
border: none !important;
|
| 39 |
+
background-color: #f8fbff !important;
|
| 40 |
+
border-radius: 30px !important;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
/* Sprechblasen abrunden */
|
| 44 |
+
.message {
|
| 45 |
+
border-radius: 20px !important;
|
| 46 |
+
padding: 12px 16px !important;
|
| 47 |
+
border: 1px solid #e1effe !important;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/* Header Text in kräftigem Blau */
|
| 51 |
+
#coco-header {
|
| 52 |
+
text-align: center;
|
| 53 |
+
color: #1a56db;
|
| 54 |
+
margin-top: 20px;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/* Eingabefeld extrem rund */
|
| 58 |
+
#input-box textarea {
|
| 59 |
+
border-radius: 25px !important;
|
| 60 |
+
border: 2px solid #e1effe !important;
|
| 61 |
+
padding: 10px 20px !important;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/* Buttons abrunden */
|
| 65 |
+
.round-btn {
|
| 66 |
+
border-radius: 25px !important;
|
| 67 |
+
background: #3b82f6 !important;
|
| 68 |
+
color: white !important;
|
| 69 |
+
transition: all 0.3s ease;
|
| 70 |
+
}
|
| 71 |
+
.round-btn:hover {
|
| 72 |
+
transform: scale(1.02);
|
| 73 |
+
background: #2563eb !important;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
footer { display: none !important; }
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
with gr.Blocks(css=custom_css, title="CocoAi Smooth") as demo:
|
| 80 |
+
|
| 81 |
+
with gr.Column(elem_id="coco-header"):
|
| 82 |
+
gr.Markdown("# 🐱 CocoAi")
|
| 83 |
+
gr.Markdown("### Intelligent • Loyal • CocoEntertainment")
|
| 84 |
+
|
| 85 |
+
chatbot = gr.Chatbot(
|
| 86 |
+
label=None,
|
| 87 |
+
elem_id="custom-chatbot",
|
| 88 |
+
bubble_full_width=False,
|
| 89 |
+
show_label=False
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
txt = gr.Textbox(
|
| 94 |
+
show_label=False,
|
| 95 |
+
placeholder="Nachricht an CocoAi...",
|
| 96 |
+
scale=4,
|
| 97 |
+
elem_id="input-box"
|
| 98 |
+
)
|
| 99 |
+
submit_btn = gr.Button("Senden", variant="primary", scale=1, elem_classes="round-btn")
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
# Auch der Löschen-Button wird rund
|
| 103 |
+
gr.ClearButton([txt, chatbot], value="Chat leeren", variant="secondary")
|
| 104 |
+
|
| 105 |
+
# Logik
|
| 106 |
+
def user_input(msg, history):
|
| 107 |
+
return "", history + [[msg, ""]]
|
| 108 |
+
|
| 109 |
+
def bot_response(history):
|
| 110 |
+
user_message = history[-1][0]
|
| 111 |
+
history[-1][1] = ""
|
| 112 |
+
for character in respond(user_message, history[:-1]):
|
| 113 |
+
history[-1][1] = character
|
| 114 |
+
yield history
|
| 115 |
+
|
| 116 |
+
submit_btn.click(user_input, [txt, chatbot], [txt, chatbot], queue=False).then(
|
| 117 |
+
bot_response, chatbot, chatbot
|
| 118 |
+
)
|
| 119 |
+
txt.submit(user_input, [txt, chatbot], [txt, chatbot], queue=False).then(
|
| 120 |
+
bot_response, chatbot, chatbot
|
| 121 |
+
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
demo.launch()
|