File size: 1,377 Bytes
04fc35c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import hf_hub_download
import joblib
import pandas as pd
import json

# Load model and config from Hugging Face
repo_id = "abhishek/autotrain-iris-xgboost"

model_path = hf_hub_download(repo_id=repo_id, filename="model.joblib")
config_path = hf_hub_download(repo_id=repo_id, filename="config.json")

# Load the model
model = joblib.load(model_path)

# Load the config to get feature names
with open(config_path, "r") as f:
    config = json.load(f)

feature_names = config["features"]

# Inference function
def predict(sepal_length, sepal_width, petal_length, petal_width):
    input_df = pd.DataFrame([[
        sepal_length, sepal_width, petal_length, petal_width
    ]], columns=feature_names)

    prediction = model.predict(input_df)[0]
    return f"🌸 Predicted species: {prediction}"

# Gradio interface
demo = gr.Interface(
    fn=predict,
    inputs=[
        gr.Slider(4.0, 8.0, step=0.1, label="Sepal Length"),
        gr.Slider(2.0, 5.0, step=0.1, label="Sepal Width"),
        gr.Slider(1.0, 7.0, step=0.1, label="Petal Length"),
        gr.Slider(0.1, 3.0, step=0.1, label="Petal Width"),
    ],
    outputs=gr.Textbox(label="Prediction"),
    title="🌸 Iris Flower Classifier",
    description="Enter flower measurements to predict the species using a model trained with AutoTrain on Hugging Face.",
)

demo.launch()