Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import base64
|
|
| 6 |
from io import BytesIO
|
| 7 |
from fastapi import FastAPI, HTTPException
|
| 8 |
from pydantic import BaseModel
|
| 9 |
-
import imagehash
|
| 10 |
|
| 11 |
MODEL_NAME = "facebook/dinov2-small"
|
| 12 |
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
|
@@ -21,6 +21,7 @@ app = FastAPI(
|
|
| 21 |
|
| 22 |
class ImageRequest(BaseModel):
|
| 23 |
image: str
|
|
|
|
| 24 |
|
| 25 |
@app.post("/embed")
|
| 26 |
async def get_embedding(request: ImageRequest):
|
|
@@ -34,10 +35,15 @@ async def get_embedding(request: ImageRequest):
|
|
| 34 |
last_hidden_state = outputs.last_hidden_state
|
| 35 |
embedding = last_hidden_state[:, 0]
|
| 36 |
embedding_512 = projection(embedding)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}")
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
from fastapi import FastAPI, HTTPException
|
| 8 |
from pydantic import BaseModel
|
| 9 |
+
import imagehash
|
| 10 |
|
| 11 |
MODEL_NAME = "facebook/dinov2-small"
|
| 12 |
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
|
|
|
| 21 |
|
| 22 |
class ImageRequest(BaseModel):
|
| 23 |
image: str
|
| 24 |
+
use_float16: bool = False # <-- NOVO: Parâmetro opcional com valor padrão False
|
| 25 |
|
| 26 |
@app.post("/embed")
|
| 27 |
async def get_embedding(request: ImageRequest):
|
|
|
|
| 35 |
last_hidden_state = outputs.last_hidden_state
|
| 36 |
embedding = last_hidden_state[:, 0]
|
| 37 |
embedding_512 = projection(embedding)
|
| 38 |
+
|
| 39 |
+
# <-- NOVA LÓGICA: Conversão condicional para float16
|
| 40 |
+
if request.use_float16:
|
| 41 |
+
embedding_512 = embedding_512.half()
|
| 42 |
+
|
| 43 |
+
phash = str(imagehash.phash(image))
|
| 44 |
return {
|
| 45 |
"embedding": embedding_512.squeeze().tolist(),
|
| 46 |
"phash": phash
|
| 47 |
}
|
| 48 |
except Exception as e:
|
| 49 |
+
raise HTTPException(status_code=400, detail=f"Erro ao processar a imagem: {e}")
|