Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,59 +1,62 @@
|
|
| 1 |
-
# app/main.py
|
| 2 |
-
from fastapi import FastAPI, UploadFile, File
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.responses import JSONResponse, FileResponse
|
| 5 |
-
from fastapi.staticfiles import StaticFiles
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import io
|
| 8 |
-
import torch
|
| 9 |
-
import torchvision.transforms as transforms
|
| 10 |
-
from torchvision import models
|
| 11 |
-
|
| 12 |
-
app = FastAPI()
|
| 13 |
-
|
| 14 |
-
# Enable CORS
|
| 15 |
-
app.add_middleware(
|
| 16 |
-
CORSMiddleware,
|
| 17 |
-
allow_origins=["*"],
|
| 18 |
-
allow_credentials=True,
|
| 19 |
-
allow_methods=["*"],
|
| 20 |
-
allow_headers=["*"],
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
model.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
transforms.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/main.py
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import torch
|
| 9 |
+
import torchvision.transforms as transforms
|
| 10 |
+
from torchvision import models
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# Enable CORS
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# ✅ Corrected path to relative static directory
|
| 24 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 25 |
+
|
| 26 |
+
@app.get("/")
|
| 27 |
+
def read_root():
|
| 28 |
+
return FileResponse("static/index.html")
|
| 29 |
+
|
| 30 |
+
# Load pretrained model
|
| 31 |
+
model = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)
|
| 32 |
+
model.eval()
|
| 33 |
+
|
| 34 |
+
# Load labels
|
| 35 |
+
with open("imagenet_classes.txt") as f:
|
| 36 |
+
labels = [line.strip() for line in f]
|
| 37 |
+
|
| 38 |
+
# Define image transforms
|
| 39 |
+
transform = transforms.Compose([
|
| 40 |
+
transforms.Resize(256),
|
| 41 |
+
transforms.CenterCrop(224),
|
| 42 |
+
transforms.ToTensor(),
|
| 43 |
+
transforms.Normalize(
|
| 44 |
+
mean=[0.485, 0.456, 0.406],
|
| 45 |
+
std=[0.229, 0.224, 0.225]),
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
@app.post("/predict")
|
| 49 |
+
async def predict(file: UploadFile = File(...)):
|
| 50 |
+
try:
|
| 51 |
+
image_bytes = await file.read()
|
| 52 |
+
img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 53 |
+
img_tensor = transform(img).unsqueeze(0)
|
| 54 |
+
|
| 55 |
+
with torch.no_grad():
|
| 56 |
+
outputs = model(img_tensor)
|
| 57 |
+
_, predicted = torch.max(outputs, 1)
|
| 58 |
+
label = labels[predicted.item()]
|
| 59 |
+
|
| 60 |
+
return JSONResponse(content={"prediction": label})
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|