Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
|
|
|
| 2 |
from transformers import AutoImageProcessor, AutoModel
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
|
@@ -8,29 +9,19 @@ import uvicorn
|
|
| 8 |
|
| 9 |
app = FastAPI(title="Movie Linker - Image Embedding API (DINOv2)")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
#
|
| 15 |
img_model_id = 'facebook/dinov2-base'
|
| 16 |
img_processor = AutoImageProcessor.from_pretrained(img_model_id)
|
| 17 |
img_model = AutoModel.from_pretrained(img_model_id)
|
| 18 |
img_model.eval()
|
| 19 |
|
| 20 |
-
print("DINOv2 loaded successfully.")
|
| 21 |
-
|
| 22 |
-
@app.get("/")
|
| 23 |
-
def home():
|
| 24 |
-
return {
|
| 25 |
-
"status": "online",
|
| 26 |
-
"model": img_model_id,
|
| 27 |
-
"endpoint": "/embed/image"
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
@app.post("/embed/image")
|
| 31 |
-
async def embed_image(
|
| 32 |
try:
|
| 33 |
-
response = requests.get(image_url, timeout=10)
|
| 34 |
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 35 |
|
| 36 |
# Process image
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transformers import AutoImageProcessor, AutoModel
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI(title="Movie Linker - Image Embedding API (DINOv2)")
|
| 11 |
|
| 12 |
+
class ImageRequest(BaseModel):
|
| 13 |
+
image_url: str
|
| 14 |
|
| 15 |
+
# Load Model
|
| 16 |
img_model_id = 'facebook/dinov2-base'
|
| 17 |
img_processor = AutoImageProcessor.from_pretrained(img_model_id)
|
| 18 |
img_model = AutoModel.from_pretrained(img_model_id)
|
| 19 |
img_model.eval()
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@app.post("/embed/image")
|
| 22 |
+
async def embed_image(request: ImageRequest):
|
| 23 |
try:
|
| 24 |
+
response = requests.get(request.image_url, timeout=10)
|
| 25 |
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 26 |
|
| 27 |
# Process image
|