|
|
|
|
|
|
| |
|
|
| from fastapi import FastAPI |
| from pydantic import BaseModel |
| from joblib import load |
| import numpy as np |
| from fastapi.responses import HTMLResponse |
|
|
|
|
| |
| app = FastAPI() |
|
|
| |
| model = load("model.joblib") |
|
|
| |
| class Item(BaseModel): |
| sepal_length: float |
| sepal_width: float |
| petal_length: float |
| petal_width: float |
|
|
| |
| @app.post("/predict") |
| async def predict(item: Item): |
| |
| input_data = [item.sepal_length, item.sepal_width, item.petal_length, item.petal_width] |
| input_array = np.array([input_data]) |
|
|
| |
| prediction = model.predict(input_array)[0] |
|
|
| |
| class_label = {0: "setosa", 1: "versicolor", 2: "virginica"} |
| predicted_class = class_label[prediction] |
|
|
| |
| return {"predicted_class": predicted_class} |
|
|
|
|
| @app.get('/', response_class=HTMLResponse) |
| async def html(): |
| content = open('static/index.html', 'r') |
| return content.read() |
|
|