Spaces:
Runtime error
Runtime error
rev app
Browse files
app.py
CHANGED
@@ -12,21 +12,15 @@ try:
|
|
12 |
except Exception as e:
|
13 |
print(f"Failed to install packages: {e}")
|
14 |
|
15 |
-
|
16 |
-
###
|
17 |
-
# เพิ่มบรรทัดนี้ที่ต้นโค้ด ก่อน import torch
|
18 |
import os
|
19 |
os.environ['NVIDIA_VISIBLE_DEVICES'] = ''
|
20 |
-
###
|
21 |
|
22 |
import warnings
|
23 |
import torch
|
24 |
|
25 |
-
|
26 |
-
torch._dynamo.config.suppress_errors = True
|
27 |
torch._dynamo.config.verbose = False
|
28 |
|
29 |
-
|
30 |
from transformers import TextStreamer
|
31 |
import gradio as gr
|
32 |
from huggingface_hub import login
|
@@ -34,92 +28,46 @@ from PIL import Image
|
|
34 |
|
35 |
warnings.filterwarnings('ignore')
|
36 |
|
37 |
-
model = None
|
38 |
-
tokenizer = None
|
39 |
-
|
40 |
if 'HUGGING_FACE_HUB_TOKEN' in os.environ:
|
41 |
print("กำลังเข้าสู่ระบบ Hugging Face Hub...")
|
42 |
login(token=os.environ['HUGGING_FACE_HUB_TOKEN'])
|
43 |
else:
|
44 |
print("คำเตือน: ไม่พบ HUGGING_FACE_HUB_TOKEN")
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# print("กำลังโหลดโมเดล...")
|
50 |
-
# try:
|
51 |
-
# from unsloth import FastVisionModel
|
52 |
-
# # โหลด base model และ tokenizer แบบพื้นฐาน
|
53 |
-
# base_model, tokenizer = FastVisionModel.from_pretrained(
|
54 |
-
# "unsloth/Llama-3.2-11B-Vision-Instruct"
|
55 |
-
# )
|
56 |
-
|
57 |
-
# print("โหลด base model และ tokenizer สำเร็จ")
|
58 |
-
|
59 |
-
# # โหลดโมเดล fine-tuned แบบพื้นฐาน
|
60 |
-
# from transformers import AutoModelForVision2Seq
|
61 |
-
# model = AutoModelForVision2Seq.from_pretrained(
|
62 |
-
# "Aekanun/Llama-3.2-11B-Vision-Instruct-XRay"
|
63 |
-
# ).to('cuda')
|
64 |
-
|
65 |
-
# print("โหลดโมเดลสำเร็จ!")
|
66 |
-
# return True
|
67 |
-
# except Exception as e:
|
68 |
-
# print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
69 |
-
# import traceback
|
70 |
-
# traceback.print_exc() # เพิ่มการแสดง stack trace
|
71 |
-
# return False
|
72 |
-
|
73 |
-
@spaces.GPU
|
74 |
-
def load_model():
|
75 |
-
global model, tokenizer
|
76 |
-
print("กำลังโหลดโมเดล...")
|
77 |
-
try:
|
78 |
-
# โหลด tokenizer จาก base model
|
79 |
-
from unsloth import FastVisionModel
|
80 |
-
from transformers import AutoTokenizer
|
81 |
-
print("กำลังโหลด tokenizer...")
|
82 |
-
base_model, _tokenizer = FastVisionModel.from_pretrained(
|
83 |
-
"unsloth/Llama-3.2-11B-Vision-Instruct",
|
84 |
-
use_gradient_checkpointing = "unsloth"
|
85 |
-
)
|
86 |
-
|
87 |
-
tokenizer = _tokenizer # กำหนดค่าให้ตัวแปร global โดยตรง
|
88 |
-
print(f"2. ประเภทของ tokenizer: {type(tokenizer)}")
|
89 |
-
print(f"3. เมธอดที่มีใน tokenizer: {dir(tokenizer)}")
|
90 |
-
print("4. Global tokenizer after assignment:", type(tokenizer)) # เช็คค่า
|
91 |
-
|
92 |
-
print("โหลด base model และ tokenizer สำเร็จ กำลังโหลดโมเดลที่ fine-tune...")
|
93 |
-
|
94 |
-
# โหลดโมเดล fine-tuned
|
95 |
-
from transformers import AutoModelForVision2Seq
|
96 |
-
print("กำลังโหลดโมเดล fine-tuned...")
|
97 |
-
model = AutoModelForVision2Seq.from_pretrained(
|
98 |
-
"Aekanun/Llama-3.2-11B-Vision-Instruct-XRay",
|
99 |
-
load_in_4bit=True,
|
100 |
-
torch_dtype=torch.float16
|
101 |
-
).to('cuda')
|
102 |
-
|
103 |
-
FastVisionModel.for_inference(model)
|
104 |
-
print("โหลดโมเดลสำเร็จ!")
|
105 |
-
return True
|
106 |
-
|
107 |
-
except Exception as e:
|
108 |
-
print(f"เกิดข้อผิดพลาดในการโหลดโมเดล: {str(e)}")
|
109 |
-
import traceback
|
110 |
-
traceback.print_exc()
|
111 |
-
return False
|
112 |
-
|
113 |
-
@spaces.GPU(duration=30)
|
114 |
-
def process_image(image):
|
115 |
-
global model, tokenizer
|
116 |
-
|
117 |
-
print("Type of model:", type(model))
|
118 |
-
print("\nใน process_image():")
|
119 |
-
print("A. Type of tokenizer:", type(tokenizer))
|
120 |
-
if tokenizer is not None:
|
121 |
-
print("B. Available methods:", dir(tokenizer))
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
if image is None:
|
124 |
return "กรุณาอัพโหลดรูปภาพ"
|
125 |
|
@@ -127,7 +75,6 @@ def process_image(image):
|
|
127 |
if not isinstance(image, Image.Image):
|
128 |
image = Image.fromarray(image)
|
129 |
|
130 |
-
print("0. Image info:", type(image), image.size) # เพิ่ม debug ข้อมูลรูปภาพ
|
131 |
instruction = "You are an expert radiographer. Describe accurately what you see in this image."
|
132 |
messages = [
|
133 |
{"role": "user", "content": [
|
@@ -136,29 +83,17 @@ def process_image(image):
|
|
136 |
]}
|
137 |
]
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
# image,
|
142 |
-
# input_text,
|
143 |
-
# add_special_tokens=False,
|
144 |
-
# return_tensors="pt",
|
145 |
-
# ).to("cuda")
|
146 |
-
print("1. Messages:", messages)
|
147 |
-
|
148 |
-
print("2. Tokenizer type:", type(tokenizer))
|
149 |
-
input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
|
150 |
-
print("3. Chat template success:", input_text[:100])
|
151 |
-
inputs = tokenizer(
|
152 |
image,
|
153 |
input_text,
|
154 |
add_special_tokens=False,
|
155 |
return_tensors="pt",
|
156 |
).to("cuda")
|
157 |
-
print("3. Tokenizer inputs:", inputs.keys()) # Debug 3
|
158 |
|
159 |
-
text_streamer = TextStreamer(
|
160 |
-
outputs =
|
161 |
-
**inputs,
|
162 |
streamer=text_streamer,
|
163 |
max_new_tokens=128,
|
164 |
use_cache=True,
|
@@ -166,21 +101,18 @@ def process_image(image):
|
|
166 |
min_p=0.1
|
167 |
)
|
168 |
|
169 |
-
return
|
170 |
|
171 |
except Exception as e:
|
172 |
return f"เกิดข้อผิดพลาด: {str(e)}"
|
173 |
|
174 |
print("กำลังเริ่มต้นแอปพลิเคชัน...")
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
demo.launch()
|
185 |
-
else:
|
186 |
-
print("ไม่สามารถเริ่มต้นแอปพลิเคชันได้")
|
|
|
12 |
except Exception as e:
|
13 |
print(f"Failed to install packages: {e}")
|
14 |
|
|
|
|
|
|
|
15 |
import os
|
16 |
os.environ['NVIDIA_VISIBLE_DEVICES'] = ''
|
|
|
17 |
|
18 |
import warnings
|
19 |
import torch
|
20 |
|
21 |
+
torch._dynamo.config.suppress_errors = True
|
|
|
22 |
torch._dynamo.config.verbose = False
|
23 |
|
|
|
24 |
from transformers import TextStreamer
|
25 |
import gradio as gr
|
26 |
from huggingface_hub import login
|
|
|
28 |
|
29 |
warnings.filterwarnings('ignore')
|
30 |
|
|
|
|
|
|
|
31 |
if 'HUGGING_FACE_HUB_TOKEN' in os.environ:
|
32 |
print("กำลังเข้าสู่ระบบ Hugging Face Hub...")
|
33 |
login(token=os.environ['HUGGING_FACE_HUB_TOKEN'])
|
34 |
else:
|
35 |
print("คำเตือน: ไม่พบ HUGGING_FACE_HUB_TOKEN")
|
36 |
|
37 |
+
def model_context():
|
38 |
+
_tokenizer = None
|
39 |
+
_model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
def decorator(func):
|
42 |
+
def wrapper(*args, **kwargs):
|
43 |
+
nonlocal _tokenizer, _model
|
44 |
+
if _tokenizer is None:
|
45 |
+
print("กำลังโหลด tokenizer...")
|
46 |
+
from unsloth import FastVisionModel
|
47 |
+
base_model, _tokenizer = FastVisionModel.from_pretrained(
|
48 |
+
"unsloth/Llama-3.2-11B-Vision-Instruct",
|
49 |
+
use_gradient_checkpointing = "unsloth"
|
50 |
+
)
|
51 |
+
print("โหลด tokenizer สำเร็จ")
|
52 |
+
|
53 |
+
if _model is None:
|
54 |
+
print("กำลังโหลดโมเดล fine-tuned...")
|
55 |
+
from transformers import AutoModelForVision2Seq
|
56 |
+
_model = AutoModelForVision2Seq.from_pretrained(
|
57 |
+
"Aekanun/Llama-3.2-11B-Vision-Instruct-XRay",
|
58 |
+
load_in_4bit=True,
|
59 |
+
torch_dtype=torch.float16
|
60 |
+
).to('cuda')
|
61 |
+
FastVisionModel.for_inference(_model)
|
62 |
+
print("โหลดโมเดลสำเร็จ!")
|
63 |
+
|
64 |
+
return func(_model, _tokenizer, *args, **kwargs)
|
65 |
+
return wrapper
|
66 |
+
return decorator
|
67 |
+
|
68 |
+
@model_context()
|
69 |
+
@spaces.GPU(duration=30)
|
70 |
+
def process_image(_model, _tokenizer, image):
|
71 |
if image is None:
|
72 |
return "กรุณาอัพโหลดรูปภาพ"
|
73 |
|
|
|
75 |
if not isinstance(image, Image.Image):
|
76 |
image = Image.fromarray(image)
|
77 |
|
|
|
78 |
instruction = "You are an expert radiographer. Describe accurately what you see in this image."
|
79 |
messages = [
|
80 |
{"role": "user", "content": [
|
|
|
83 |
]}
|
84 |
]
|
85 |
|
86 |
+
input_text = _tokenizer.apply_chat_template(messages, add_generation_prompt=True)
|
87 |
+
inputs = _tokenizer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
image,
|
89 |
input_text,
|
90 |
add_special_tokens=False,
|
91 |
return_tensors="pt",
|
92 |
).to("cuda")
|
|
|
93 |
|
94 |
+
text_streamer = TextStreamer(_tokenizer, skip_prompt=True)
|
95 |
+
outputs = _model.generate(
|
96 |
+
**inputs,
|
97 |
streamer=text_streamer,
|
98 |
max_new_tokens=128,
|
99 |
use_cache=True,
|
|
|
101 |
min_p=0.1
|
102 |
)
|
103 |
|
104 |
+
return _tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
105 |
|
106 |
except Exception as e:
|
107 |
return f"เกิดข้อผิดพลาด: {str(e)}"
|
108 |
|
109 |
print("กำลังเริ่มต้นแอปพลิเคชัน...")
|
110 |
+
demo = gr.Interface(
|
111 |
+
fn=process_image,
|
112 |
+
inputs=gr.Image(type="pil"),
|
113 |
+
outputs=gr.Textbox(),
|
114 |
+
title="Medical Vision Analysis"
|
115 |
+
)
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
+
demo.launch()
|
|
|
|
|
|