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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
# สร้างอินเตอร์เฟซด้วย Gradio
|
48 |
-
demo = gr.Interface(
|
49 |
-
fn=generate_caption,
|
50 |
-
image_input = gr.Image(type="pil", label="Upload Image"),
|
51 |
-
output_text = gr.Textbox(label="Generated Caption"),
|
52 |
-
title="Medical Vision Analysis"
|
53 |
-
)
|
54 |
-
|
55 |
-
# รันแอป
|
56 |
-
if __name__ == "__main__":
|
57 |
-
demo.launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
def install_packages():
|
7 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "unsloth-zoo"])
|
8 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-deps", "git+https://github.com/unslothai/unsloth.git"])
|
9 |
+
|
10 |
+
try:
|
11 |
+
install_packages()
|
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
|
43 |
from PIL import Image
|
44 |
+
|
45 |
+
warnings.filterwarnings('ignore')
|
46 |
+
|
47 |
+
model = None
|
48 |
+
tokenizer = None
|
49 |
+
|
50 |
+
if 'HUGGING_FACE_HUB_TOKEN' in os.environ:
|
51 |
+
print("กำลังเข้าสู่ระบบ Hugging Face Hub...")
|
52 |
+
login(token=os.environ['HUGGING_FACE_HUB_TOKEN'])
|
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 |
+
# model = AutoModelForVision2Seq.from_pretrained(
|
141 |
+
# "Aekanun/Llama-3.2-11B-Vision-Instruct-XRay",
|
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="auto",
|
149 |
+
torch_dtype = torch.float16
|
150 |
+
)
|
151 |
+
|
152 |
+
print("โหลดโมเดลสำเร็จ!")
|
153 |
+
return True
|
154 |
+
|
155 |
+
except Exception as e:
|
156 |
+
print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
157 |
+
import traceback
|
158 |
+
traceback.print_exc()
|
159 |
+
return False
|
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
|
170 |
+
from unsloth import FastVisionModel
|
171 |
+
|
172 |
+
FastVisionModel.for_inference(model) ###ลองแก้ไขปัญหา torch
|
173 |
+
|
174 |
+
from transformers import AutoTokenizer
|
175 |
+
print("กำลังโหลด tokenizer...")
|
176 |
+
base_model, tokenizer = FastVisionModel.from_pretrained(
|
177 |
+
"unsloth/Llama-3.2-11B-Vision-Instruct",
|
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))
|
186 |
+
print("A. Type of tokenizer:", type(tokenizer))
|
187 |
+
if tokenizer is not None:
|
188 |
+
print("B. Available methods:", dir(tokenizer))
|
189 |
+
|
190 |
+
if image is None:
|
191 |
+
return "กรุณาอัพโหลดรูปภาพ"
|
192 |
+
|
193 |
+
try:
|
194 |
+
if not isinstance(image, Image.Image):
|
195 |
+
image = Image.fromarray(image)
|
196 |
+
|
197 |
+
print("0. Image info:", type(image), image.size) # เพิ่ม debug ข้อมูลรูปภาพ
|
198 |
+
instruction = "You are an expert radiographer. Describe accurately what you see in this image."
|
199 |
+
messages = [
|
200 |
+
{"role": "user", "content": [
|
201 |
+
{"type": "image"},
|
202 |
+
{"type": "text", "text": instruction}
|
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))
|
216 |
+
input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
|
217 |
+
print("3. Chat template success:", input_text[:100])
|
218 |
+
inputs = tokenizer(
|
219 |
+
image,
|
220 |
+
input_text,
|
221 |
+
add_special_tokens=False,
|
222 |
+
return_tensors="pt",
|
223 |
+
).to("cuda")
|
224 |
+
print("3. Tokenizer inputs:", inputs.keys()) # Debug 3
|
225 |
+
|
226 |
+
text_streamer = TextStreamer(tokenizer, skip_prompt=True)
|
227 |
+
outputs = model.generate(
|
228 |
+
**inputs,
|
229 |
+
streamer=text_streamer,
|
230 |
+
max_new_tokens=256,
|
231 |
+
use_cache=True,
|
232 |
+
temperature=1.5,
|
233 |
+
min_p=0.1
|
234 |
+
)
|
235 |
+
|
236 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
237 |
+
|
238 |
+
except Exception as e:
|
239 |
+
return f"เกิดข้อผิดพลาด: {str(e)}"
|
240 |
+
|
241 |
+
if load_model():
|
242 |
+
demo = gr.Interface(
|
243 |
+
fn=process_image,
|
244 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
245 |
+
outputs=gr.Textbox(label="Generated Caption"),
|
246 |
+
title="Medical Vision Analysis"
|
247 |
+
)
|
248 |
|
249 |
+
if __name__ == "__main__":
|
250 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|