Tantawi65
Flatten structure: Move all files from app/ to root directory
2f34ba3
# main.py
import shutil
import os
import uuid
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from predict import predict_image
app = FastAPI(
title="Medical Image Classification API",
description="AI-powered medical image classification service",
version="1.0.0"
)
# Add CORS middleware for Flutter integration
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Create uploads directory in tmp (writable in containers)
import tempfile
UPLOAD_DIR = tempfile.mkdtemp()
os.makedirs(UPLOAD_DIR, exist_ok=True)
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "gp-tea-skin-analysis"}
@app.post("/analyze_image")
async def analyze_image(file: UploadFile = File(...)):
"""Analyze skin image for medical conditions"""
try:
if not file.content_type or not file.content_type.startswith('image/'):
raise HTTPException(status_code=400, detail="File must be an image")
unique_filename = f"{uuid.uuid4().hex}_{file.filename}"
file_path = os.path.join(UPLOAD_DIR, unique_filename)
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
label, confidence, all_predictions = predict_image(file_path)
os.remove(file_path)
formatted_predictions = []
for pred in all_predictions:
formatted_predictions.append({
"label": pred["label"],
"confidence": float(pred["confidence"]),
"confidence_percent": f"{pred['confidence'] * 100:.2f}%"
})
return JSONResponse(
status_code=200,
content={
"success": True,
"prediction": {
"top_prediction": {
"label": label,
"confidence": float(confidence),
"confidence_percent": f"{confidence * 100:.2f}%"
},
"all_predictions": formatted_predictions
}
}
)
except Exception as e:
if 'file_path' in locals() and os.path.exists(file_path):
os.remove(file_path)
raise HTTPException(status_code=500, detail=f"Classification failed: {str(e)}")