Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ app = FastAPI()
|
|
| 13 |
|
| 14 |
# Load SmolVLM-Instruct model and processor
|
| 15 |
model_id = "HuggingFaceTB/SmolVLM-Instruct"
|
| 16 |
-
processor = AutoProcessor.from_pretrained(model_id, token=os.environ.get("HF_TOKEN"))
|
| 17 |
model = AutoModelForVision2Seq.from_pretrained(model_id, token=os.environ.get("HF_TOKEN"))
|
| 18 |
|
| 19 |
# Harmful objects list for detection
|
|
@@ -31,23 +31,26 @@ async def predict(files: List[UploadFile] = File(...)):
|
|
| 31 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 32 |
|
| 33 |
# Generate description
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
# Extract signs/number plates (OCR)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
# Detect harmful objects/blood
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
detected_objects = processor.decode(detect_outputs[0], skip_special_tokens=True).lower()
|
| 47 |
harmful_detected = any(obj in detected_objects for obj in harmful_objects) and "Detected: " + ", ".join([obj for obj in harmful_objects if obj in detected_objects]) or "None detected"
|
| 48 |
|
| 49 |
# Get image embedding for similarity
|
| 50 |
-
inputs_emb = processor(images=[image], return_tensors="pt")
|
| 51 |
with torch.no_grad():
|
| 52 |
emb = model.vision_model(inputs_emb["pixel_values"]).last_hidden_state.mean(dim=1).cpu().numpy()
|
| 53 |
image_embeddings.append(emb)
|
|
@@ -89,23 +92,26 @@ def gradio_predict(*images):
|
|
| 89 |
image = Image.fromarray(image).convert("RGB")
|
| 90 |
|
| 91 |
# Generate description
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
# Extract signs/number plates (OCR)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
# Detect harmful objects/blood
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
detected_objects = processor.decode(detect_outputs[0], skip_special_tokens=True).lower()
|
| 105 |
harmful_detected = any(obj in detected_objects for obj in harmful_objects) and "Detected: " + ", ".join([obj for obj in harmful_objects if obj in detected_objects]) or "None detected"
|
| 106 |
|
| 107 |
# Get image embedding for similarity
|
| 108 |
-
inputs_emb = processor(images=[image], return_tensors="pt")
|
| 109 |
with torch.no_grad():
|
| 110 |
emb = model.vision_model(inputs_emb["pixel_values"]).last_hidden_state.mean(dim=1).cpu().numpy()
|
| 111 |
image_embeddings.append(emb)
|
|
@@ -153,7 +159,7 @@ iface = gr.Interface(
|
|
| 153 |
inputs=[gr.Image(label=f"Upload Image {i+1}") for i in range(3)], # Allow up to 3 images
|
| 154 |
outputs=gr.Textbox(label="Results"),
|
| 155 |
title="VisionSage: Image Analysis with SmolVLM",
|
| 156 |
-
description="Upload up to 3 images to get descriptions, extract signs/number plates, detect harmful objects/blood, and compute similarity to the first image."
|
| 157 |
)
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
|
|
|
| 13 |
|
| 14 |
# Load SmolVLM-Instruct model and processor
|
| 15 |
model_id = "HuggingFaceTB/SmolVLM-Instruct"
|
| 16 |
+
processor = AutoProcessor.from_pretrained(model_id, token=os.environ.get("HF_TOKEN"), padding=True)
|
| 17 |
model = AutoModelForVision2Seq.from_pretrained(model_id, token=os.environ.get("HF_TOKEN"))
|
| 18 |
|
| 19 |
# Harmful objects list for detection
|
|
|
|
| 31 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 32 |
|
| 33 |
# Generate description
|
| 34 |
+
prompt_desc = "<image> Provide a detailed description of the image, including objects, colors, and context."
|
| 35 |
+
inputs = processor(text=[prompt_desc], images=[image], return_tensors="pt", padding=True)
|
| 36 |
+
outputs = model.generate(**inputs, max_length=512)
|
| 37 |
+
description = processor.decode(outputs[0], skip_special_tokens=True).replace(prompt_desc, "").strip()
|
| 38 |
|
| 39 |
# Extract signs/number plates (OCR)
|
| 40 |
+
prompt_ocr = "<image> Extract all visible text in the image, such as signs or license plates."
|
| 41 |
+
inputs_ocr = processor(text=[prompt_ocr], images=[image], return_tensors="pt", padding=True)
|
| 42 |
+
ocr_outputs = model.generate(**inputs_ocr, max_length=512)
|
| 43 |
+
signs_text = processor.decode(ocr_outputs[0], skip_special_tokens=True).replace(prompt_ocr, "").strip()
|
| 44 |
|
| 45 |
# Detect harmful objects/blood
|
| 46 |
+
prompt_detect = "<image> Identify any harmful objects (e.g., knife, gun, blood, syringe, bomb, blade) in the image. List them explicitly."
|
| 47 |
+
inputs_detect = processor(text=[prompt_detect], images=[image], return_tensors="pt", padding=True)
|
| 48 |
+
detect_outputs = model.generate(**inputs_detect, max_length=512)
|
| 49 |
detected_objects = processor.decode(detect_outputs[0], skip_special_tokens=True).lower()
|
| 50 |
harmful_detected = any(obj in detected_objects for obj in harmful_objects) and "Detected: " + ", ".join([obj for obj in harmful_objects if obj in detected_objects]) or "None detected"
|
| 51 |
|
| 52 |
# Get image embedding for similarity
|
| 53 |
+
inputs_emb = processor(images=[image], return_tensors="pt", padding=True)
|
| 54 |
with torch.no_grad():
|
| 55 |
emb = model.vision_model(inputs_emb["pixel_values"]).last_hidden_state.mean(dim=1).cpu().numpy()
|
| 56 |
image_embeddings.append(emb)
|
|
|
|
| 92 |
image = Image.fromarray(image).convert("RGB")
|
| 93 |
|
| 94 |
# Generate description
|
| 95 |
+
prompt_desc = "<image> Provide a detailed description of the image, including objects, colors, and context."
|
| 96 |
+
inputs = processor(text=[prompt_desc], images=[image], return_tensors="pt", padding=True)
|
| 97 |
+
outputs = model.generate(**inputs, max_length=512)
|
| 98 |
+
description = processor.decode(outputs[0], skip_special_tokens=True).replace(prompt_desc, "").strip()
|
| 99 |
|
| 100 |
# Extract signs/number plates (OCR)
|
| 101 |
+
prompt_ocr = "<image> Extract all visible text in the image, such as signs or license plates."
|
| 102 |
+
inputs_ocr = processor(text=[prompt_ocr], images=[image], return_tensors="pt", padding=True)
|
| 103 |
+
ocr_outputs = model.generate(**inputs_ocr, max_length=512)
|
| 104 |
+
signs_text = processor.decode(ocr_outputs[0], skip_special_tokens=True).replace(prompt_ocr, "").strip()
|
| 105 |
|
| 106 |
# Detect harmful objects/blood
|
| 107 |
+
prompt_detect = "<image> Identify any harmful objects (e.g., knife, gun, blood, syringe, bomb, blade) in the image. List them explicitly."
|
| 108 |
+
inputs_detect = processor(text=[prompt_detect], images=[image], return_tensors="pt", padding=True)
|
| 109 |
+
detect_outputs = model.generate(**inputs_detect, max_length=512)
|
| 110 |
detected_objects = processor.decode(detect_outputs[0], skip_special_tokens=True).lower()
|
| 111 |
harmful_detected = any(obj in detected_objects for obj in harmful_objects) and "Detected: " + ", ".join([obj for obj in harmful_objects if obj in detected_objects]) or "None detected"
|
| 112 |
|
| 113 |
# Get image embedding for similarity
|
| 114 |
+
inputs_emb = processor(images=[image], return_tensors="pt", padding=True)
|
| 115 |
with torch.no_grad():
|
| 116 |
emb = model.vision_model(inputs_emb["pixel_values"]).last_hidden_state.mean(dim=1).cpu().numpy()
|
| 117 |
image_embeddings.append(emb)
|
|
|
|
| 159 |
inputs=[gr.Image(label=f"Upload Image {i+1}") for i in range(3)], # Allow up to 3 images
|
| 160 |
outputs=gr.Textbox(label="Results"),
|
| 161 |
title="VisionSage: Image Analysis with SmolVLM",
|
| 162 |
+
description="Upload up to 3 images to get detailed descriptions, extract signs/number plates, detect harmful objects/blood, and compute similarity to the first image."
|
| 163 |
)
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|