Update app. py
Browse files
app. py
CHANGED
|
@@ -1,54 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
# تحدي
|
| 6 |
-
model_name = "google/
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
model_name,
|
| 13 |
-
torch_dtype=torch.
|
| 14 |
-
device_map="auto"
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
#
|
| 19 |
messages = []
|
| 20 |
-
for
|
| 21 |
-
messages.append({"role": "user", "content":
|
| 22 |
-
messages.append({"role": "assistant", "content":
|
| 23 |
|
| 24 |
-
# 2. إضافة الرسالة الجديدة للمستخدم
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
-
#
|
| 28 |
input_ids = tokenizer.apply_chat_template(
|
| 29 |
messages,
|
| 30 |
add_generation_prompt=True,
|
| 31 |
return_tensors="pt"
|
| 32 |
).to(model.device)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
outputs = model.generate(
|
| 36 |
input_ids,
|
| 37 |
max_new_tokens=512,
|
| 38 |
do_sample=True,
|
| 39 |
temperature=0.7,
|
| 40 |
-
top_p=0.9
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
|
| 46 |
-
|
| 47 |
return response
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
gr.ChatInterface(
|
| 51 |
-
|
| 52 |
-
title="Gemma
|
| 53 |
-
description="ر
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# 1. تحديد النموذج (استخدمنا 2b لأنه الأنسب للمساحات المجانية وسريع)
|
| 7 |
+
model_name = "google/gemma-2-2b-it"
|
| 8 |
|
| 9 |
+
# 2. الحصول على التوكن من إعدادات المساحة (Secrets)
|
| 10 |
+
token = os.getenv("HF_TOKEN")
|
| 11 |
+
|
| 12 |
+
# 3. تحميل المحلل اللفظي والنموذج
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=token)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
model_name,
|
| 16 |
+
torch_dtype=torch.bfloat16, # أفضل للأداء والدقة
|
| 17 |
+
device_map="auto",
|
| 18 |
+
token=token
|
| 19 |
)
|
| 20 |
|
| 21 |
+
def chat_function(message, history):
|
| 22 |
+
# بناء سجل المحادثة بتنسيق Gemma
|
| 23 |
messages = []
|
| 24 |
+
for user_msg, assistant_msg in history:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 27 |
|
|
|
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
+
# تجهيز المدخلات باستخدام Template النموذج الرسمي
|
| 31 |
input_ids = tokenizer.apply_chat_template(
|
| 32 |
messages,
|
| 33 |
add_generation_prompt=True,
|
| 34 |
return_tensors="pt"
|
| 35 |
).to(model.device)
|
| 36 |
|
| 37 |
+
# توليد الرد
|
| 38 |
outputs = model.generate(
|
| 39 |
input_ids,
|
| 40 |
max_new_tokens=512,
|
| 41 |
do_sample=True,
|
| 42 |
temperature=0.7,
|
| 43 |
+
top_p=0.9,
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# فك التشفير واستخراج النص الجديد فقط
|
| 47 |
+
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
|
|
|
|
|
|
| 48 |
return response
|
| 49 |
|
| 50 |
+
# 4. إنشاء واجهة الدردشة
|
| 51 |
+
demo = gr.ChatInterface(
|
| 52 |
+
fn=chat_function,
|
| 53 |
+
title="Gemma 2 Chatbot",
|
| 54 |
+
description="دردشة مباشرة مع نموذج Gemma من جوجل على Hugging Face Spaces",
|
| 55 |
+
examples=["كيف حالك؟", "اشرح لي الثقوب السوداء ببساطة", "اكتب قصيدة قصيرة عن الذكاء الاصطناعي"],
|
| 56 |
+
cache_examples=False,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|