Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,23 @@
|
|
| 1 |
-
import
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
message,
|
| 6 |
-
history: list[dict[str, str]],
|
| 7 |
-
system_message,
|
| 8 |
-
max_tokens,
|
| 9 |
-
temperature,
|
| 10 |
-
top_p,
|
| 11 |
-
hf_token: gr.OAuthToken,
|
| 12 |
-
):
|
| 13 |
-
# استخدام نموذج Qwen 2.5 72B
|
| 14 |
-
client = InferenceClient(
|
| 15 |
-
model="moonshotai/Kimi-K2-Instruct",
|
| 16 |
-
token=hf_token.token
|
| 17 |
-
)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
messages = [{"role": "system", "content": system_message}]
|
| 21 |
-
|
| 22 |
-
# إضافة التاريخ (history في Gradio type="messages" هو قائمة قواميس جاهزة)
|
| 23 |
-
messages.extend(history)
|
| 24 |
-
|
| 25 |
-
# إضافة رسالة المستخدم الحالية
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
stream=True,
|
| 36 |
-
temperature=temperature,
|
| 37 |
-
top_p=top_p,
|
| 38 |
-
):
|
| 39 |
-
# التأكد من أن القائمة ليست فارغة قبل الوصول للعنصر [0]
|
| 40 |
-
if msg.choices and len(msg.choices) > 0:
|
| 41 |
-
token = msg.choices[0].delta.content
|
| 42 |
-
if token:
|
| 43 |
-
response += token
|
| 44 |
-
yield response
|
| 45 |
-
except Exception as e:
|
| 46 |
-
yield f"⚠️ حدث خطأ: {str(e)}"
|
| 47 |
-
|
| 48 |
-
# إعداد واجهة Gradio
|
| 49 |
-
chatbot = gr.ChatInterface(
|
| 50 |
-
respond,
|
| 51 |
-
type="messages", # يضمن إرسال التاريخ بتنسيق قواميس الأدوار
|
| 52 |
-
additional_inputs=[
|
| 53 |
-
gr.Textbox(value="أنت مساعد ذكي ومفيد.", label="System message"),
|
| 54 |
-
gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max tokens"),
|
| 55 |
-
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 56 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 57 |
],
|
| 58 |
-
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
with gr.Blocks() as demo:
|
| 62 |
-
with gr.Sidebar():
|
| 63 |
-
gr.LoginButton()
|
| 64 |
-
chatbot.render()
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { InferenceClient } from "@huggingface/inference";
|
|
|
|
| 2 |
|
| 3 |
+
const client = new InferenceClient(process.env.HF_TOKEN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
let out = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
const stream = client.chatCompletionStream({
|
| 8 |
+
model: "moonshotai/Kimi-K2-Instruct-0905",
|
| 9 |
+
messages: [
|
| 10 |
+
{
|
| 11 |
+
role: "user",
|
| 12 |
+
content: "What is the capital of France?",
|
| 13 |
+
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
],
|
| 15 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
for await (const chunk of stream) {
|
| 18 |
+
if (chunk.choices && chunk.choices.length > 0) {
|
| 19 |
+
const newContent = chunk.choices[0].delta.content;
|
| 20 |
+
out += newContent;
|
| 21 |
+
console.log(newContent);
|
| 22 |
+
}
|
| 23 |
+
}
|