Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
-
import spaces
|
| 5 |
from threading import Thread
|
|
|
|
| 6 |
from huggingface_hub import login
|
| 7 |
-
import
|
| 8 |
|
| 9 |
# ======================================================
|
| 10 |
-
# تسجيل الدخول في حال
|
| 11 |
# ======================================================
|
| 12 |
if os.getenv("HF_TOKEN"):
|
| 13 |
login(token=os.getenv("HF_TOKEN"))
|
|
@@ -24,42 +24,29 @@ SYSTEM_PROMPT = (
|
|
| 24 |
"لا تستخدم مقدمات مثل (مرحباً أو بالتأكيد)، فقط الجواب المباشر."
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
| 27 |
# ======================================================
|
| 28 |
-
# تحميل الموديل
|
| 29 |
# ======================================================
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
from unsloth import FastLanguageModel
|
| 34 |
-
HAS_UNSLOTH = True
|
| 35 |
-
except ImportError:
|
| 36 |
-
HAS_UNSLOTH = False
|
| 37 |
-
|
| 38 |
-
if HAS_UNSLOTH:
|
| 39 |
-
print("🚀 Using Unsloth FastLanguageModel backend")
|
| 40 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 41 |
-
MODEL_ID,
|
| 42 |
-
dtype=torch.bfloat16,
|
| 43 |
-
load_in_4bit=True,
|
| 44 |
-
device_map="auto",
|
| 45 |
-
)
|
| 46 |
-
else:
|
| 47 |
-
print("⚙️ Using standard Transformers backend")
|
| 48 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 49 |
-
MODEL_ID,
|
| 50 |
-
torch_dtype=torch.bfloat16,
|
| 51 |
-
device_map="auto",
|
| 52 |
-
)
|
| 53 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
model.eval()
|
|
|
|
| 56 |
print("✅ Model ready!\n")
|
| 57 |
|
| 58 |
|
| 59 |
# ======================================================
|
| 60 |
-
# دالة المحادثة
|
| 61 |
# ======================================================
|
|
|
|
| 62 |
def chat(message, history):
|
|
|
|
| 63 |
messages = []
|
| 64 |
for msg in history:
|
| 65 |
if msg["role"] == "user":
|
|
@@ -67,15 +54,20 @@ def chat(message, history):
|
|
| 67 |
elif msg["role"] == "assistant":
|
| 68 |
messages.append({"role": "assistant", "content": msg["content"]})
|
| 69 |
|
|
|
|
| 70 |
messages.append({"role": "user", "content": f"{SYSTEM_PROMPT}\n\nالسؤال: {message}"})
|
| 71 |
|
|
|
|
| 72 |
input_ids = tokenizer.apply_chat_template(
|
| 73 |
messages,
|
| 74 |
return_tensors="pt",
|
| 75 |
-
add_generation_prompt=True
|
| 76 |
).to(model.device)
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
generation_kwargs = dict(
|
| 81 |
input_ids=input_ids,
|
|
@@ -87,6 +79,7 @@ def chat(message, history):
|
|
| 87 |
repetition_penalty=1.15,
|
| 88 |
)
|
| 89 |
|
|
|
|
| 90 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 91 |
thread.start()
|
| 92 |
|
|
@@ -104,12 +97,12 @@ def chat(message, history):
|
|
| 104 |
demo = gr.ChatInterface(
|
| 105 |
fn=chat,
|
| 106 |
type="messages",
|
| 107 |
-
title="🇮🇶 Gemma 3 Iraqi Chat",
|
| 108 |
description="""
|
| 109 |
-
**نموذج Gemma 3
|
| 110 |
-
|
| 111 |
|
| 112 |
-
🧠
|
| 113 |
- "شلونك اليوم؟"
|
| 114 |
- "اشرحلي شنو يعني تصعيد إداري"
|
| 115 |
- "وضحلي الفرق بين الدليل والعبرة"
|
|
|
|
| 1 |
+
import os
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 6 |
from huggingface_hub import login
|
| 7 |
+
import spaces
|
| 8 |
|
| 9 |
# ======================================================
|
| 10 |
+
# تسجيل الدخول في حال الموديل خاص
|
| 11 |
# ======================================================
|
| 12 |
if os.getenv("HF_TOKEN"):
|
| 13 |
login(token=os.getenv("HF_TOKEN"))
|
|
|
|
| 24 |
"لا تستخدم مقدمات مثل (مرحباً أو بالتأكيد)، فقط الجواب المباشر."
|
| 25 |
)
|
| 26 |
|
| 27 |
+
print(f"🔄 Loading model: {MODEL_ID}")
|
| 28 |
+
|
| 29 |
# ======================================================
|
| 30 |
+
# تحميل الموديل
|
| 31 |
# ======================================================
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
+
MODEL_ID,
|
| 36 |
+
torch_dtype=torch.bfloat16,
|
| 37 |
+
device_map="auto", # سيستخدم GPU عند تنفيذ الدالة أدناه
|
| 38 |
+
)
|
| 39 |
model.eval()
|
| 40 |
+
|
| 41 |
print("✅ Model ready!\n")
|
| 42 |
|
| 43 |
|
| 44 |
# ======================================================
|
| 45 |
+
# دالة المحادثة (تُنفذ داخل ZeroGPU)
|
| 46 |
# ======================================================
|
| 47 |
+
@spaces.GPU(duration=60)
|
| 48 |
def chat(message, history):
|
| 49 |
+
# تجهيز سجل المحادثة
|
| 50 |
messages = []
|
| 51 |
for msg in history:
|
| 52 |
if msg["role"] == "user":
|
|
|
|
| 54 |
elif msg["role"] == "assistant":
|
| 55 |
messages.append({"role": "assistant", "content": msg["content"]})
|
| 56 |
|
| 57 |
+
# إضافة السؤال الحالي مع الـ system prompt
|
| 58 |
messages.append({"role": "user", "content": f"{SYSTEM_PROMPT}\n\nالسؤال: {message}"})
|
| 59 |
|
| 60 |
+
# تجهيز الإدخال
|
| 61 |
input_ids = tokenizer.apply_chat_template(
|
| 62 |
messages,
|
| 63 |
return_tensors="pt",
|
| 64 |
+
add_generation_prompt=True,
|
| 65 |
).to(model.device)
|
| 66 |
|
| 67 |
+
# إعداد البث المباشر للنص
|
| 68 |
+
streamer = TextIteratorStreamer(
|
| 69 |
+
tokenizer, skip_prompt=True, skip_special_tokens=True
|
| 70 |
+
)
|
| 71 |
|
| 72 |
generation_kwargs = dict(
|
| 73 |
input_ids=input_ids,
|
|
|
|
| 79 |
repetition_penalty=1.15,
|
| 80 |
)
|
| 81 |
|
| 82 |
+
# إنشاء Thread للبث التدريجي
|
| 83 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 84 |
thread.start()
|
| 85 |
|
|
|
|
| 97 |
demo = gr.ChatInterface(
|
| 98 |
fn=chat,
|
| 99 |
type="messages",
|
| 100 |
+
title="🇮🇶 Gemma 3 Iraqi Chat – ZeroGPU Edition",
|
| 101 |
description="""
|
| 102 |
+
**نموذج Gemma 3 Iraqi 🇮🇶**
|
| 103 |
+
مبني ومهيأ للحوار باللهجة العراقية والعربية الفصحى.
|
| 104 |
|
| 105 |
+
🧠 أمثلة جاهزة:
|
| 106 |
- "شلونك اليوم؟"
|
| 107 |
- "اشرحلي شنو يعني تصعيد إداري"
|
| 108 |
- "وضحلي الفرق بين الدليل والعبرة"
|