Update app.py
Browse files
app.py
CHANGED
|
@@ -34,18 +34,43 @@ class ImageAnalysisResponse(BaseModel):
|
|
| 34 |
print("[INFO] Loading Florence-2 model on CPU...")
|
| 35 |
try:
|
| 36 |
MODEL_ID = "microsoft/Florence-2-large"
|
|
|
|
|
|
|
| 37 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
|
|
|
|
|
|
| 38 |
model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
MODEL_ID,
|
| 40 |
trust_remote_code=True,
|
| 41 |
torch_dtype=torch.float32,
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
print("[INFO] Model loaded successfully!")
|
| 45 |
except Exception as e:
|
| 46 |
print(f"[ERROR] Failed to load model: {e}")
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# ===== Helper Functions =====
|
| 51 |
def download_image(url: str) -> Image.Image:
|
|
@@ -92,14 +117,16 @@ def analyze_image(image: Image.Image) -> str:
|
|
| 92 |
return_tensors="pt"
|
| 93 |
).to(DEVICE)
|
| 94 |
|
| 95 |
-
# Generate caption
|
| 96 |
with torch.no_grad():
|
| 97 |
generated_ids = model.generate(
|
| 98 |
input_ids=inputs["input_ids"],
|
| 99 |
pixel_values=inputs["pixel_values"],
|
| 100 |
-
max_new_tokens=
|
| 101 |
num_beams=3,
|
| 102 |
-
do_sample=False
|
|
|
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
# Decode and clean output
|
|
|
|
| 34 |
print("[INFO] Loading Florence-2 model on CPU...")
|
| 35 |
try:
|
| 36 |
MODEL_ID = "microsoft/Florence-2-large"
|
| 37 |
+
|
| 38 |
+
# Load processor
|
| 39 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 40 |
+
|
| 41 |
+
# Load model with specific parameters to avoid SDPA issues
|
| 42 |
model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
MODEL_ID,
|
| 44 |
trust_remote_code=True,
|
| 45 |
torch_dtype=torch.float32,
|
| 46 |
+
attn_implementation="eager", # Force eager attention to avoid SDPA issues
|
| 47 |
+
device_map=None # Explicitly set to None for CPU
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Move to device manually
|
| 51 |
+
model = model.to(DEVICE)
|
| 52 |
+
model.eval()
|
| 53 |
+
|
| 54 |
print("[INFO] Model loaded successfully!")
|
| 55 |
except Exception as e:
|
| 56 |
print(f"[ERROR] Failed to load model: {e}")
|
| 57 |
+
# Try fallback to base model if large fails
|
| 58 |
+
try:
|
| 59 |
+
print("[INFO] Trying Florence-2-base as fallback...")
|
| 60 |
+
MODEL_ID = "microsoft/Florence-2-base"
|
| 61 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 62 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 63 |
+
MODEL_ID,
|
| 64 |
+
trust_remote_code=True,
|
| 65 |
+
torch_dtype=torch.float32,
|
| 66 |
+
attn_implementation="eager",
|
| 67 |
+
device_map=None
|
| 68 |
+
).to(DEVICE).eval()
|
| 69 |
+
print("[INFO] Fallback model loaded successfully!")
|
| 70 |
+
except Exception as fallback_error:
|
| 71 |
+
print(f"[ERROR] Fallback also failed: {fallback_error}")
|
| 72 |
+
processor = None
|
| 73 |
+
model = None
|
| 74 |
|
| 75 |
# ===== Helper Functions =====
|
| 76 |
def download_image(url: str) -> Image.Image:
|
|
|
|
| 117 |
return_tensors="pt"
|
| 118 |
).to(DEVICE)
|
| 119 |
|
| 120 |
+
# Generate caption with error handling
|
| 121 |
with torch.no_grad():
|
| 122 |
generated_ids = model.generate(
|
| 123 |
input_ids=inputs["input_ids"],
|
| 124 |
pixel_values=inputs["pixel_values"],
|
| 125 |
+
max_new_tokens=256, # Reduced for stability
|
| 126 |
num_beams=3,
|
| 127 |
+
do_sample=False,
|
| 128 |
+
early_stopping=True,
|
| 129 |
+
pad_token_id=processor.tokenizer.eos_token_id
|
| 130 |
)
|
| 131 |
|
| 132 |
# Decode and clean output
|