|
a |
|
b/main.py |
|
|
1 |
import joblib |
|
|
2 |
from fastapi import FastAPI, Request, Form |
|
|
3 |
from fastapi.responses import HTMLResponse |
|
|
4 |
from fastapi.staticfiles import StaticFiles |
|
|
5 |
from fastapi.templating import Jinja2Templates |
|
|
6 |
from pydantic import BaseModel |
|
|
7 |
import uvicorn |
|
|
8 |
# Load the saved model |
|
|
9 |
model = joblib.load('cancer_model.pkl') |
|
|
10 |
import sklearn |
|
|
11 |
print(sklearn.__version__) |
|
|
12 |
# Define the input data schema |
|
|
13 |
class PredictionInput(BaseModel): |
|
|
14 |
gender: str = Form() |
|
|
15 |
age: int = Form() |
|
|
16 |
smoking: str = Form() |
|
|
17 |
yellowFingers: str = Form() |
|
|
18 |
anxiety: str = Form() |
|
|
19 |
peerPressure: str = Form() |
|
|
20 |
chronicDisease: str = Form() |
|
|
21 |
fatigue: str = Form() |
|
|
22 |
allergy: str = Form() |
|
|
23 |
wheezing: str = Form() |
|
|
24 |
alcoholConsuming: str = Form() |
|
|
25 |
coughing: str = Form() |
|
|
26 |
shortnessOfBreath: str = Form() |
|
|
27 |
swallowingDifficulty: str = Form() |
|
|
28 |
chestPain: str = Form() |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
# Create the FastAPI application |
|
|
32 |
app = FastAPI() |
|
|
33 |
templates = Jinja2Templates(directory="static") |
|
|
34 |
|
|
|
35 |
# Mount the static files directory to serve the HTML and CSS files |
|
|
36 |
app.mount("/static", StaticFiles(directory="static"), name="static") |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
# Define the prediction route |
|
|
40 |
@app.get("/", response_class=HTMLResponse) |
|
|
41 |
async def home(request: Request): |
|
|
42 |
return templates.TemplateResponse("/index.html", {"request": request}) |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
@app.post("/predict") |
|
|
46 |
async def predict_lung_cancer( |
|
|
47 |
request: Request, |
|
|
48 |
gender: str = Form(), |
|
|
49 |
age: int = Form(), |
|
|
50 |
smoking: str = Form(), |
|
|
51 |
yellowFingers: str = Form(), |
|
|
52 |
anxiety: str = Form(), |
|
|
53 |
peerPressure: str = Form(), |
|
|
54 |
chronicDisease: str = Form(), |
|
|
55 |
fatigue: str = Form(), |
|
|
56 |
allergy: str = Form(), |
|
|
57 |
wheezing: str = Form(), |
|
|
58 |
alcoholConsuming: str = Form(), |
|
|
59 |
coughing: str = Form(), |
|
|
60 |
shortnessOfBreath: str = Form(), |
|
|
61 |
swallowingDifficulty: str = Form(), |
|
|
62 |
chestPain: str = Form() |
|
|
63 |
): |
|
|
64 |
# Convert the input data to a dictionary |
|
|
65 |
|
|
|
66 |
input_data = { |
|
|
67 |
# "gender":gender, |
|
|
68 |
# "age":float(age), |
|
|
69 |
# "smoking":float(smoking), |
|
|
70 |
"yellowFingers":float(yellowFingers), |
|
|
71 |
"anxiety":float(anxiety), |
|
|
72 |
"peerPressure":float(peerPressure), |
|
|
73 |
"chronicDisease":float(chronicDisease), |
|
|
74 |
"fatigue":float(fatigue), |
|
|
75 |
"allergy":float(allergy), |
|
|
76 |
"wheezing":float(wheezing), |
|
|
77 |
"alcoholConsuming":float(alcoholConsuming), |
|
|
78 |
"coughing":float(coughing), |
|
|
79 |
"shortnessOfBreath":float(shortnessOfBreath), |
|
|
80 |
"swallowingDifficulty":float(swallowingDifficulty), |
|
|
81 |
"chestPain":float(chestPain) |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
print(type(input_data)) |
|
|
85 |
|
|
|
86 |
# Prepare the input features for prediction |
|
|
87 |
input_features = [input_data[col] for col in input_data] |
|
|
88 |
|
|
|
89 |
# Make the prediction |
|
|
90 |
prediction = model.predict([input_features])[0] |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
# Return the prediction as a JSON response |
|
|
95 |
return {"prediction": int(prediction)} |
|
|
96 |
if __name__ == "__main__": |
|
|
97 |
uvicorn.run(app, host="0.0.0.0", port=8000) |