Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
|
@@ -14,7 +14,7 @@ app = FastAPI()
|
|
| 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 =
|
| 18 |
|
| 19 |
# Harmful objects list for detection
|
| 20 |
harmful_objects = ["knife", "gun", "weapon", "blood", "syringe", "bomb", "blade"]
|
|
@@ -49,7 +49,8 @@ async def predict(files: List[UploadFile] = File(...)):
|
|
| 49 |
# Get image embedding for similarity
|
| 50 |
inputs_emb = processor(images=image, return_tensors="pt").to("cuda")
|
| 51 |
with torch.no_grad():
|
| 52 |
-
|
|
|
|
| 53 |
image_embeddings.append(emb)
|
| 54 |
|
| 55 |
results.append({
|
|
@@ -107,7 +108,7 @@ def gradio_predict(*images):
|
|
| 107 |
# Get image embedding for similarity
|
| 108 |
inputs_emb = processor(images=image, return_tensors="pt").to("cuda")
|
| 109 |
with torch.no_grad():
|
| 110 |
-
emb = model.
|
| 111 |
image_embeddings.append(emb)
|
| 112 |
|
| 113 |
results.append({
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForVision2Seq, AutoProcessor
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
|
|
|
| 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, torch_dtype=torch.bfloat16, token=os.environ.get("HF_TOKEN")).to("cuda")
|
| 18 |
|
| 19 |
# Harmful objects list for detection
|
| 20 |
harmful_objects = ["knife", "gun", "weapon", "blood", "syringe", "bomb", "blade"]
|
|
|
|
| 49 |
# Get image embedding for similarity
|
| 50 |
inputs_emb = processor(images=image, return_tensors="pt").to("cuda")
|
| 51 |
with torch.no_grad():
|
| 52 |
+
# Use vision tower for embeddings
|
| 53 |
+
emb = model.vision_model(inputs_emb["pixel_values"]).last_hidden_state.mean(dim=1).cpu().numpy()
|
| 54 |
image_embeddings.append(emb)
|
| 55 |
|
| 56 |
results.append({
|
|
|
|
| 108 |
# Get image embedding for similarity
|
| 109 |
inputs_emb = processor(images=image, return_tensors="pt").to("cuda")
|
| 110 |
with torch.no_grad():
|
| 111 |
+
emb = model.vision_model(inputs_emb["pixel_values"]).last_hidden_state.mean(dim=1).cpu().numpy()
|
| 112 |
image_embeddings.append(emb)
|
| 113 |
|
| 114 |
results.append({
|