Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (52 loc) · 1.87 KB
/
Copy pathapp.py
File metadata and controls
65 lines (52 loc) · 1.87 KB
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
60
61
62
63
64
65
import torch
import torchvision.transforms as transforms
from fastapi import FastAPI, File, UploadFile
from PIL import Image
import io
import torchvision.models as models
import os
# Get the current directory of the script
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_PATH = os.path.join(BASE_DIR, "mvtec_model.pth")
# Initialize ResNet18 model with the correct number of output classes (2)
model = models.resnet18(pretrained=False)
model.fc = torch.nn.Linear(512, 2) # Modify the fully connected layer
# Load the saved model weights
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device("cpu")))
# Set model to evaluation mode
model.eval()
# Define transformations (should match validation transforms)
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5])
])
# Initialize FastAPI
app = FastAPI()
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# Enable CORS for all domains (you can replace "*" with your frontend domain)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Or ["https://your-frontend-url.com"] for security
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Root endpoint to prevent 404 errors
@app.get("/")
def home():
return {"message": "Welcome to the FastAPI server!"}
# Prediction endpoint
@app.post("/predict/")
async def predict(file: UploadFile = File(...)):
try:
image = Image.open(io.BytesIO(await file.read())).convert("RGB")
image = transform(image).unsqueeze(0) # Add batch dimension
with torch.no_grad():
output = model(image)
_, predicted = torch.max(output, 1)
result = "Defective" if predicted.item() == 1 else "Good"
return {"prediction": result}
except Exception as e:
return {"error": str(e)}