Lucky Sharma commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,35 @@
|
|
| 1 |
-
from flask import Flask, render_template, request
|
| 2 |
import joblib
|
| 3 |
import numpy as np
|
| 4 |
from sklearn import datasets
|
|
|
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
try:
|
| 12 |
-
# Try loading the new model first, then fall back to the original model
|
| 13 |
-
if os.path.exists('new_iris_model.pkl'):
|
| 14 |
-
model = joblib.load('new_iris_model.pkl')
|
| 15 |
-
print("Successfully loaded new_iris_model.pkl")
|
| 16 |
-
else:
|
| 17 |
-
model = joblib.load('iris_model.pkl')
|
| 18 |
-
print("Successfully loaded iris_model.pkl")
|
| 19 |
-
|
| 20 |
-
# Load iris dataset to get target names
|
| 21 |
-
iris = datasets.load_iris()
|
| 22 |
-
class_names = iris.target_names
|
| 23 |
-
print(f"Class names: {class_names}")
|
| 24 |
-
|
| 25 |
-
except Exception as e:
|
| 26 |
-
print(f"Error loading model or dataset: {e}")
|
| 27 |
-
# Fallback to class names if model fails to load
|
| 28 |
-
class_names = ['setosa', 'versicolor', 'virginica']
|
| 29 |
-
model = None
|
| 30 |
|
| 31 |
-
|
| 32 |
-
def
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
# Make prediction
|
| 50 |
-
prediction = model.predict([features])[0]
|
| 51 |
-
|
| 52 |
-
# Get the class name (flower species)
|
| 53 |
-
species = class_names[prediction]
|
| 54 |
-
|
| 55 |
-
# Capitalize the species name for display
|
| 56 |
-
species_display = f"Iris {species}"
|
| 57 |
-
|
| 58 |
-
# Print debug info
|
| 59 |
-
print(f"Input features: {features}")
|
| 60 |
-
print(f"Prediction: {prediction}, Species: {species_display}")
|
| 61 |
-
|
| 62 |
-
return render_template('result.html', prediction=species_display)
|
| 63 |
-
|
| 64 |
-
except Exception as e:
|
| 65 |
-
error_message = f"Error making prediction: {str(e)}"
|
| 66 |
-
print(error_message)
|
| 67 |
-
return render_template('result.html', prediction="Error: Could not make prediction", error=error_message)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
|
|
|
|
|
| 1 |
import joblib
|
| 2 |
import numpy as np
|
| 3 |
from sklearn import datasets
|
| 4 |
+
import gradio as gr
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Load the model and class names
|
| 8 |
+
if os.path.exists('new_iris_model.pkl'):
|
| 9 |
+
model = joblib.load('new_iris_model.pkl')
|
| 10 |
+
else:
|
| 11 |
+
model = joblib.load('iris_model.pkl')
|
| 12 |
|
| 13 |
+
iris = datasets.load_iris()
|
| 14 |
+
class_names = iris.target_names
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Define prediction function
|
| 17 |
+
def predict_species(sepal_length, sepal_width, petal_length, petal_width):
|
| 18 |
+
features = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
|
| 19 |
+
prediction = model.predict(features)[0]
|
| 20 |
+
return f"Iris {class_names[prediction]}"
|
| 21 |
|
| 22 |
+
# Create Gradio interface
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=predict_species,
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.Number(label="Sepal Length"),
|
| 27 |
+
gr.Number(label="Sepal Width"),
|
| 28 |
+
gr.Number(label="Petal Length"),
|
| 29 |
+
gr.Number(label="Petal Width")
|
| 30 |
+
],
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="Iris Flower Classifier"
|
| 33 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
demo.launch()
|
|
|