Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
|
@@ -11,62 +11,73 @@ def respond(
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"""
|
| 17 |
client = InferenceClient(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
|
|
|
|
|
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
response = ""
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
response += token
|
| 42 |
-
yield response
|
| 43 |
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 47 |
-
"""
|
| 48 |
chatbot = gr.ChatInterface(
|
| 49 |
respond,
|
| 50 |
type="messages",
|
| 51 |
additional_inputs=[
|
| 52 |
-
gr.Textbox(value="
|
| 53 |
-
gr.Slider(minimum=1, maximum=
|
| 54 |
-
gr.Slider(minimum=0.1, maximum=
|
| 55 |
gr.Slider(
|
| 56 |
minimum=0.1,
|
| 57 |
maximum=1.0,
|
| 58 |
value=0.95,
|
| 59 |
step=0.05,
|
| 60 |
-
label="Top-p
|
| 61 |
),
|
| 62 |
],
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
with gr.Sidebar():
|
|
|
|
| 67 |
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
| 68 |
chatbot.render()
|
| 69 |
|
| 70 |
-
|
| 71 |
if __name__ == "__main__":
|
| 72 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# تعريف الدالة التي ستتولى الرد
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
|
|
|
| 11 |
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
+
# استخدام نموذج Qwen 2.5 72B الحديث
|
| 15 |
+
# ملاحظة: إذا واجهت مشكلة في التوفر، يمكنك تجربة "Qwen/Qwen2.5-32B-Instruct" كبديل أسرع
|
|
|
|
| 16 |
client = InferenceClient(
|
| 17 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
| 18 |
+
token=hf_token.token
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# بناء قائمة الرسائل بدءاً برسالة النظام
|
| 22 |
messages = [{"role": "system", "content": system_message}]
|
| 23 |
|
| 24 |
+
# إضافة سجل المحادثة السابق
|
| 25 |
+
for interaction in history:
|
| 26 |
+
messages.append(interaction)
|
| 27 |
|
| 28 |
+
# إضافة رسالة المستخدم الحالية
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
response = ""
|
| 32 |
|
| 33 |
+
try:
|
| 34 |
+
# الاتصال بالنموذج وتوليد النص بشكل تدفقي (Streaming)
|
| 35 |
+
for msg in client.chat_completion(
|
| 36 |
+
messages,
|
| 37 |
+
max_tokens=max_tokens,
|
| 38 |
+
stream=True,
|
| 39 |
+
temperature=temperature,
|
| 40 |
+
top_p=top_p,
|
| 41 |
+
):
|
| 42 |
+
token = msg.choices[0].delta.content
|
| 43 |
+
if token:
|
| 44 |
+
response += token
|
| 45 |
+
yield response
|
| 46 |
+
except Exception as e:
|
| 47 |
+
# عرض رسالة خطأ واضحة في حال تعذر الوصول للنموذج
|
| 48 |
+
yield f"عذراً، حدث خطأ أثناء الاتصال بالنموذج: {str(e)}"
|
| 49 |
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# بناء واجهة المستخدم باستخدام Gradio
|
|
|
|
|
|
|
|
|
|
| 52 |
chatbot = gr.ChatInterface(
|
| 53 |
respond,
|
| 54 |
type="messages",
|
| 55 |
additional_inputs=[
|
| 56 |
+
gr.Textbox(value="أنت مساعد ذكي ومفيد يتحدث العربية بطلاقة.", label="رسالة النظام (System message)"),
|
| 57 |
+
gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="الحد الأقصى للكلمات (Max tokens)"),
|
| 58 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="درجة الإبداع (Temperature)"),
|
| 59 |
gr.Slider(
|
| 60 |
minimum=0.1,
|
| 61 |
maximum=1.0,
|
| 62 |
value=0.95,
|
| 63 |
step=0.05,
|
| 64 |
+
label="تنوع الإجابات (Top-p)",
|
| 65 |
),
|
| 66 |
],
|
| 67 |
+
title="دردشة مع Qwen 2.5 72B",
|
| 68 |
+
description="واجهة تفاعلية قوية تدعم العربية بشكل كامل.",
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# عرض الواجهة مع زر تسجيل الدخول
|
| 72 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 73 |
with gr.Sidebar():
|
| 74 |
+
gr.Markdown("### الإعدادات")
|
| 75 |
gr.LoginButton()
|
| 76 |
+
gr.Markdown("---")
|
| 77 |
+
gr.Markdown("قم بتسجيل الدخول بحساب Hugging Face لاستخدام الـ API الخاص بك.")
|
| 78 |
+
|
| 79 |
chatbot.render()
|
| 80 |
|
| 81 |
+
# تشغيل التطبيق
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|