-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (83 loc) · 2.83 KB
/
main.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
import joblib
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
import uvicorn
# Load the saved model
model = joblib.load('cancer_model.pkl')
import sklearn
print(sklearn.__version__)
# Define the input data schema
class PredictionInput(BaseModel):
gender: str = Form()
age: int = Form()
smoking: str = Form()
yellowFingers: str = Form()
anxiety: str = Form()
peerPressure: str = Form()
chronicDisease: str = Form()
fatigue: str = Form()
allergy: str = Form()
wheezing: str = Form()
alcoholConsuming: str = Form()
coughing: str = Form()
shortnessOfBreath: str = Form()
swallowingDifficulty: str = Form()
chestPain: str = Form()
# Create the FastAPI application
app = FastAPI()
templates = Jinja2Templates(directory="static")
# Mount the static files directory to serve the HTML and CSS files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Define the prediction route
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("/index.html", {"request": request})
@app.post("/predict")
async def predict_lung_cancer(
request: Request,
gender: str = Form(),
age: int = Form(),
smoking: str = Form(),
yellowFingers: str = Form(),
anxiety: str = Form(),
peerPressure: str = Form(),
chronicDisease: str = Form(),
fatigue: str = Form(),
allergy: str = Form(),
wheezing: str = Form(),
alcoholConsuming: str = Form(),
coughing: str = Form(),
shortnessOfBreath: str = Form(),
swallowingDifficulty: str = Form(),
chestPain: str = Form()
):
# Convert the input data to a dictionary
input_data = {
# "gender":gender,
# "age":float(age),
# "smoking":float(smoking),
"yellowFingers":float(yellowFingers),
"anxiety":float(anxiety),
"peerPressure":float(peerPressure),
"chronicDisease":float(chronicDisease),
"fatigue":float(fatigue),
"allergy":float(allergy),
"wheezing":float(wheezing),
"alcoholConsuming":float(alcoholConsuming),
"coughing":float(coughing),
"shortnessOfBreath":float(shortnessOfBreath),
"swallowingDifficulty":float(swallowingDifficulty),
"chestPain":float(chestPain)
}
print(type(input_data))
# Prepare the input features for prediction
input_features = [input_data[col] for col in input_data]
# Make the prediction
prediction = model.predict([input_features])[0]
# Return the prediction as a JSON response
return {"prediction": int(prediction)}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)