Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| import joblib | |
| # Load trained KNN model | |
| model, target_names = joblib.load("iris_knn.pkl") | |
| def predict_iris(sepal_length, sepal_width, petal_length, petal_width): | |
| arr = np.array([[sepal_length, sepal_width, petal_length, petal_width]]) | |
| pred = model.predict(arr)[0] | |
| proba = model.predict_proba(arr)[0] | |
| return str(target_names[pred]), {str(target_names[i]): float(proba[i]) for i in range(len(target_names))} | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# πΈ Iris Detector β KNN Classifier (k=5)") | |
| gr.Markdown("Enter 4 iris flower measurements below to predict the species:") | |
| with gr.Row(): | |
| with gr.Column(): | |
| sepal_length = gr.Number(label="Sepal Length (cm)") | |
| sepal_width = gr.Number(label="Sepal Width (cm)") | |
| petal_length = gr.Number(label="Petal Length (cm)") | |
| petal_width = gr.Number(label="Petal Width (cm)") | |
| predict_btn = gr.Button("Predict") | |
| output_class = gr.Label(label="Predicted Class") | |
| output_proba = gr.JSON(label="Probabilities") | |
| predict_btn.click( | |
| fn=predict_iris, | |
| inputs=[sepal_length, sepal_width, petal_length, petal_width], | |
| outputs=[output_class, output_proba] | |
| ) | |
| with gr.Column(): | |
| gr.Markdown( | |
| """ | |
| ## π Iris Detector API Usage (FastAPI) | |
| Your predictions can also be made programmatically using the FastAPI backend deployed at: | |
| ### **API Endpoint** | |
| ``` | |
| POST https://tofighi-iris-detector-api.hf.space/predict | |
| ``` | |
| --- | |
| ### **π JSON Request Example** | |
| ```json | |
| { | |
| "sepal_length": 5.1, | |
| "sepal_width": 3.5, | |
| "petal_length": 1.4, | |
| "petal_width": 0.2 | |
| } | |
| ``` | |
| --- | |
| ### **π Python Example** | |
| ```python | |
| import requests | |
| url = "https://tofighi-iris-detector-api.hf.space/predict" | |
| data = { | |
| "sepal_length": 5.1, | |
| "sepal_width": 3.5, | |
| "petal_length": 1.4, | |
| "petal_width": 0.2 | |
| } | |
| resp = requests.post(url, json=data) | |
| print(resp.json()) | |
| ``` | |
| --- | |
| ### **π» cURL Example** | |
| ```bash | |
| curl -X POST "https://tofighi-iris-detector-api.hf.space/predict" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2}' | |
| ``` | |
| """) | |
| demo.launch() |