Rahul23232's picture
Create app.py
46967b7 verified
Raw
History Blame Contribute Delete
1.58 kB
import gradio as gr
import numpy as np
import pickle
# Load trained model
model = pickle.load(open("model.pkl", "rb"))
# Class labels
classes = ["🌸 Setosa", "🌼 Versicolor", "🌺 Virginica"]
# Prediction function
def predict(sepal_length, sepal_width, petal_length, petal_width):
try:
features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
prediction = model.predict(features)[0]
probabilities = model.predict_proba(features)[0]
result = f"Prediction: {classes[prediction]}\n\n"
result += "Confidence:\n"
for i, prob in enumerate(probabilities):
result += f"{classes[i]}: {round(prob*100, 2)}%\n"
return result
except Exception as e:
return f"Error: {str(e)}"
# Gradio UI
with gr.Blocks(title="Iris Flower Classifier") as demo:
gr.Markdown("## 🌸 Iris Flower Prediction App")
gr.Markdown("Enter flower measurements to predict the species")
with gr.Row():
sepal_length = gr.Number(label="Sepal Length (cm)")
sepal_width = gr.Number(label="Sepal Width (cm)")
with gr.Row():
petal_length = gr.Number(label="Petal Length (cm)")
petal_width = gr.Number(label="Petal Width (cm)")
predict_btn = gr.Button("Predict")
output = gr.Textbox(label="Result")
predict_btn.click(
fn=predict,
inputs=[sepal_length, sepal_width, petal_length, petal_width],
outputs=output
)
# Launch (important for Hugging Face)
if __name__ == "__main__":
demo.launch()