-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (115 loc) · 4.18 KB
/
app.py
File metadata and controls
141 lines (115 loc) · 4.18 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
import sys
import shutil
import uvicorn
from dotenv import load_dotenv
from fastapi import FastAPI, File, Request, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates
from src.logger import logging
from src.exception import CustomException
from src.utils.main_utils import load_object
from src.entity.config_entity import ModelTrainerConfig
load_dotenv()
ENV_MODE = os.getenv("ENV_MODE", "dev")
IS_DEV = ENV_MODE == "dev"
if IS_DEV:
HOST = os.getenv("DEV_HOST", "127.0.0.1")
PORT = int(os.getenv("DEV_PORT", "8000"))
else:
HOST = os.getenv("PROD_HOST", "0.0.0.0")
PORT = int(os.getenv("PROD_PORT", "8080"))
templates = Jinja2Templates(directory="templates")
app = FastAPI(
title="Signature Recognition API",
description="Genuine vs Forged signature detection using ResNet-34",
version="1.0.0",
docs_url="/docs" if IS_DEV else None,
redoc_url="/redoc" if IS_DEV else None,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"] if IS_DEV else ["https://yourdomain.com"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/", tags=["Health"])
def home(request: Request):
model_path = ModelTrainerConfig().MODEL_PATH
model_ready = os.path.exists(model_path)
return templates.TemplateResponse(
request=request,
name="index.html",
context={"env_mode": ENV_MODE, "model_ready": model_ready},
)
@app.get("/health", tags=["Health"])
def health():
model_path = ModelTrainerConfig().MODEL_PATH
return {"status": "ok", "env": ENV_MODE, "model_ready": os.path.exists(model_path)}
@app.post("/train", tags=["Training"])
def train():
try:
from src.pipeline.training import TrainingPipeline
logging.info(f"Training triggered via API — env: {ENV_MODE}")
pipeline = TrainingPipeline()
artifacts = pipeline.run_pipeline()
return JSONResponse({
"status": "success",
"message": "Training complete",
"val_accuracy": f"{artifacts.val_accuracy:.4f}",
"model_path": artifacts.model_path,
})
except Exception as e:
raise HTTPException(status_code=500, detail=str(CustomException(e, sys)))
@app.post("/predict", tags=["Prediction"])
async def predict(file: UploadFile = File(...)):
"""Upload a signature image (PNG/JPG) — returns genuine or forged."""
if not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="File must be an image (PNG or JPG)")
model_path = ModelTrainerConfig().MODEL_PATH
if not os.path.exists(model_path):
raise HTTPException(status_code=404, detail="Model not trained yet. Hit /train first.")
import torch
from PIL import Image
from torchvision import transforms
predict_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.Grayscale(num_output_channels=3),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
])
tmp_path = f"tmp_{file.filename}"
try:
with open(tmp_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
model = load_object(model_path)
model.eval()
img = Image.open(tmp_path).convert("RGB")
tensor = predict_transform(img).unsqueeze(0)
with torch.no_grad():
outputs = model(tensor)
probs = torch.softmax(outputs, dim=1)[0]
predicted = outputs.argmax(1).item()
label = "genuine" if predicted == 0 else "forged"
confidence = probs[predicted].item()
return JSONResponse({
"status": "success",
"filename": file.filename,
"prediction": label,
"confidence": f"{confidence:.2%}",
})
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(CustomException(e, sys)))
finally:
if os.path.exists(tmp_path):
os.remove(tmp_path)
if __name__ == "__main__":
uvicorn.run(
"app:app",
host=HOST,
port=PORT,
reload=IS_DEV,
)