Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ from PIL import Image
|
|
11 |
vl_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
12 |
"Qwen/Qwen2-VL-2B-Instruct", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, device_map="auto"
|
13 |
)
|
14 |
-
vl_processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
15 |
|
16 |
# Load Text Model
|
17 |
model_name = "Qwen/Qwen2.5-Math-1.5B-Instruct"
|
@@ -21,20 +21,27 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
21 |
|
22 |
math_messages = []
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def process_image(image, shouldConvert=False):
|
25 |
global math_messages
|
26 |
math_messages = [] # Reset when uploading an image
|
27 |
|
28 |
if image is None:
|
29 |
return "No image provided."
|
30 |
-
|
31 |
if shouldConvert:
|
32 |
new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))
|
33 |
new_img.paste(image, (0, 0), mask=image)
|
34 |
image = new_img
|
35 |
|
36 |
try:
|
37 |
-
inputs = vl_processor(images=image, return_tensors="pt").to(device)
|
38 |
if inputs is None:
|
39 |
return "Error processing image."
|
40 |
|
|
|
11 |
vl_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
12 |
"Qwen/Qwen2-VL-2B-Instruct", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, device_map="auto"
|
13 |
)
|
14 |
+
vl_processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", max_pixels=480*480)
|
15 |
|
16 |
# Load Text Model
|
17 |
model_name = "Qwen/Qwen2.5-Math-1.5B-Instruct"
|
|
|
21 |
|
22 |
math_messages = []
|
23 |
|
24 |
+
def resize_image(image):
|
25 |
+
max_size = 480
|
26 |
+
if isinstance(image, str): # Handle file paths
|
27 |
+
image = Image.open(image)
|
28 |
+
image.thumbnail((max_size, max_size))
|
29 |
+
return image
|
30 |
+
|
31 |
def process_image(image, shouldConvert=False):
|
32 |
global math_messages
|
33 |
math_messages = [] # Reset when uploading an image
|
34 |
|
35 |
if image is None:
|
36 |
return "No image provided."
|
37 |
+
|
38 |
if shouldConvert:
|
39 |
new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))
|
40 |
new_img.paste(image, (0, 0), mask=image)
|
41 |
image = new_img
|
42 |
|
43 |
try:
|
44 |
+
inputs = vl_processor(images=resize_image(image), return_tensors="pt").to(device)
|
45 |
if inputs is None:
|
46 |
return "Error processing image."
|
47 |
|