Spaces:
Runtime error
Runtime error
Update app.py
Browse files2 unmodified lines3456789101112133 unmodified lines17181920212223245 unmodified lines3031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182832 unmodified linesfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipelineimport tracebackMODEL_NAME = "Qwen/Qwen2.5-3B-Instruct"SYSTEM_PROMPT = "أنت مساعد عربي ذكي، تجيب بوضوح وبأسلوب بسيط ومفيد."# ---------- تحميل الموديل ----------print("🔄 Loading tokenizer...")tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME,3 unmodified linesprint("🔄 Loading model (CPU)...")model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,torch_dtype=torch.float32,device_map="cpu",trust_remote_code=True)5 unmodified linestokenizer=tokenizer)# ---------- تنسيق البرومبت ----------def build_prompt(history, user_message):prompt = SYSTEM_PROMPT + "\n\n"for user, bot in history:prompt += f"المستخدم: {user}\n"prompt += f"المساعد: {bot}\n"prompt += f"المستخدم: {user_message}\nالمساعد:"return prompt# ---------- دالة الرد ----------def chat(user_message, history):if not user_message.strip():return history, "⚠️ اكتب رسالة"try:prompt = build_prompt(history, user_message)output = generator(prompt,max_new_tokens=256,temperature=0.7,top_p=0.9,do_sample=True)full_text = output[0]["generated_text"]response = full_text.split("المساعد:")[-1].strip()history.append((user_message, response))return history, ""except Exception as e:error_msg = f"❌ Error:\n{str(e)}\n\n{traceback.format_exc()}"history.append((user_message, error_msg))return history, ""# ---------- واجهة Gradio ----------with gr.Blocks(title="Qwen2.5 Arabic Chatbot") as demo:gr.Markdown("# 🤖 Qwen2.5 Arabic Chatbot")gr.Markdown("نموذج Qwen2.5 يعمل بدون messages وبدون API")chatbot = gr.Chatbot(height=450)msg = gr.Textbox(label="رسالتك", placeholder="اكتب سؤالك هنا...")clear = gr.Button("🗑️ مسح المحادثة")msg.submit(chat, [msg, chatbot], [chatbot, msg])clear.click(lambda: [], None, chatbot)demo.launch()2 unmodified lines3456789101112133 unmodified lines1718192021222324255 unmodified lines313233343536373839404142434445464748495051525354555657585960616263646566672 unmodified linesfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipelineimport traceback# 👈 استخدم موديلك انتMODEL_NAME = "kawkabelaloom/astramindx"SYSTEM_PROMPT = "أنت مساعد عربي ذكي، تجيب بوضوح وبأسلوب بسيط ومفيد."print("🔄 Loading tokenizer...")tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME,3 unmodified linesprint("🔄 Loading model (CPU)...")model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,device_map="cpu",torch_dtype=torch.float32,low_cpu_mem_usage=True,trust_remote_code=True)5 unmodified linestokenizer=tokenizer)def build_prompt(history, user_message):prompt = SYSTEM_PROMPT + "\n\n"for user, bot in history:prompt += f"المستخدم: {user}\nالمساعد: {bot}\n"prompt += f"المستخدم: {user_message}\nالمساعد:"return promptdef chat(user_message, history):try:prompt = build_prompt(history, user_message)output = generator(prompt,max_new_tokens=128, # 👈 قللها للسرعةtemperature=0.7,top_p=0.9,do_sample=True)response = output[0]["generated_text"].split("المساعد:")[-1].strip()history.append((user_message, response))return history, ""except Exception as e:history.append((user_message, str(e)))return history, ""with gr.Blocks() as demo:gr.Markdown("# 🤖 Astramindx Chatbot")chatbot = gr.Chatbot(height=450)msg = gr.Textbox()msg.submit(chat, [msg, chatbot], [chatbot, msg])demo.launch()
app.py+11-27