Spaces:
Sleeping
Sleeping
File size: 1,964 Bytes
5c30b4c 448b588 5c30b4c 0c26744 5c30b4c 448b588 5c30b4c 448b588 5c30b4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import json
import torch
from model_utils import create_model, predict
# --- App Initialization ---
app = FastAPI(title="Fish Species Classification API")
# --- CORS Configuration ---
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173", # For your local frontend development
"https://aqua-ai-omega.vercel.app" # MODIFIED: Add your Vercel URL here
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --- Model Loading ---
try:
with open("class_names.json", "r") as f:
CLASS_NAMES = json.load(f)
NUM_CLASSES = len(CLASS_NAMES)
except FileNotFoundError:
raise RuntimeError("class_names.json not found. Please run the training script to generate it.")
MODEL_PATH = "models/fish_resnet18_best.pth"
model = create_model(NUM_CLASSES)
try:
# MODIFIED: Added weights_only=False to the torch.load call
state_dict = torch.load(MODEL_PATH, map_location=torch.device('cpu'), weights_only=False)
model.load_state_dict(state_dict)
print("✅ Model loaded successfully.")
except FileNotFoundError:
raise RuntimeError(f"Model file not found at {MODEL_PATH}. Please ensure it's in the correct directory.")
model.eval()
# --- API Endpoints ---
@app.get("/")
def read_root():
return {"message": "Welcome to the Fish Species Classification API"}
@app.post("/predict")
async def predict_species(file: UploadFile = File(...)):
if not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="File provided is not an image.")
image_bytes = await file.read()
try:
prediction_result = predict(model, image_bytes, CLASS_NAMES)
return prediction_result
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error during prediction: {str(e)}") |