Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. تحديث وتثبيت المكتبات لضمان التوافق
|
| 2 |
+
!pip install -q -U transformers accelerate bitsandbytes gradio
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# 2. إعداد النموذج (Qwen 2.5 - 1.5B)
|
| 9 |
+
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 10 |
+
|
| 11 |
+
# إعدادات الـ 4-bit لتقليل استهلاك الذاكرة
|
| 12 |
+
bnb_config = BitsAndBytesConfig(
|
| 13 |
+
load_in_4bit=True,
|
| 14 |
+
bnb_4bit_use_double_quant=True,
|
| 15 |
+
bnb_4bit_quant_type="nf4",
|
| 16 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
print("⏳ جاري تشغيل Zeus... الرجاء الانتظار قليلاً...")
|
| 20 |
+
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
quantization_config=bnb_config,
|
| 25 |
+
device_map="auto"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# 3. شخصية Zeus
|
| 29 |
+
system_prompt = """أنت Zeus، مساعد ذكي وودود. تتحدث العربية بطلاقة وتفهم اللهجات.
|
| 30 |
+
أنت خبير تقني وتقدم إجابات منظمة وواضحة جداً بالنقاط."""
|
| 31 |
+
|
| 32 |
+
# 4. دالة المعالجة (تعديل طفيف لتتوافق مع نظام الرسائل الجديد)
|
| 33 |
+
def chat_with_zeus(message, history):
|
| 34 |
+
# تحويل التاريخ إلى تنسيق Qwen
|
| 35 |
+
full_history = [{"role": "system", "content": system_prompt}]
|
| 36 |
+
for h in history:
|
| 37 |
+
full_history.append({"role": "user", "content": h[0]})
|
| 38 |
+
full_history.append({"role": "assistant", "content": h[1]})
|
| 39 |
+
|
| 40 |
+
full_history.append({"role": "user", "content": message})
|
| 41 |
+
|
| 42 |
+
# تحويل الرسائل لنص يفهمه النموذج
|
| 43 |
+
text = tokenizer.apply_chat_template(full_history, tokenize=False, add_generation_prompt=True)
|
| 44 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 45 |
+
|
| 46 |
+
# التوليد
|
| 47 |
+
generated_ids = model.generate(**model_inputs, max_new_tokens=512, temperature=0.7, do_sample=True)
|
| 48 |
+
|
| 49 |
+
# استخراج الرد
|
| 50 |
+
response_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
|
| 51 |
+
return tokenizer.batch_decode(response_ids, skip_special_tokens=True)[0]
|
| 52 |
+
|
| 53 |
+
# 5. الواجهة الرسومية (الإصدار المتوافق والمبسط)
|
| 54 |
+
# قمنا بإزالة الوسائط التي تسبب أخطاء واستخدمنا الوضع القياسي المستقر
|
| 55 |
+
demo = gr.ChatInterface(
|
| 56 |
+
fn=chat_with_zeus,
|
| 57 |
+
title="⚡ Zeus AI Assistant",
|
| 58 |
+
description="مرحباً بك! أنا زيوس، مساعدك الذكي. كيف يمكنني مساعدتك اليوم؟",
|
| 59 |
+
examples=["من أنت؟", "اشرح لي الذكاء الاصطناعي ببساطة", "كيف أتعلم البرمجة؟"],
|
| 60 |
+
cache_examples=False,
|
| 61 |
+
# لاحظ أننا لم نضف retry_btn هنا لتجنب خطأ الإصدارات
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# 6. التشغيل
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch(share=True)
|