File size: 2,783 Bytes
44b3ab8 | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | """
Iris Flower Classifier — Web App
================================
A Flask web app that takes flower measurements and predicts the species.
Run with: python app.py
"""
import joblib
import numpy as np
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
# Load trained model artifacts
model = joblib.load('models/iris_model.pkl')
scaler = joblib.load('models/scaler.pkl')
label_encoder = joblib.load('models/label_encoder.pkl')
metadata = joblib.load('models/metadata.pkl')
SPECIES_INFO = {
'Iris-setosa': {
'emoji': '🌸',
'color': '#FF6B6B',
'description': 'Small flowers with short, narrow petals. Found in Arctic and temperate regions.',
},
'Iris-versicolor': {
'emoji': '🌺',
'color': '#4ECDC4',
'description': 'Medium-sized flowers with wider petals. Native to North America.',
},
'Iris-virginica': {
'emoji': '🌷',
'color': '#A06CD5',
'description': 'Large flowers with long, wide petals. Found in eastern North America.',
},
}
@app.route('/')
def index():
return render_template('index.html', model_name=metadata['model_name'],
accuracy=metadata['accuracy'])
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.get_json()
features = np.array([[
float(data['sepal_length']),
float(data['sepal_width']),
float(data['petal_length']),
float(data['petal_width']),
]])
scaled = scaler.transform(features)
prediction = model.predict(scaled)[0]
species = label_encoder.inverse_transform([prediction])[0]
# Get confidence (decision function or probability)
try:
proba = model.predict_proba(scaled)[0]
confidence = float(max(proba)) * 100
all_proba = {label_encoder.inverse_transform([i])[0]: round(float(p) * 100, 1)
for i, p in enumerate(proba)}
except AttributeError:
confidence = 95.0
all_proba = {species: 95.0}
info = SPECIES_INFO.get(species, {})
return jsonify({
'species': species,
'confidence': round(confidence, 1),
'probabilities': all_proba,
'emoji': info.get('emoji', '🌿'),
'color': info.get('color', '#666'),
'description': info.get('description', ''),
})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == '__main__':
print(f"\nIris Classifier Web App")
print(f"Model: {metadata['model_name']} (accuracy: {metadata['accuracy']:.1%})")
print(f"Open: http://localhost:5000\n")
app.run(debug=True, port=5000)
|