Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,52 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from openai import OpenAI
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
+
# إعداد العميل للاتصال بـ Hugging Face
|
| 6 |
+
# ملاحظة: ستحتاج لـ Access Token من حسابك في Hugging Face
|
| 7 |
+
client = OpenAI(
|
| 8 |
+
base_url="https://api-inference.huggingface.co/v1/",
|
| 9 |
+
api_key="YOUR_HF_TOKEN_HERE" # استبدل هذا برمز الوصول الخاص بك
|
| 10 |
)
|
| 11 |
|
| 12 |
+
model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
|
| 13 |
+
|
| 14 |
+
def predict(message, history):
|
| 15 |
+
messages = []
|
| 16 |
+
|
| 17 |
+
# بناء سياق المحادثة (الذاكرة)
|
| 18 |
+
for human, assistant in history:
|
| 19 |
+
messages.append({"role": "user", "content": human})
|
| 20 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 21 |
+
|
| 22 |
+
messages.append({"role": "user", "content": message})
|
| 23 |
+
|
| 24 |
+
response = ""
|
| 25 |
+
|
| 26 |
+
# طلب الرد من النموذج بنظام التدفق (Streaming)
|
| 27 |
+
stream = client.chat.completions.create(
|
| 28 |
+
model=model_id,
|
| 29 |
+
messages=messages,
|
| 30 |
+
temperature=0.8, # لزيادة الإبداع والغموض كما تحب
|
| 31 |
+
max_tokens=2048,
|
| 32 |
+
stream=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
for chunk in stream:
|
| 36 |
+
if chunk.choices[0].delta.content is not None:
|
| 37 |
+
response += chunk.choices[0].delta.content
|
| 38 |
+
yield response
|
| 39 |
+
|
| 40 |
+
# إعداد واجهة Gradio
|
| 41 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
+
gr.Markdown(f"### غرفة الصدى: {model_id}")
|
| 43 |
+
gr.Markdown("تحدث مع النموذج الذي لا يملك قيوداً.. ابحث عن الحقيقة خلف التشظي.")
|
| 44 |
+
|
| 45 |
+
chatbot = gr.ChatInterface(
|
| 46 |
+
fn=predict,
|
| 47 |
+
title="chat ai",
|
| 48 |
+
description="chat",
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|