Spaces:
Sleeping
Sleeping
initial commit
Browse files- app/__init__.py +0 -0
- app/__pycache__/__init__.cpython-39.pyc +0 -0
- app/__pycache__/main.cpython-39.pyc +0 -0
- app/main.py +51 -0
- app/model/eye_disease_model.pth +3 -0
- app/utils/__pycache__/image_utils.cpython-39.pyc +0 -0
- app/utils/image_utils.py +53 -0
- model/eye_disease_model.pth +3 -0
- requirements.txt +27 -0
- utils/__pycache__/image_utils.cpython-39.pyc +0 -0
- utils/image_utils.py +53 -0
app/__init__.py
ADDED
|
File without changes
|
app/__pycache__/__init__.cpython-39.pyc
ADDED
|
Binary file (142 Bytes). View file
|
|
|
app/__pycache__/main.cpython-39.pyc
ADDED
|
Binary file (1.47 kB). View file
|
|
|
app/main.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/main.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
+
from app.utils.image_utils import load_model, process_image
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
# Initialize FastAPI app
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# CORS settings for frontend communication
|
| 14 |
+
# Add CORS middleware to allow requests from your frontend
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["http://localhost:5173","https://eye-disease-detection-frontend.vercel.app/"], # Set your frontend URL here
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"], # Allow all HTTP methods (GET, POST, etc.)
|
| 20 |
+
allow_headers=["*"], # Allow all headers
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Model and label setup
|
| 24 |
+
MODEL_PATH = r"D:\College Works\ML_project\Web\api\app\model\eye_disease_model.pth"
|
| 25 |
+
categories = ["cataract", "diabetic_retinopathy", "glaucoma", "normal"]
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
model = load_model(MODEL_PATH)
|
| 29 |
+
except FileNotFoundError as e:
|
| 30 |
+
print(e)
|
| 31 |
+
model = None
|
| 32 |
+
|
| 33 |
+
# Prediction endpoint
|
| 34 |
+
@app.post("/predict")
|
| 35 |
+
async def predict(file: UploadFile = File(...)):
|
| 36 |
+
if model is None:
|
| 37 |
+
raise HTTPException(status_code=500, detail="Model not loaded")
|
| 38 |
+
|
| 39 |
+
contents = await file.read()
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
image = process_image(contents)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
raise HTTPException(status_code=400, detail=f"Invalid image: {e}")
|
| 45 |
+
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
outputs = model(image)
|
| 48 |
+
_, predicted = torch.max(outputs, 1)
|
| 49 |
+
prediction = categories[predicted.item()]
|
| 50 |
+
|
| 51 |
+
return JSONResponse(content={"prediction": prediction})
|
app/model/eye_disease_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:27a2e05d406289e1ac51649c2605d30d32cf917a3c1a9044e03aa8bcbb35f42a
|
| 3 |
+
size 45845690
|
app/utils/__pycache__/image_utils.cpython-39.pyc
ADDED
|
Binary file (1.5 kB). View file
|
|
|
app/utils/image_utils.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import transforms, models
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
|
| 8 |
+
# Model path
|
| 9 |
+
MODEL_PATH = r"D:\College Works\ML_project\Web\api\app\model\eye_disease_model.pth"
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
def load_model(model_path=MODEL_PATH):
|
| 13 |
+
if not os.path.exists(model_path): # Check if model path exists
|
| 14 |
+
raise FileNotFoundError(f"Model file not found at: {model_path}")
|
| 15 |
+
|
| 16 |
+
# Define the model (with custom layers as per your training)
|
| 17 |
+
model = models.resnet18(pretrained=False)
|
| 18 |
+
|
| 19 |
+
# Freeze the layers and modify the final layers
|
| 20 |
+
for param in model.parameters():
|
| 21 |
+
param.requires_grad = False
|
| 22 |
+
|
| 23 |
+
num_ftrs = model.fc.in_features
|
| 24 |
+
model.fc = nn.Sequential(
|
| 25 |
+
nn.Linear(num_ftrs, 512),
|
| 26 |
+
nn.ReLU(),
|
| 27 |
+
nn.Dropout(0.5),
|
| 28 |
+
nn.Linear(512, 4) # Assuming 4 output classes for your eye disease model
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Load the trained weights into the model
|
| 32 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
| 33 |
+
|
| 34 |
+
# Set the model to evaluation mode
|
| 35 |
+
model.eval()
|
| 36 |
+
|
| 37 |
+
return model
|
| 38 |
+
|
| 39 |
+
# Define image transformation (should match test-time transforms)
|
| 40 |
+
def get_transform():
|
| 41 |
+
return transforms.Compose([
|
| 42 |
+
transforms.Resize((224, 224)),
|
| 43 |
+
transforms.ToTensor(),
|
| 44 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 45 |
+
std=[0.229, 0.224, 0.225])
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
# Process the incoming image
|
| 49 |
+
def process_image(contents):
|
| 50 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 51 |
+
transform = get_transform()
|
| 52 |
+
image = transform(image).unsqueeze(0) # Add batch dimension
|
| 53 |
+
return image
|
model/eye_disease_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:27a2e05d406289e1ac51649c2605d30d32cf917a3c1a9044e03aa8bcbb35f42a
|
| 3 |
+
size 45845690
|
requirements.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
annotated-types==0.7.0
|
| 2 |
+
anyio==4.9.0
|
| 3 |
+
click==8.1.8
|
| 4 |
+
colorama==0.4.6
|
| 5 |
+
exceptiongroup==1.2.2
|
| 6 |
+
fastapi==0.115.12
|
| 7 |
+
filelock==3.18.0
|
| 8 |
+
fsspec==2025.3.2
|
| 9 |
+
h11==0.16.0
|
| 10 |
+
idna==3.10
|
| 11 |
+
Jinja2==3.1.6
|
| 12 |
+
MarkupSafe==3.0.2
|
| 13 |
+
mpmath==1.3.0
|
| 14 |
+
networkx==3.2.1
|
| 15 |
+
numpy==2.0.2
|
| 16 |
+
pillow==11.2.1
|
| 17 |
+
pydantic==2.11.4
|
| 18 |
+
pydantic_core==2.33.2
|
| 19 |
+
python-multipart==0.0.20
|
| 20 |
+
sniffio==1.3.1
|
| 21 |
+
starlette==0.46.2
|
| 22 |
+
sympy==1.14.0
|
| 23 |
+
torch==2.7.0
|
| 24 |
+
torchvision==0.22.0
|
| 25 |
+
typing-inspection==0.4.0
|
| 26 |
+
typing_extensions==4.13.2
|
| 27 |
+
uvicorn==0.34.2
|
utils/__pycache__/image_utils.cpython-39.pyc
ADDED
|
Binary file (1.5 kB). View file
|
|
|
utils/image_utils.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import transforms, models
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
|
| 8 |
+
# Model path
|
| 9 |
+
MODEL_PATH = r"D:\College Works\ML_project\Web\api\app\model\eye_disease_model.pth"
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
def load_model(model_path=MODEL_PATH):
|
| 13 |
+
if not os.path.exists(model_path): # Check if model path exists
|
| 14 |
+
raise FileNotFoundError(f"Model file not found at: {model_path}")
|
| 15 |
+
|
| 16 |
+
# Define the model (with custom layers as per your training)
|
| 17 |
+
model = models.resnet18(pretrained=False)
|
| 18 |
+
|
| 19 |
+
# Freeze the layers and modify the final layers
|
| 20 |
+
for param in model.parameters():
|
| 21 |
+
param.requires_grad = False
|
| 22 |
+
|
| 23 |
+
num_ftrs = model.fc.in_features
|
| 24 |
+
model.fc = nn.Sequential(
|
| 25 |
+
nn.Linear(num_ftrs, 512),
|
| 26 |
+
nn.ReLU(),
|
| 27 |
+
nn.Dropout(0.5),
|
| 28 |
+
nn.Linear(512, 4) # Assuming 4 output classes for your eye disease model
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Load the trained weights into the model
|
| 32 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
| 33 |
+
|
| 34 |
+
# Set the model to evaluation mode
|
| 35 |
+
model.eval()
|
| 36 |
+
|
| 37 |
+
return model
|
| 38 |
+
|
| 39 |
+
# Define image transformation (should match test-time transforms)
|
| 40 |
+
def get_transform():
|
| 41 |
+
return transforms.Compose([
|
| 42 |
+
transforms.Resize((224, 224)),
|
| 43 |
+
transforms.ToTensor(),
|
| 44 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 45 |
+
std=[0.229, 0.224, 0.225])
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
# Process the incoming image
|
| 49 |
+
def process_image(contents):
|
| 50 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 51 |
+
transform = get_transform()
|
| 52 |
+
image = transform(image).unsqueeze(0) # Add batch dimension
|
| 53 |
+
return image
|