Spaces:
Runtime error
Runtime error
File size: 2,520 Bytes
2f34ba3 22a70b4 d82a135 2f34ba3 22a70b4 d82a135 2f34ba3 d82a135 22a70b4 d82a135 22a70b4 2f34ba3 22a70b4 d82a135 22a70b4 d82a135 2f34ba3 d82a135 | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 | # 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)}")
|