-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (47 loc) · 1.63 KB
/
app.py
File metadata and controls
65 lines (47 loc) · 1.63 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 os
from dotenv import load_dotenv
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path
import uvicorn
from src.cnnClassifier.pipeline.prediction import PredictionPipeline
# Load environment variables
load_dotenv()
app = FastAPI(title="Chest Cancer Classification API")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True)
@app.get("/", response_class=HTMLResponse)
async def home():
return Path("templates/index.html").read_text(encoding="utf-8")
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "chest-cancer-classifier"}
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
if not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="File must be an image")
file_path = UPLOAD_DIR / file.filename
try:
with open(file_path, "wb") as f:
f.write(await file.read())
pipeline = PredictionPipeline(filename=str(file_path))
result = pipeline.predict()
return {
"success": True,
"prediction": result[0]["image"],
"filename": file.filename
}
finally:
if file_path.exists():
os.remove(file_path)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
uvicorn.run(app, host="0.0.0.0", port=port)