-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
43 lines (36 loc) · 1.09 KB
/
app.py
File metadata and controls
43 lines (36 loc) · 1.09 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
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
import joblib
import pandas as pd
app = FastAPI()
# Load the trained model
model = joblib.load('random_forest_resampled_model.pkl')
# Initialize the FastAPI app
app = FastAPI()
# Define the request body using Pydantic
class PredictionRequest(BaseModel):
Temperature_C: float
Humidity: float
eCO2_ppm: float
# Define the prediction endpoint
@app.post("/predict")
def predict(request: PredictionRequest):
# Convert the request data to a DataFrame
data = {
'Temperature[C]': [request.Temperature_C],
'Humidity[%]': [request.Humidity],
'eCO2[ppm]': [request.eCO2_ppm]
}
df = pd.DataFrame(data)
# Make predictions
prediction = model.predict(df)
# Return the prediction as JSON
return {'prediction': int(prediction[0])}
@app.get("/")
async def root():
return RedirectResponse(url="/docs#/default/predict_predict_post")
# Run the app using Uvicorn
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)