|
|
import gradio as gr |
|
|
from fastapi import FastAPI |
|
|
import joblib |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
model = joblib.load("iris_model.pkl") |
|
|
|
|
|
|
|
|
|
|
|
class_names = {1: "Iris-setosa", 2: "Iris-versicolor", 3: "Iris-virginica"} |
|
|
|
|
|
|
|
|
def predict_iris(sepal_l, sepal_w, petal_l, petal_w): |
|
|
|
|
|
features = np.array([[sepal_l, sepal_w, petal_l, petal_w]]) |
|
|
|
|
|
|
|
|
prediction = model.predict(features) |
|
|
predicted_class = int(prediction[0]) |
|
|
|
|
|
|
|
|
return class_names.get(predicted_class, "Unknown") |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=predict_iris, |
|
|
inputs=[ |
|
|
gr.Slider(4.0, 8.0, label="Sepal Length (cm)"), |
|
|
gr.Slider(2.0, 4.5, label="Sepal Width (cm)"), |
|
|
gr.Slider(1.0, 7.0, label="Petal Length (cm)"), |
|
|
gr.Slider(0.1, 2.5, label="Petal Width (cm)"), |
|
|
], |
|
|
outputs=gr.Textbox(label="Predicted Species"), |
|
|
title="Iris Species Classifier", |
|
|
description="Slide the values to predict if the flower is Setosa, Versicolor, or Virginica.", |
|
|
theme="soft" |
|
|
) |
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get("/health") |
|
|
def health_check(): |
|
|
return {"status": "online"} |
|
|
|
|
|
|
|
|
app = gr.mount_gradio_app(app, interface, path="/") |