""" 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)