Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,38 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
-
"""
|
| 4 |
-
Gemma-3 Iraqi Arabic Chat App
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import os
|
| 8 |
import torch
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
import gradio as gr
|
|
|
|
| 11 |
|
| 12 |
MODEL_NAME = "anaspro/gemma3-iraqi"
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
tokenizer =
|
| 17 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
-
MODEL_NAME,
|
| 19 |
-
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 20 |
-
device_map="auto",
|
| 21 |
-
)
|
| 22 |
-
print("✅ Model loaded and ready!")
|
| 23 |
|
| 24 |
def chat_fn(user_message, history):
|
| 25 |
-
# سجل رسالة المستخدم
|
| 26 |
history = history or []
|
| 27 |
history.append((user_message, None))
|
| 28 |
-
|
| 29 |
-
# إعداد الإدخال
|
| 30 |
inputs = tokenizer.apply_chat_template(
|
| 31 |
[{"role": "user", "content": user_message}],
|
| 32 |
tokenize=True,
|
| 33 |
add_generation_prompt=True,
|
| 34 |
return_tensors="pt"
|
| 35 |
).to(model.device)
|
| 36 |
-
|
| 37 |
-
# توليد الرد
|
| 38 |
outputs = model.generate(
|
| 39 |
**inputs,
|
| 40 |
max_new_tokens=1024,
|
|
@@ -45,22 +43,25 @@ def chat_fn(user_message, history):
|
|
| 45 |
eos_token_id=tokenizer.eos_token_id,
|
| 46 |
)
|
| 47 |
resp = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 48 |
-
|
| 49 |
-
# اقتطاع الجزء بعد “assistant” إذا موجود
|
| 50 |
if "assistant" in resp:
|
| 51 |
resp = resp.split("assistant")[-1].strip()
|
| 52 |
-
|
| 53 |
-
# سجل الرد
|
| 54 |
history[-1] = (user_message, resp)
|
| 55 |
return history, history
|
| 56 |
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
-
gr.Markdown("##
|
| 59 |
-
chatbot = gr.Chatbot(label="Chatbot")
|
| 60 |
-
msg = gr.Textbox(placeholder="اكتب
|
| 61 |
-
clear = gr.Button("🧹 مسح
|
| 62 |
-
|
| 63 |
msg.submit(chat_fn, [msg, chatbot], [chatbot, chatbot])
|
| 64 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
import gradio as gr
|
| 6 |
+
import spaces # مهم جداً في Hugging Face Spaces
|
| 7 |
|
| 8 |
MODEL_NAME = "anaspro/gemma3-iraqi"
|
| 9 |
|
| 10 |
+
# نحدد أن هذا التطبيق يحتاج GPU لتشغيله
|
| 11 |
+
@spaces.GPU
|
| 12 |
+
def load_model():
|
| 13 |
+
print("🔐 Loading model:", MODEL_NAME)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
MODEL_NAME,
|
| 17 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 18 |
+
device_map="auto",
|
| 19 |
+
)
|
| 20 |
+
print("✅ Model loaded and ready!")
|
| 21 |
+
return tokenizer, model
|
| 22 |
|
| 23 |
+
tokenizer, model = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def chat_fn(user_message, history):
|
|
|
|
| 26 |
history = history or []
|
| 27 |
history.append((user_message, None))
|
| 28 |
+
|
|
|
|
| 29 |
inputs = tokenizer.apply_chat_template(
|
| 30 |
[{"role": "user", "content": user_message}],
|
| 31 |
tokenize=True,
|
| 32 |
add_generation_prompt=True,
|
| 33 |
return_tensors="pt"
|
| 34 |
).to(model.device)
|
| 35 |
+
|
|
|
|
| 36 |
outputs = model.generate(
|
| 37 |
**inputs,
|
| 38 |
max_new_tokens=1024,
|
|
|
|
| 43 |
eos_token_id=tokenizer.eos_token_id,
|
| 44 |
)
|
| 45 |
resp = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 46 |
if "assistant" in resp:
|
| 47 |
resp = resp.split("assistant")[-1].strip()
|
| 48 |
+
|
|
|
|
| 49 |
history[-1] = (user_message, resp)
|
| 50 |
return history, history
|
| 51 |
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
+
gr.Markdown("## 🧠 Chat with Iraqi Gemma 3 – (GPU Optimized)")
|
| 54 |
+
chatbot = gr.Chatbot(label="💬 Chatbot", type="messages")
|
| 55 |
+
msg = gr.Textbox(placeholder="اكتب باللهجة العراقية...")
|
| 56 |
+
clear = gr.Button("🧹 مسح")
|
| 57 |
+
|
| 58 |
msg.submit(chat_fn, [msg, chatbot], [chatbot, chatbot])
|
| 59 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 60 |
+
|
| 61 |
+
# Spaces يعرف أن نقطة البداية هي GPU
|
| 62 |
+
@spaces.GPU
|
| 63 |
+
def main():
|
| 64 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
main()
|