-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
393 lines (329 loc) · 14.2 KB
/
Copy pathapp.py
File metadata and controls
393 lines (329 loc) · 14.2 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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 starlette.responses import HTMLResponse, RedirectResponse
from uvicorn import run as app_run
from typing import Optional
from us_visa_approval_prediction.constants import APP_HOST, APP_PORT
from us_visa_approval_prediction.pipeline.prediction_pipeline import USvisaData, USvisaClassifier
from us_visa_approval_prediction.pipeline.training_pipeline import TrainingPipeline
import os
import requests
from dotenv import load_dotenv
from fastapi import HTTPException, status, Query
from fastapi.responses import FileResponse
import json
import pickle
import tempfile
from datetime import datetime
from zoneinfo import ZoneInfo
from fastapi import HTTPException
from us_visa_approval_prediction.cloud_storage.gdrive_storage import GoogleDriveStorageService
from us_visa_approval_prediction.entity.estimator import USvisaModel
# --- 1️⃣ Load .env ---
load_dotenv()
TRAIN_PASS = os.getenv("TRAIN_PASS")
TOKEN_URL = os.getenv("TOKEN_URL")
TOKEN_DIR = "gdrive_setup"
TOKEN_PATH = os.path.join(TOKEN_DIR, "token.pickle")
# Create directory if it doesn't exist
os.makedirs(TOKEN_DIR, exist_ok=True)
# Download token.pickle only if not exists
if not os.path.exists(TOKEN_PATH):
try:
response = requests.get(TOKEN_URL)
response.raise_for_status()
with open(TOKEN_PATH, "wb") as f:
f.write(response.content)
print(f"token.pickle downloaded to {TOKEN_PATH}")
except Exception as e:
print(f"Failed to download token.pickle: {e}")
raise
else:
print(f"token.pickle already exists at {TOKEN_PATH}, skipping download.")
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory='templates')
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class DataForm:
def __init__(self, request: Request):
self.request: Request = request
self.continent: Optional[str] = None
self.education_of_employee: Optional[str] = None
self.has_job_experience: Optional[str] = None
self.requires_job_training: Optional[str] = None
self.no_of_employees: Optional[str] = None
self.company_age: Optional[str] = None
self.region_of_employment: Optional[str] = None
self.prevailing_wage: Optional[str] = None
self.unit_of_wage: Optional[str] = None
self.full_time_position: Optional[str] = None
async def get_usvisa_data(self):
form = await self.request.form()
self.continent = form.get("continent")
self.education_of_employee = form.get("education_of_employee")
self.has_job_experience = form.get("has_job_experience")
self.requires_job_training = form.get("requires_job_training")
self.no_of_employees = form.get("no_of_employees")
self.company_age = form.get("company_age")
self.region_of_employment = form.get("region_of_employment")
self.prevailing_wage = form.get("prevailing_wage")
self.unit_of_wage = form.get("unit_of_wage")
self.full_time_position = form.get("full_time_position")
@app.get("/", tags=["authentication"])
async def index(request: Request):
return templates.TemplateResponse(
"index.html",{"request": request, "context": "Rendering"})
@app.get("/drift-report")
async def get_drift_report():
report_path = "us_visa_approval_prediction/notebooks/visa_data_drift_report.html"
if not os.path.exists(report_path):
raise HTTPException(status_code=404, detail="Drift report not found")
return FileResponse(report_path, media_type="text/html")
@app.get("/train")
async def trainRouteClient(password: str = Query(..., description="Training password")):
if password != TRAIN_PASS:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid password for training"
)
try:
train_pipeline = TrainingPipeline()
train_pipeline.run_pipeline()
return Response("Training successful !!")
except Exception as e:
return Response(f"Error Occurred! {e}")
@app.post("/")
async def predictRouteClient(request: Request):
try:
form = DataForm(request)
await form.get_usvisa_data()
# Convert numeric fields to proper types
try:
no_of_employees = int(form.no_of_employees) if form.no_of_employees else None
company_age = int(form.company_age) if form.company_age else None
prevailing_wage = float(form.prevailing_wage) if form.prevailing_wage else None
except ValueError as ve:
raise HTTPException(status_code=400, detail=f"Invalid numeric input: {ve}")
usvisa_data = USvisaData(
continent= form.continent,
education_of_employee = form.education_of_employee,
has_job_experience = form.has_job_experience,
requires_job_training = form.requires_job_training,
no_of_employees= no_of_employees,
company_age= company_age,
region_of_employment = form.region_of_employment,
prevailing_wage= prevailing_wage,
unit_of_wage= form.unit_of_wage,
full_time_position= form.full_time_position,
)
usvisa_df = usvisa_data.get_usvisa_input_data_frame()
model_predictor = USvisaClassifier()
value = model_predictor.predict(dataframe=usvisa_df)[0]
status = None
if value == 1:
status = "Visa-approved"
else:
status = "Visa Not-Approved"
return HTMLResponse(f'<h1 class="display-4">Visa Prediction Status: {status}</h1>')
except Exception as e:
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
# Model reports
BDT = ZoneInfo("Asia/Dhaka")
def bdt_now_iso() -> str:
"""Return current time in Bangladesh as ISO string."""
return datetime.now(BDT).isoformat()
@app.get("/model-report")
async def get_model_report():
"""
Comprehensive model report with metrics, features, model info.
"""
try:
gdrive = GoogleDriveStorageService(folder_name="Visa Approval ML Project")
report = {
"timestamp": bdt_now_iso(),
"model_info": {},
"metrics": {},
"features": {},
"model_health": {},
"training_info": {},
}
# 1️⃣ Model health
try:
if gdrive.gdrive_file_exists("model.pkl"):
model = gdrive.load_model("model.pkl")
report["model_health"] = {
"model_exists": True,
"model_loadable": True,
"status": "healthy",
}
if hasattr(model, "trained_model_object"):
mobj = model.trained_model_object
report["model_info"]["model_type"] = type(mobj).__name__
report["model_info"]["model_class"] = str(type(mobj))
if hasattr(mobj, "get_params"):
try:
report["model_info"]["parameters"] = mobj.get_params()
except Exception:
report["model_info"]["parameters"] = "Unable to extract"
if hasattr(model, "preprocessing_object"):
report["model_info"]["preprocessing_type"] = type(
model.preprocessing_object
).__name__
else:
report["model_health"] = {
"model_exists": False,
"model_loadable": False,
"status": "unhealthy",
"message": "model.pkl not found",
}
except Exception as e:
report["model_health"] = {"status": "unhealthy", "error": str(e)}
# 2️⃣ Metrics
try:
if gdrive.gdrive_file_exists("metrics.json"):
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
tmp.close()
try:
gdrive.download_file("metrics.json", tmp.name)
with open(tmp.name, "r", encoding="utf-8") as f:
report["metrics"] = json.load(f)
finally:
os.unlink(tmp.name)
else:
report["metrics"] = {"error": "metrics.json not found"}
except Exception as e:
report["metrics"] = {"error": str(e)}
# 3️⃣ Features
try:
if gdrive.gdrive_file_exists("feature_names.pkl"):
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pkl")
tmp.close()
try:
gdrive.download_file("feature_names.pkl", tmp.name)
with open(tmp.name, "rb") as f:
names = pickle.load(f)
report["features"] = {
"feature_names": list(names)
if hasattr(names, "__iter__")
else [str(names)],
"total_features": len(names)
if hasattr(names, "__len__")
else 0,
}
finally:
os.unlink(tmp.name)
else:
report["features"] = {"error": "feature_names.pkl not found"}
except Exception as e:
report["features"] = {"error": str(e)}
# 4️⃣ Training info
metrics = report.get("metrics", {})
if isinstance(metrics, dict):
if "best_parameters" in metrics:
report["training_info"]["best_parameters"] = metrics["best_parameters"]
if "best_cv_score" in metrics:
report["training_info"]["cross_validation_score"] = metrics[
"best_cv_score"
]
return report
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to generate report: {e}")
@app.get("/model-metrics")
async def get_model_metrics():
"""Return only metrics (timestamp in BDT)."""
try:
gdrive = GoogleDriveStorageService(folder_name="Visa Approval ML Project")
if not gdrive.gdrive_file_exists("metrics.json"):
raise HTTPException(status_code=404, detail="metrics.json not found")
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".json")
tmp.close()
try:
gdrive.download_file("metrics.json", tmp.name)
with open(tmp.name, "r", encoding="utf-8") as f:
metrics = json.load(f)
finally:
os.unlink(tmp.name)
return {"timestamp": bdt_now_iso(), "metrics": metrics}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error reading metrics: {e}")
@app.get("/model-features")
async def get_model_features():
"""Return feature list and count."""
try:
gdrive = GoogleDriveStorageService(folder_name="Visa Approval ML Project")
if not gdrive.gdrive_file_exists("feature_names.pkl"):
raise HTTPException(status_code=404, detail="feature_names.pkl not found")
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pkl")
tmp.close()
try:
gdrive.download_file("feature_names.pkl", tmp.name)
with open(tmp.name, "rb") as f:
names = pickle.load(f)
finally:
os.unlink(tmp.name)
return {
"timestamp": bdt_now_iso(),
"feature_names": list(names)
if hasattr(names, "__iter__")
else [str(names)],
"total_features": len(names) if hasattr(names, "__len__") else 1,
"feature_types": {"categorical": [], "numerical": [], "encoded": []},
}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error reading features: {e}")
@app.get("/model-info")
async def get_model_info():
"""Return model architecture and preprocessing info."""
try:
gdrive = GoogleDriveStorageService(folder_name="Visa Approval ML Project")
if not gdrive.gdrive_file_exists("model.pkl"):
raise HTTPException(status_code=404, detail="model.pkl not found")
model = gdrive.load_model("model.pkl")
info = {
"timestamp": bdt_now_iso(),
"model_architecture": {},
"preprocessing": {},
"configuration": {},
}
if hasattr(model, "trained_model_object"):
mobj = model.trained_model_object
info["model_architecture"]["type"] = type(mobj).__name__
info["model_architecture"]["full_class"] = str(type(mobj))
if hasattr(mobj, "get_params"):
try:
info["configuration"]["parameters"] = mobj.get_params()
except Exception:
info["configuration"]["parameters"] = "Unable to extract"
if hasattr(mobj, "n_estimators"):
info["model_architecture"]["n_estimators"] = mobj.n_estimators
if hasattr(mobj, "max_depth"):
info["model_architecture"]["max_depth"] = mobj.max_depth
if hasattr(model, "preprocessing_object"):
pobj = model.preprocessing_object
info["preprocessing"]["type"] = type(pobj).__name__
if hasattr(pobj, "steps"):
info["preprocessing"]["pipeline_steps"] = [
{"name": n, "transformer": type(t).__name__}
for n, t in pobj.steps
]
return info
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error reading model info: {e}")
if __name__ == "__main__":
app_run(app, host=APP_HOST, port=APP_PORT)