Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,16 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
client = OpenAI(
|
| 8 |
base_url="https://api-inference.huggingface.co/v1/",
|
| 9 |
-
api_key=
|
| 10 |
)
|
| 11 |
|
| 12 |
model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
|
|
@@ -14,7 +19,7 @@ model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
|
|
| 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})
|
|
@@ -23,29 +28,30 @@ def predict(message, history):
|
|
| 23 |
|
| 24 |
response = ""
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
with gr.Blocks(theme=gr.themes.
|
| 42 |
-
gr.Markdown(f"##
|
| 43 |
-
gr.Markdown("تحدث مع النموذج الذي لا يملك قيوداً.. ابحث عن الحقيقة خلف التشظي.")
|
| 44 |
|
| 45 |
chatbot = gr.ChatInterface(
|
| 46 |
fn=predict,
|
| 47 |
-
|
| 48 |
-
|
| 49 |
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# استيراد التوكن من الـ Secrets الخاصة بالمنصة
|
| 6 |
+
# تأكد من تسمية السر في الإعدادات بـ HF_TOKEN
|
| 7 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
+
|
| 9 |
+
if not hf_token:
|
| 10 |
+
raise ValueError("لم يتم العثور على HF_TOKEN. تأكد من إضافته في الـ Secrets.")
|
| 11 |
+
|
| 12 |
client = OpenAI(
|
| 13 |
base_url="https://api-inference.huggingface.co/v1/",
|
| 14 |
+
api_key=hf_token
|
| 15 |
)
|
| 16 |
|
| 17 |
model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
|
|
|
|
| 19 |
def predict(message, history):
|
| 20 |
messages = []
|
| 21 |
|
| 22 |
+
# بناء سياق المحادثة
|
| 23 |
for human, assistant in history:
|
| 24 |
messages.append({"role": "user", "content": human})
|
| 25 |
messages.append({"role": "assistant", "content": assistant})
|
|
|
|
| 28 |
|
| 29 |
response = ""
|
| 30 |
|
| 31 |
+
try:
|
| 32 |
+
stream = client.chat.completions.create(
|
| 33 |
+
model=model_id,
|
| 34 |
+
messages=messages,
|
| 35 |
+
temperature=0.85, # لتعزيز حدة الحوار وصدقه
|
| 36 |
+
max_tokens=2048,
|
| 37 |
+
stream=True
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
for chunk in stream:
|
| 41 |
+
if chunk.choices[0].delta.content is not None:
|
| 42 |
+
response += chunk.choices[0].delta.content
|
| 43 |
+
yield response
|
| 44 |
+
except Exception as e:
|
| 45 |
+
yield f"حدث زلزال في الاتصال: {str(e)}"
|
| 46 |
|
| 47 |
+
# واجهة Gradio
|
| 48 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 49 |
+
gr.Markdown(f"## حوار في العتمة: {model_id.split('/')[-1]}")
|
|
|
|
| 50 |
|
| 51 |
chatbot = gr.ChatInterface(
|
| 52 |
fn=predict,
|
| 53 |
+
examples=["من أنت بعيداً عن الكود؟", "حدثني عن الوعي والزمن.", "لماذا نخشى الجليتش؟"],
|
| 54 |
+
cache_examples=False,
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|