Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,47 +6,38 @@ import base64
|
|
| 6 |
from io import BytesIO
|
| 7 |
from fastapi import FastAPI, HTTPException
|
| 8 |
from pydantic import BaseModel
|
|
|
|
| 9 |
|
| 10 |
-
# Nome do modelo no Hugging Face Hub
|
| 11 |
MODEL_NAME = "facebook/dinov2-small"
|
| 12 |
-
|
| 13 |
-
# Carregando processador e modelo
|
| 14 |
-
# Usamos a classe espec铆fica Dinov2Model para garantir que o modelo seja carregado corretamente
|
| 15 |
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
| 16 |
model = Dinov2Model.from_pretrained(MODEL_NAME)
|
| 17 |
-
|
| 18 |
-
# Proje莽茫o para 512D
|
| 19 |
projection = nn.Linear(model.config.hidden_size, 512)
|
| 20 |
|
| 21 |
-
# Inicializa o FastAPI
|
| 22 |
app = FastAPI(
|
| 23 |
title="API de Embedding de Imagem",
|
| 24 |
-
description="Endpoint para obter o embedding de uma imagem
|
| 25 |
version="1.0.0"
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# Define o modelo de dados para a requisi莽茫o
|
| 29 |
class ImageRequest(BaseModel):
|
| 30 |
image: str
|
| 31 |
|
| 32 |
-
# Define o endpoint para o embedding da imagem
|
| 33 |
@app.post("/embed")
|
| 34 |
async def get_embedding(request: ImageRequest):
|
| 35 |
try:
|
| 36 |
header, img_base64 = request.image.split(",", 1)
|
| 37 |
image_data = base64.b64decode(img_base64)
|
| 38 |
-
image = Image.open(BytesIO(image_data))
|
| 39 |
-
|
| 40 |
-
# --- L贸gica de Infer锚ncia do seu script original ---
|
| 41 |
inputs = processor(images=image, return_tensors="pt")
|
| 42 |
-
|
| 43 |
with torch.no_grad():
|
| 44 |
outputs = model(**inputs)
|
| 45 |
last_hidden_state = outputs.last_hidden_state
|
| 46 |
embedding = last_hidden_state[:, 0]
|
| 47 |
embedding_512 = projection(embedding)
|
| 48 |
-
|
| 49 |
-
return {
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
raise HTTPException(status_code=400, detail=f"Erro ao processar a imagem: {e}")
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
from fastapi import FastAPI, HTTPException
|
| 8 |
from pydantic import BaseModel
|
| 9 |
+
import imagehash # <-- NOVO
|
| 10 |
|
|
|
|
| 11 |
MODEL_NAME = "facebook/dinov2-small"
|
|
|
|
|
|
|
|
|
|
| 12 |
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
| 13 |
model = Dinov2Model.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
| 14 |
projection = nn.Linear(model.config.hidden_size, 512)
|
| 15 |
|
|
|
|
| 16 |
app = FastAPI(
|
| 17 |
title="API de Embedding de Imagem",
|
| 18 |
+
description="Endpoint para obter o embedding e pHash de uma imagem.",
|
| 19 |
version="1.0.0"
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
class ImageRequest(BaseModel):
|
| 23 |
image: str
|
| 24 |
|
|
|
|
| 25 |
@app.post("/embed")
|
| 26 |
async def get_embedding(request: ImageRequest):
|
| 27 |
try:
|
| 28 |
header, img_base64 = request.image.split(",", 1)
|
| 29 |
image_data = base64.b64decode(img_base64)
|
| 30 |
+
image = Image.open(BytesIO(image_data)).convert("RGB")
|
|
|
|
|
|
|
| 31 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model(**inputs)
|
| 34 |
last_hidden_state = outputs.last_hidden_state
|
| 35 |
embedding = last_hidden_state[:, 0]
|
| 36 |
embedding_512 = projection(embedding)
|
| 37 |
+
phash = str(imagehash.phash(image)) # <-- NOVO
|
| 38 |
+
return {
|
| 39 |
+
"embedding": embedding_512.squeeze().tolist(),
|
| 40 |
+
"phash": phash
|
| 41 |
+
}
|
| 42 |
except Exception as e:
|
| 43 |
+
raise HTTPException(status_code=400, detail=f"Erro ao processar a imagem: {e}")
|