rinrikatoki commited on
Commit
c8b0b86
·
verified ·
1 Parent(s): ea2466c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -43
app.py CHANGED
@@ -1,62 +1,54 @@
1
  import os
2
  import zipfile
3
- import torch
4
- import gradio as gr
5
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
6
- from peft import PeftModel
7
  from huggingface_hub import login
 
 
 
8
 
9
-
10
- # --- گام ۱: احراز هویت Hugging Face
11
  hf_token = os.environ.get("HF_TOKEN")
12
  if not hf_token:
13
  raise ValueError("❌ HF_TOKEN not found in environment secrets.")
 
 
14
  login(hf_token)
15
 
16
- # اگر فایل اشتباه وجود داره و فایل جدید نه
17
- if os.path.exists("dorna-diabetes-finetuned-20250514T183411Z-1-001.zip") and not os.path.exists("dorna-diabetes-finetuned.zip"):
18
- os.rename("dorna-diabetes-finetuned-20250514T183411Z-1-001.zip", "dorna-diabetes-finetuned.zip")
19
- print("✅ اسم فایل تغییر کرد.")
20
 
21
- # --- گام ۲: اکسترکت فایل فشرده (فقط بار اول)
22
- if not os.path.exists("dorna-diabetes-finetuned"):
23
- with zipfile.ZipFile("dorna-diabetes-finetuned.zip", "r") as zip_ref:
24
- zip_ref.extractall(".")
25
  print("✅ فایل LoRA اکسترکت شد.")
26
 
27
- # --- گام ۳: بارگذاری مدل پایه و LoRA
28
- BASE_MODEL = "PartAI/Dorna-Llama3-8B-Instruct"
29
- LORA_PATH = "./dorna-diabetes-finetuned"
 
 
 
 
 
 
30
 
31
- print("🔹 در حال بارگذاری مدل پایه...")
32
- tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_auth_token=hf_token)
33
- base_model = AutoModelForCausalLM.from_pretrained(
34
- BASE_MODEL,
 
 
 
35
  device_map="auto",
36
  token=hf_token,
37
  trust_remote_code=True
38
  )
39
 
40
- print("🔹 در حال بارگذاری LoRA...")
41
- model = PeftModel.from_pretrained(base_model, LORA_PATH)
42
- model.eval()
43
-
44
- streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
45
-
46
- # --- گام ۴: رابط چت با Gradio
47
- def chat(prompt):
48
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids.cuda()
49
- with torch.no_grad():
50
- output = model.generate(
51
- input_ids=input_ids,
52
- max_new_tokens=200,
53
- temperature=0.7,
54
- top_p=0.9,
55
- do_sample=True
56
- )
57
- response = tokenizer.decode(output[0], skip_special_tokens=True)
58
- return response[len(prompt):].strip()
59
-
60
- iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="💬 Dorna LoRA Model")
61
 
62
- iface.launch()
 
1
  import os
2
  import zipfile
 
 
 
 
3
  from huggingface_hub import login
4
+ import torch
5
+ from transformers import AutoTokenizer
6
+ from peft import AutoPeftModelForCausalLM
7
 
8
+ # 🔹 گرفتن توکن از محیط
 
9
  hf_token = os.environ.get("HF_TOKEN")
10
  if not hf_token:
11
  raise ValueError("❌ HF_TOKEN not found in environment secrets.")
12
+
13
+ # 🔹 لاگین به HuggingFace
14
  login(hf_token)
15
 
16
+ # 🔹 تنظیم مسیرها
17
+ LORA_ZIP_PATH = "dorna-diabetes-finetuned-20250514T183411Z-1-001.zip" # نام فایل زیپ که آپلود کردی
18
+ LORA_PATH = "./dorna-diabetes-finetuned"
 
19
 
20
+ # اکسترکت کردن فایل LoRA
21
+ if not os.path.exists(LORA_PATH):
22
+ with zipfile.ZipFile(LORA_ZIP_PATH, 'r') as zip_ref:
23
+ zip_ref.extractall(LORA_PATH)
24
  print("✅ فایل LoRA اکسترکت شد.")
25
 
26
+ # تغییر نام فایل .safetensors به adapter_model.safetensors
27
+ for filename in os.listdir(LORA_PATH):
28
+ if filename.endswith(".safetensors") and filename != "adapter_model.safetensors":
29
+ os.rename(
30
+ os.path.join(LORA_PATH, filename),
31
+ os.path.join(LORA_PATH, "adapter_model.safetensors")
32
+ )
33
+ print("✅ اسم فایل تغییر کرد.")
34
+ break
35
 
36
+ # 🔹 بارگذاری مدل و توکنایزر
37
+ print("🔹 در حال بارگذاری مدل پایه + LoRA...")
38
+
39
+ model = AutoPeftModelForCausalLM.from_pretrained(
40
+ LORA_PATH,
41
+ torch_dtype=torch.float16,
42
+ low_cpu_mem_usage=True,
43
  device_map="auto",
44
  token=hf_token,
45
  trust_remote_code=True
46
  )
47
 
48
+ tokenizer = AutoTokenizer.from_pretrained(
49
+ model.base_model.config._name_or_path,
50
+ token=hf_token,
51
+ trust_remote_code=True
52
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ print("✅ مدل و توکنایزر با موفقیت بارگذاری شدند.")