Update app.py
Browse files
app.py
CHANGED
@@ -12,31 +12,9 @@ try:
|
|
12 |
except Exception as e:
|
13 |
print(f"Failed to install packages: {e}")
|
14 |
|
15 |
-
|
16 |
-
###
|
17 |
-
# # เพิ่มบรรทัดนี้ที่ต้นโค้ด ก่อน import torch
|
18 |
-
# import os
|
19 |
-
# # ตั้งค่า env variables ทั้งหมดก่อน import torch
|
20 |
-
# os.environ['TORCH_LOGS'] = '+dynamo'
|
21 |
-
# os.environ['TORCHDYNAMO_VERBOSE'] = '1'
|
22 |
-
# os.environ['TORCH_INDUCTOR_BACKEND'] = 'CUDA'
|
23 |
-
# # os.environ['NVIDIA_VISIBLE_DEVICES'] = '' # อาจต้องตรวจสอบว่าจำเป็นไหม
|
24 |
-
|
25 |
-
# os.environ['TORCHDYNAMO_DEBUG'] = '1' # เพิ่มเพื่อดู debug info
|
26 |
-
###
|
27 |
-
|
28 |
import warnings
|
29 |
import torch
|
30 |
|
31 |
-
# เปลี่ยนแปลงที่ 1: เพิ่มการตั้งค่า dynamo ก่อน import unsloth
|
32 |
-
# torch._dynamo.config.suppress_errors = True
|
33 |
-
# torch._dynamo.config.verbose = False
|
34 |
-
# torch._inductor.config.fallback_random = True # เพิ่มบรรทัดนี้
|
35 |
-
|
36 |
-
# torch._inductor.config.triton.cudagraphs = False
|
37 |
-
# torch._inductor.config.disable_kernel_cache = True
|
38 |
-
|
39 |
-
|
40 |
from transformers import TextStreamer
|
41 |
import gradio as gr
|
42 |
from huggingface_hub import login
|
@@ -53,99 +31,19 @@ if 'HUGGING_FACE_HUB_TOKEN' in os.environ:
|
|
53 |
else:
|
54 |
print("คำเตือน: ไม่พบ HUGGING_FACE_HUB_TOKEN")
|
55 |
|
56 |
-
# @spaces.GPU
|
57 |
-
# def load_model():
|
58 |
-
# global model, tokenizer
|
59 |
-
# print("กำลังโหลดโมเดล...")
|
60 |
-
# try:
|
61 |
-
# from unsloth import FastVisionModel
|
62 |
-
# # โหลด base model และ tokenizer แบบพื้นฐาน
|
63 |
-
# base_model, tokenizer = FastVisionModel.from_pretrained(
|
64 |
-
# "unsloth/Llama-3.2-11B-Vision-Instruct"
|
65 |
-
# )
|
66 |
-
|
67 |
-
# print("โหลด base model และ tokenizer สำเร็จ")
|
68 |
-
|
69 |
-
# # โหลดโมเดล fine-tuned แบบพื้นฐาน
|
70 |
-
# from transformers import AutoModelForVision2Seq
|
71 |
-
# model = AutoModelForVision2Seq.from_pretrained(
|
72 |
-
# "Aekanun/Llama-3.2-11B-Vision-Instruct-XRay"
|
73 |
-
# ).to('cuda')
|
74 |
-
|
75 |
-
# print("โหลดโมเดลสำเร็จ!")
|
76 |
-
# return True
|
77 |
-
# except Exception as e:
|
78 |
-
# print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
79 |
-
# import traceback
|
80 |
-
# traceback.print_exc() # เพิ่มการแสดง stack trace
|
81 |
-
# return False
|
82 |
-
|
83 |
###@spaces.GPU
|
84 |
def load_model():
|
85 |
global model
|
86 |
print("กำลังโหลดโมเดล...")
|
87 |
try:
|
88 |
-
# โหลด tokenizer จาก base model
|
89 |
-
# from unsloth import FastVisionModel
|
90 |
-
# from transformers import AutoTokenizer
|
91 |
-
# print("กำลังโหลด tokenizer...")
|
92 |
-
# base_model, _tokenizer = FastVisionModel.from_pretrained(
|
93 |
-
# "unsloth/Llama-3.2-11B-Vision-Instruct",
|
94 |
-
# use_gradient_checkpointing = "unsloth",
|
95 |
-
# device_map="auto" ### เพิ่มตรงนี้
|
96 |
-
# )
|
97 |
-
|
98 |
-
# tokenizer = _tokenizer # กำหนดค่าให้ตัวแปร global โดยตรง
|
99 |
-
# print(f"2. ประเภทของ tokenizer: {type(tokenizer)}")
|
100 |
-
# print(f"3. เมธอดที่มีใน tokenizer: {dir(tokenizer)}")
|
101 |
-
# print("4. Global tokenizer after assignment:", type(tokenizer)) # เช็คค่า
|
102 |
-
|
103 |
-
# print("โหลด base model และ tokenizer สำเร็จ กำลังโหลดโมเดลที่ fine-tune...")
|
104 |
-
|
105 |
-
# # โหลดโมเดล fine-tuned
|
106 |
-
# from transformers import AutoModelForVision2Seq
|
107 |
-
# print("กำลังโหลดโมเดล fine-tuned...")
|
108 |
-
# model = AutoModelForVision2Seq.from_pretrained(
|
109 |
-
# "Aekanun/Llama-3.2-11B-Vision-Instruct-XRay",
|
110 |
-
# device_map="auto", ### เพิ่มตรงนี้
|
111 |
-
# ###load_in_4bit=True,
|
112 |
-
# torch_dtype=torch.float16
|
113 |
-
# ).to('cuda')
|
114 |
-
|
115 |
-
# FastVisionModel.for_inference(model)
|
116 |
-
# print("โหลดโมเดลสำเร็จ!")
|
117 |
-
# return True
|
118 |
from transformers import AutoModelForVision2Seq
|
119 |
-
### import torch
|
120 |
-
|
121 |
-
# print("กำลังโหลด tokenizer...")
|
122 |
-
# # tokenizer = AutoTokenizer.from_pretrained(
|
123 |
-
# # "unsloth/Llama-3.2-11B-Vision-Instruct",
|
124 |
-
# # trust_remote_code=True
|
125 |
-
# # )
|
126 |
-
# tokenizer = AutoTokenizer.from_pretrained(
|
127 |
-
# "meta-llama/Llama-3.2-11B-Vision-Instruct",
|
128 |
-
# trust_remote_code=True,
|
129 |
-
# use_auth_token=True
|
130 |
-
# )
|
131 |
-
|
132 |
-
# print(f"2. ประเภทของ tokenizer: {type(tokenizer)}")
|
133 |
-
# print(f"3. เมธอดที่มีใน tokenizer: {dir(tokenizer)}")
|
134 |
-
# print("4. Global tokenizer after assignment:", type(tokenizer))
|
135 |
-
|
136 |
-
# print("โหลด tokenizer สำเร็จ กำลังโหลดโมเดลที่ fine-tune...")
|
137 |
-
|
138 |
-
# โหลดโมเดล fine-tuned
|
139 |
print("กำลังโหลดโมเดล fine-tuned...")
|
140 |
-
|
141 |
-
|
142 |
-
# device_map="auto",
|
143 |
-
# torch_dtype=torch.float16
|
144 |
-
# ).to('cuda')
|
145 |
model = AutoModelForVision2Seq.from_pretrained(
|
146 |
"0llheaven/Llama-3.2-11B-Vision-Radiology-mini",
|
147 |
load_in_4bit = True,
|
148 |
-
device_map=
|
149 |
torch_dtype = torch.float16
|
150 |
)
|
151 |
|
@@ -160,10 +58,6 @@ def load_model():
|
|
160 |
|
161 |
@spaces.GPU(duration=120)
|
162 |
def process_image(image):
|
163 |
-
# # ย้ายมาไว้ตรงนี้
|
164 |
-
# import os
|
165 |
-
# os.environ['TORCH_LOGS'] = '+dynamo'
|
166 |
-
# os.environ['TORCHDYNAMO_VERBOSE'] = '1'
|
167 |
global model
|
168 |
|
169 |
### โหลด tokenizer จาก base model
|
@@ -178,8 +72,6 @@ def process_image(image):
|
|
178 |
use_gradient_checkpointing = "unsloth",
|
179 |
### device_map="auto" ### เพิ่มตรงนี้
|
180 |
)
|
181 |
-
###
|
182 |
-
|
183 |
|
184 |
print("\nใน process_image():")
|
185 |
print("Type of model:", type(model))
|
@@ -203,13 +95,6 @@ def process_image(image):
|
|
203 |
]}
|
204 |
]
|
205 |
|
206 |
-
# input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
|
207 |
-
# inputs = tokenizer(
|
208 |
-
# image,
|
209 |
-
# input_text,
|
210 |
-
# add_special_tokens=False,
|
211 |
-
# return_tensors="pt",
|
212 |
-
# ).to("cuda")
|
213 |
print("1. Messages:", messages)
|
214 |
|
215 |
print("2. Tokenizer type:", type(tokenizer))
|
|
|
12 |
except Exception as e:
|
13 |
print(f"Failed to install packages: {e}")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
import warnings
|
16 |
import torch
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
from transformers import TextStreamer
|
19 |
import gradio as gr
|
20 |
from huggingface_hub import login
|
|
|
31 |
else:
|
32 |
print("คำเตือน: ไม่พบ HUGGING_FACE_HUB_TOKEN")
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
###@spaces.GPU
|
35 |
def load_model():
|
36 |
global model
|
37 |
print("กำลังโหลดโมเดล...")
|
38 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
from transformers import AutoModelForVision2Seq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
print("กำลังโหลดโมเดล fine-tuned...")
|
41 |
+
|
42 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
43 |
model = AutoModelForVision2Seq.from_pretrained(
|
44 |
"0llheaven/Llama-3.2-11B-Vision-Radiology-mini",
|
45 |
load_in_4bit = True,
|
46 |
+
device_map=device,
|
47 |
torch_dtype = torch.float16
|
48 |
)
|
49 |
|
|
|
58 |
|
59 |
@spaces.GPU(duration=120)
|
60 |
def process_image(image):
|
|
|
|
|
|
|
|
|
61 |
global model
|
62 |
|
63 |
### โหลด tokenizer จาก base model
|
|
|
72 |
use_gradient_checkpointing = "unsloth",
|
73 |
### device_map="auto" ### เพิ่มตรงนี้
|
74 |
)
|
|
|
|
|
75 |
|
76 |
print("\nใน process_image():")
|
77 |
print("Type of model:", type(model))
|
|
|
95 |
]}
|
96 |
]
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
print("1. Messages:", messages)
|
99 |
|
100 |
print("2. Tokenizer type:", type(tokenizer))
|