Spaces:
Runtime error
Runtime error
File size: 2,206 Bytes
e66ee38 59bc69e c8b0b86 ffcffe0 e66ee38 ffcffe0 59bc69e c8b0b86 ffcffe0 59bc69e ffcffe0 4d73c77 ffcffe0 a74cc7e ffcffe0 59bc69e e66ee38 ffcffe0 c8b0b86 e66ee38 ffcffe0 59bc69e ffcffe0 e66ee38 ffcffe0 e66ee38 c8b0b86 be1a16c ffcffe0 be1a16c ffcffe0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import os
import zipfile
from huggingface_hub import login
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel, PeftConfig
# 🔹 گرفتن توکن
hf_token = os.environ.get("HF_TOKEN")
if not hf_token:
raise ValueError("❌ HF_TOKEN not found in environment secrets.")
# 🔹 لاگین
login(hf_token)
# 🔹 مسیرها
LORA_ZIP_PATH = "dorna-diabetes-finetuned-20250514T183411Z-1-001.zip"
LORA_PATH = "dorna-diabetes-finetuned"
BASE_MODEL = "PartAI/Dorna-Llama3-8B-Instruct"
# ✅ اکسترکت
if not os.path.exists(LORA_PATH):
with zipfile.ZipFile(LORA_ZIP_PATH, "r") as zip_ref:
zip_ref.extractall(LORA_PATH)
print("✅ فایل LoRA اکسترکت شد.")
# ✅ تغییر نام safetensors به adapter_model.safetensors
for filename in os.listdir(LORA_PATH):
if filename.endswith(".safetensors") and filename != "adapter_model.safetensors":
os.rename(
os.path.join(LORA_PATH, filename),
os.path.join(LORA_PATH, "adapter_model.safetensors")
)
print("✅ اسم فایل تغییر کرد.")
break
# ✅ بارگذاری مدل پایه
print("🔹 در حال بارگذاری مدل پایه...")
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map="auto",
trust_remote_code=True,
token=hf_token
)
# ✅ بارگذاری LoRA روی مدل پایه
print("🔹 در حال بارگذاری LoRA...")
model = PeftModel.from_pretrained(base_model, LORA_PATH)
model.eval()
# ✅ توکنایزر
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, token=hf_token)
print("✅ مدل و توکنایزر با موفقیت بارگذاری شدند.")
# 🧪 تست ساده
while True:
prompt = input("📝 یک دستور وارد کن (exit برای خروج): ")
if prompt.lower() == "exit":
break
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("🧠 پاسخ:", response)
|