Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,11 @@ import gradio as gr
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from qwen_vl_utils import process_vision_info
|
| 6 |
-
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
|
|
|
| 7 |
import traceback
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ========================================
|
| 10 |
# AIN VLM MODEL FOR OCR
|
|
@@ -66,12 +69,42 @@ def ensure_model_loaded():
|
|
| 66 |
trust_remote_code=True,
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Load processor
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
model = loaded_model
|
| 77 |
processor = loaded_processor
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from qwen_vl_utils import process_vision_info
|
| 6 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, AutoTokenizer
|
| 7 |
+
from transformers import Qwen2VLProcessor, Qwen2VLImageProcessor
|
| 8 |
import traceback
|
| 9 |
+
import json
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
# ========================================
|
| 13 |
# AIN VLM MODEL FOR OCR
|
|
|
|
| 69 |
trust_remote_code=True,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Load processor with proper configuration
|
| 73 |
+
# Manual construction to avoid size parameter issues
|
| 74 |
+
try:
|
| 75 |
+
# First, try the standard way
|
| 76 |
+
loaded_processor = AutoProcessor.from_pretrained(
|
| 77 |
+
MODEL_ID,
|
| 78 |
+
trust_remote_code=True,
|
| 79 |
+
)
|
| 80 |
+
print("✅ Processor loaded successfully (standard method)")
|
| 81 |
+
except ValueError as e:
|
| 82 |
+
if "size must contain 'shortest_edge' and 'longest_edge' keys" in str(e):
|
| 83 |
+
print("⚠️ Standard processor loading failed, trying manual construction...")
|
| 84 |
+
# Manually construct processor with correct size format
|
| 85 |
+
try:
|
| 86 |
+
# Load tokenizer separately
|
| 87 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 88 |
+
|
| 89 |
+
# Create image processor with correct size format
|
| 90 |
+
image_processor = Qwen2VLImageProcessor(
|
| 91 |
+
size={"shortest_edge": 224, "longest_edge": 1120}, # Valid format
|
| 92 |
+
do_resize=True,
|
| 93 |
+
do_rescale=True,
|
| 94 |
+
do_normalize=True,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Create processor from components
|
| 98 |
+
loaded_processor = Qwen2VLProcessor(
|
| 99 |
+
image_processor=image_processor,
|
| 100 |
+
tokenizer=tokenizer,
|
| 101 |
+
)
|
| 102 |
+
print("✅ Processor loaded successfully (manual construction)")
|
| 103 |
+
except Exception as manual_error:
|
| 104 |
+
print(f"❌ Manual construction also failed: {manual_error}")
|
| 105 |
+
raise
|
| 106 |
+
else:
|
| 107 |
+
raise
|
| 108 |
|
| 109 |
model = loaded_model
|
| 110 |
processor = loaded_processor
|