Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ import torch
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
import traceback
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
SYSTEM_PROMPT = "أنت مساعد عربي ذكي، تجيب بوضوح وبأسلوب بسيط ومفيد."
|
| 9 |
|
| 10 |
-
# ---------- تحميل الموديل ----------
|
| 11 |
print("🔄 Loading tokenizer...")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
MODEL_NAME,
|
|
@@ -17,8 +17,9 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 17 |
print("🔄 Loading model (CPU)...")
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
MODEL_NAME,
|
| 20 |
-
torch_dtype=torch.float32,
|
| 21 |
device_map="cpu",
|
|
|
|
|
|
|
| 22 |
trust_remote_code=True
|
| 23 |
)
|
| 24 |
|
|
@@ -30,54 +31,37 @@ generator = pipeline(
|
|
| 30 |
tokenizer=tokenizer
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# ---------- تنسيق البرومبت ----------
|
| 34 |
def build_prompt(history, user_message):
|
| 35 |
prompt = SYSTEM_PROMPT + "\n\n"
|
| 36 |
for user, bot in history:
|
| 37 |
-
prompt += f"المستخدم: {user}\n"
|
| 38 |
-
prompt += f"المساعد: {bot}\n"
|
| 39 |
prompt += f"المستخدم: {user_message}\nالمساعد:"
|
| 40 |
return prompt
|
| 41 |
|
| 42 |
-
|
| 43 |
-
# ---------- دالة الرد ----------
|
| 44 |
def chat(user_message, history):
|
| 45 |
-
if not user_message.strip():
|
| 46 |
-
return history, "⚠️ اكتب رسالة"
|
| 47 |
-
|
| 48 |
try:
|
| 49 |
prompt = build_prompt(history, user_message)
|
| 50 |
|
| 51 |
output = generator(
|
| 52 |
prompt,
|
| 53 |
-
max_new_tokens=
|
| 54 |
temperature=0.7,
|
| 55 |
top_p=0.9,
|
| 56 |
do_sample=True
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
response = full_text.split("المساعد:")[-1].strip()
|
| 61 |
-
|
| 62 |
history.append((user_message, response))
|
| 63 |
return history, ""
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
-
history.append((user_message, error_msg))
|
| 68 |
return history, ""
|
| 69 |
|
| 70 |
-
|
| 71 |
-
#
|
| 72 |
-
with gr.Blocks(title="Qwen2.5 Arabic Chatbot") as demo:
|
| 73 |
-
gr.Markdown("# 🤖 Qwen2.5 Arabic Chatbot")
|
| 74 |
-
gr.Markdown("نموذج Qwen2.5 يعمل بدون messages وبدون API")
|
| 75 |
-
|
| 76 |
chatbot = gr.Chatbot(height=450)
|
| 77 |
-
msg = gr.Textbox(
|
| 78 |
-
clear = gr.Button("🗑️ مسح المحادثة")
|
| 79 |
-
|
| 80 |
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
| 81 |
-
clear.click(lambda: [], None, chatbot)
|
| 82 |
|
| 83 |
demo.launch()
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
import traceback
|
| 5 |
|
| 6 |
+
# 👈 استخدم موديلك انت
|
| 7 |
+
MODEL_NAME = "kawkabelaloom/astramindx"
|
| 8 |
|
| 9 |
SYSTEM_PROMPT = "أنت مساعد عربي ذكي، تجيب بوضوح وبأسلوب بسيط ومفيد."
|
| 10 |
|
|
|
|
| 11 |
print("🔄 Loading tokenizer...")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
MODEL_NAME,
|
|
|
|
| 17 |
print("🔄 Loading model (CPU)...")
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
MODEL_NAME,
|
|
|
|
| 20 |
device_map="cpu",
|
| 21 |
+
torch_dtype=torch.float32,
|
| 22 |
+
low_cpu_mem_usage=True,
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
| 25 |
|
|
|
|
| 31 |
tokenizer=tokenizer
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
def build_prompt(history, user_message):
|
| 35 |
prompt = SYSTEM_PROMPT + "\n\n"
|
| 36 |
for user, bot in history:
|
| 37 |
+
prompt += f"المستخدم: {user}\nالمساعد: {bot}\n"
|
|
|
|
| 38 |
prompt += f"المستخدم: {user_message}\nالمساعد:"
|
| 39 |
return prompt
|
| 40 |
|
|
|
|
|
|
|
| 41 |
def chat(user_message, history):
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
prompt = build_prompt(history, user_message)
|
| 44 |
|
| 45 |
output = generator(
|
| 46 |
prompt,
|
| 47 |
+
max_new_tokens=128, # 👈 قللها للسرعة
|
| 48 |
temperature=0.7,
|
| 49 |
top_p=0.9,
|
| 50 |
do_sample=True
|
| 51 |
)
|
| 52 |
|
| 53 |
+
response = output[0]["generated_text"].split("المساعد:")[-1].strip()
|
|
|
|
|
|
|
| 54 |
history.append((user_message, response))
|
| 55 |
return history, ""
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
history.append((user_message, str(e)))
|
|
|
|
| 59 |
return history, ""
|
| 60 |
|
| 61 |
+
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# 🤖 Astramindx Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
chatbot = gr.Chatbot(height=450)
|
| 64 |
+
msg = gr.Textbox()
|
|
|
|
|
|
|
| 65 |
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
|
|
|
| 66 |
|
| 67 |
demo.launch()
|