| import os
|
| import torch
|
| from transformers import PreTrainedTokenizerFast, GPT2LMHeadModel
|
|
|
| class ModelRunner:
|
| def __init__(self, model_dir="./math_gpt3_final"):
|
| """
|
| تهيئة مفسر الرموز والنموذج من المجلد المحفوظ.
|
| """
|
| if not os.path.exists(model_dir):
|
| raise FileNotFoundError(f"المجلد {model_dir} غير موجود. تأكد من مسار الحفظ الصحيح.")
|
|
|
| print(f"جاري تحميل النموذج من: {model_dir} ...")
|
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
| self.tokenizer = PreTrainedTokenizerFast.from_pretrained(model_dir)
|
| self.model = GPT2LMHeadModel.from_pretrained(model_dir).to(self.device)
|
| self.model.eval()
|
|
|
| print(f"تم تحميل النموذج بنجاح على جهاز: {self.device}\n")
|
|
|
| def predict(self, prompt, max_new_tokens=1024, temperature=0.6):
|
| """
|
| تأخذ السؤال وتُرجع الحل فقط بدون تكرار السؤال.
|
| """
|
|
|
| if "<ans>" not in prompt:
|
| prompt = prompt + "<ans>"
|
|
|
| input_ids = self.tokenizer.encode(prompt, return_tensors="pt").to(self.device)
|
|
|
|
|
|
|
| with torch.no_grad():
|
| output_ids = self.model.generate(
|
| input_ids=input_ids,
|
| max_new_tokens=max_new_tokens,
|
| do_sample=True if temperature > 0 else False,
|
| temperature=temperature if temperature > 0 else None,
|
| eos_token_id=self.tokenizer.eos_token_id,
|
| pad_token_id=self.tokenizer.pad_token_id,
|
| top_p=0.9 if temperature > 0 else None
|
| )
|
|
|
|
|
| full_tokens = self.tokenizer.convert_ids_to_tokens(output_ids[0])
|
| full_text = "".join(full_tokens)
|
|
|
|
|
| if "<ans>" in full_text:
|
| answer = full_text.split("<ans>")[1]
|
| else:
|
| answer = full_text
|
|
|
|
|
| answer = answer.replace("<eos>", "").replace("<pad>", "").strip()
|
| return answer
|
|
|
|
|
|
|
|
|
| if __name__ == "__main__":
|
|
|
|
|
| try:
|
| runner = ModelRunner(model_dir="./math_gpt3_final")
|
|
|
| print("--- برنامج حل المعادلات من الدرجة الثانية ---")
|
| print("اكتب 'exit' للخروج من البرنامج.\n")
|
|
|
| while True:
|
| user_input = input("أدخل المعادلة (مثال: 1x²-5x+6=0): ")
|
| if user_input.lower() == 'exit':
|
| break
|
|
|
| if not user_input.strip():
|
| continue
|
|
|
|
|
| solution = runner.predict(user_input, temperature=0.1)
|
| print(f"🤖 الحل المشتق من النموذج:\n{solution}")
|
| print("-" * 40)
|
|
|
| except Exception as e:
|
| print(f"حدث خطأ: {e}") |