-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
121 lines (92 loc) · 4.08 KB
/
app.py
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
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.constants import APP_HOST, APP_PORT
from us_visa.pipline.prediction_pipeline import USvisaData, USvisaClassifier
from us_visa.pipline.training_pipeline import TrainPipeline
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(
"usvisa.html",{"request": request, "context": "Rendering"})
@app.get("/train")
async def trainRouteClient():
try:
train_pipeline = TrainPipeline()
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()
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= form.no_of_employees,
company_age= form.company_age,
region_of_employment = form.region_of_employment,
prevailing_wage= form.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 templates.TemplateResponse(
"usvisa.html",
{"request": request, "context": status},
)
except Exception as e:
return {"status": False, "error": f"{e}"}
if __name__ == "__main__":
app_run(app, host=APP_HOST, port=APP_PORT)