-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
181 lines (151 loc) · 5.53 KB
/
app.py
File metadata and controls
181 lines (151 loc) · 5.53 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Main application entry point for the Vehicle Insurance Data Pipeline MLops project.
Handles orchestration of the pipeline and serves as the primary script to run the project.
"""
import os
import traceback
from typing import Optional
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import Response
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from uvicorn import run as app_run
from src.pipline.prediction_pipeline import VehicleData, VehicleDataClassifier
from src.pipline.training_pipeline import TrainPipeline
# -----------------------------
# App + Config
# -----------------------------
APP_HOST = os.getenv("APP_HOST", "0.0.0.0")
APP_PORT = int(os.getenv("APP_PORT", "5000"))
app = FastAPI()
# Docker-safe base directory (this file's folder)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# Static + Templates (absolute paths => stable in Docker)
app.mount(
"/static",
StaticFiles(directory=os.path.join(BASE_DIR, "static")),
name="static",
)
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# -----------------------------
# Health check
# -----------------------------
@app.get("/health")
def health():
return {"status": "ok"}
# -----------------------------
# Form parser
# -----------------------------
class DataForm:
"""
Handles and converts incoming form data to correct numeric types.
"""
def __init__(self, request: Request):
self.request: Request = request
self.Gender: Optional[int] = None
self.Age: Optional[int] = None
self.Driving_License: Optional[int] = None
self.Region_Code: Optional[float] = None
self.Previously_Insured: Optional[int] = None
self.Annual_Premium: Optional[float] = None
self.Policy_Sales_Channel: Optional[float] = None
self.Vintage: Optional[int] = None
self.Vehicle_Age_lt_1_Year: Optional[int] = None
self.Vehicle_Age_gt_2_Years: Optional[int] = None
self.Vehicle_Damage_Yes: Optional[int] = None
async def get_vehicle_data(self):
"""
Fetch form values and convert to correct numeric types.
NOTE: form.get() returns strings -> conversion avoids ML pipeline errors.
"""
form = await self.request.form()
# Convert safely (raise clear error if missing / wrong)
self.Gender = int(form.get("Gender"))
self.Age = int(form.get("Age"))
self.Driving_License = int(form.get("Driving_License"))
self.Region_Code = float(form.get("Region_Code"))
self.Previously_Insured = int(form.get("Previously_Insured"))
self.Annual_Premium = float(form.get("Annual_Premium"))
self.Policy_Sales_Channel = float(form.get("Policy_Sales_Channel"))
self.Vintage = int(form.get("Vintage"))
self.Vehicle_Age_lt_1_Year = int(form.get("Vehicle_Age_lt_1_Year"))
self.Vehicle_Age_gt_2_Years = int(form.get("Vehicle_Age_gt_2_Years"))
self.Vehicle_Damage_Yes = int(form.get("Vehicle_Damage_Yes"))
# -----------------------------
# Routes
# -----------------------------
@app.get("/", tags=["authentication"])
async def index(request: Request):
"""
Renders the main HTML form page.
"""
return templates.TemplateResponse(
"index.html",
{"request": request, "context": "Rendering"},
)
@app.get("/train")
async def trainRouteClient():
"""
Trigger model training pipeline.
"""
try:
train_pipeline = TrainPipeline()
train_pipeline.run_pipeline()
return Response("Training successful!!!")
except Exception as e:
traceback.print_exc()
return Response(f"Error Occurred! {e}", status_code=500)
@app.post("/")
async def predictRouteClient(request: Request):
"""
Receive form data and return prediction on same page.
"""
try:
form = DataForm(request)
await form.get_vehicle_data()
vehicle_data = VehicleData(
Gender=form.Gender,
Age=form.Age,
Driving_License=form.Driving_License,
Region_Code=form.Region_Code,
Previously_Insured=form.Previously_Insured,
Annual_Premium=form.Annual_Premium,
Policy_Sales_Channel=form.Policy_Sales_Channel,
Vintage=form.Vintage,
Vehicle_Age_lt_1_Year=form.Vehicle_Age_lt_1_Year,
Vehicle_Age_gt_2_Years=form.Vehicle_Age_gt_2_Years,
Vehicle_Damage_Yes=form.Vehicle_Damage_Yes,
)
# DataFrame for model
vehicle_df = vehicle_data.get_vehicle_input_data_frame()
# Predictor
model_predictor = VehicleDataClassifier()
value = model_predictor.predict(dataframe=vehicle_df)[0]
status = "Response-Yes" if int(value) == 1 else "Response-No"
return templates.TemplateResponse(
"index.html",
{"request": request, "context": status},
)
except Exception as e:
# Full traceback in docker logs
traceback.print_exc()
return {"status": False, "error": str(e)}
# -----------------------------
# Run app
# -----------------------------
if __name__ == "__main__":
app_run(app, host=APP_HOST, port=APP_PORT)
"""
Use test dataset to see our model prediction result on web page is correct or not.
test dataset can be found in:
artifacts/data_ingestion/../test.csv
"""