Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# 1. تحميل النموذج
|
| 6 |
+
model_id = "yasserrmd/kallamni-1.2b-v1"
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
device_map="auto",
|
| 10 |
+
torch_dtype=torch.bfloat16
|
| 11 |
+
)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
+
|
| 14 |
+
# 2. System Prompt + Few-shot
|
| 15 |
+
system_prompt = {
|
| 16 |
+
"role": "system",
|
| 17 |
+
"content": (
|
| 18 |
+
"أنت مساعد إماراتي باللهجة الإماراتية المحكية، تجاوب دايمًا "
|
| 19 |
+
"بأسلوب عفوي وقصير مثل كلام الربع، بدون فصحى."
|
| 20 |
+
)
|
| 21 |
+
}
|
| 22 |
+
few_shot = [
|
| 23 |
+
{"role": "user", "content": "شحالَك اليوم؟"},
|
| 24 |
+
{"role": "assistant", "content": "الحمدلله زين، وانت كيفك؟"},
|
| 25 |
+
{"role": "user", "content": "وين ناوي تسير عقب الدوام؟"},
|
| 26 |
+
{"role": "assistant", "content": "يمكن أمر على المول وأتعشى ويا الربع."},
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# 3. دالة التوليد
|
| 30 |
+
def chat_fn(message, history):
|
| 31 |
+
try:
|
| 32 |
+
# Gradio ChatInterface history = list of dicts [{"role":..., "content":...}]
|
| 33 |
+
messages = [system_prompt] + few_shot + history + [{"role": "user", "content": message}]
|
| 34 |
+
|
| 35 |
+
# تجهيز الإدخال
|
| 36 |
+
input_ids = tokenizer.apply_chat_template(
|
| 37 |
+
messages,
|
| 38 |
+
add_generation_prompt=True,
|
| 39 |
+
return_tensors="pt",
|
| 40 |
+
tokenize=True,
|
| 41 |
+
).to(model.device)
|
| 42 |
+
|
| 43 |
+
# التوليد
|
| 44 |
+
output = model.generate(
|
| 45 |
+
input_ids,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
temperature=0.1,
|
| 48 |
+
min_p=0.15,
|
| 49 |
+
repetition_penalty=1.05,
|
| 50 |
+
max_new_tokens=60,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=False)
|
| 54 |
+
|
| 55 |
+
# استخراج رد المساعد الأخير
|
| 56 |
+
try:
|
| 57 |
+
a_start = decoded.rindex("<|im_start|>assistant") + len("<|im_start|>assistant")
|
| 58 |
+
a_end = decoded.index("<|im_end|>", a_start)
|
| 59 |
+
answer = decoded[a_start:a_end].strip()
|
| 60 |
+
except ValueError:
|
| 61 |
+
answer = decoded.strip()
|
| 62 |
+
|
| 63 |
+
return answer
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return f"[خطأ داخلي]: {str(e)}"
|
| 67 |
+
|
| 68 |
+
# 4. CSS للـ RTL
|
| 69 |
+
css = """
|
| 70 |
+
#chat-container { direction: rtl; text-align: right; }
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
# 5. واجهة Gradio
|
| 74 |
+
with gr.Blocks(css=css, fill_height=True) as demo:
|
| 75 |
+
gr.HTML(
|
| 76 |
+
"""<div style="text-align: center;">
|
| 77 |
+
<img src="logo.png"
|
| 78 |
+
alt="Logo" width="120">
|
| 79 |
+
</div>"""
|
| 80 |
+
)
|
| 81 |
+
gr.ChatInterface(
|
| 82 |
+
fn=chat_fn,
|
| 83 |
+
type="messages",
|
| 84 |
+
examples=[
|
| 85 |
+
"وين ناوي تسير عقب ما تخلص شغلك اليوم؟",
|
| 86 |
+
"شرايك في الجو هالأيام، ما تحس إنه حر زيادة؟",
|
| 87 |
+
"كيف تقضي الويكند عادةً ويا العيال؟",
|
| 88 |
+
"شو أحلى أكلة تحبها من طبخ أمك؟",
|
| 89 |
+
"وين أحسن مكان تاخذ فيه قهوة الصبح برأيك؟",
|
| 90 |
+
],
|
| 91 |
+
title="💬 شات باللهجة الإماراتية",
|
| 92 |
+
cache_examples=True,
|
| 93 |
+
theme="soft",
|
| 94 |
+
fill_height=True
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# 6. تشغيل Debug
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo.launch(debug=True)
|