File size: 4,869 Bytes
d3f5270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from flask import Flask, render_template, request, jsonify
import joblib
import numpy as np
from sklearn import datasets
import os
import json

app = Flask(__name__)

# Load the model and iris dataset for target names
print("Starting Iris Flower Classification Application...")
try:
    # Try loading the new model first, then fall back to the original model
    if os.path.exists('new_iris_model.pkl'):
        model = joblib.load('new_iris_model.pkl')
        print("Successfully loaded new_iris_model.pkl")
    else:
        model = joblib.load('iris_model.pkl')
        print("Successfully loaded iris_model.pkl")
    
    # Load iris dataset to get target names
    iris = datasets.load_iris()
    class_names = iris.target_names
    print(f"Class names: {class_names}")
    
except Exception as e:
    print(f"Error loading model or dataset: {e}")
    # Fallback to class names if model fails to load
    class_names = ['setosa', 'versicolor', 'virginica']
    model = None

# Additional flower information for enhanced display
flower_info = {
    'setosa': {
        'emoji': '๐ŸŒธ',
        'color': '#FFB6C1',
        'color_name': 'Pink and White',
        'description': 'Small, delicate petals with beautiful pink and white colors',
        'origin': 'North America and eastern Asia',
        'size': 'Small (petals < 2cm)',
        'habitat': 'Cooler climates, Arctic regions',
        'image': 'iris setosa.jpg'
    },
    'versicolor': {
        'emoji': '๐ŸŒบ',
        'color': '#8A2BE2',
        'color_name': 'Blue-Purple',
        'description': 'Medium-sized flowers with stunning blue-purple hues',
        'origin': 'Eastern North America',
        'size': 'Medium (petals 2-4cm)',
        'habitat': 'Wetlands and marshy areas',
        'image': 'iris versicolor.jpg'
    },
    'virginica': {
        'emoji': '๐ŸŒท',
        'color': '#4B0082',
        'color_name': 'Deep Violet-Purple',
        'description': 'Large, magnificent blooms with deep violet-purple colors',
        'origin': 'Eastern North America (Virginia)',
        'size': 'Large (petals > 4cm)',
        'habitat': 'Various soil types, adaptable',
        'image': 'iris verginica.jpg'
    }
}

@app.route('/')
def form():
    return render_template('form.html')

@app.route('/api/flower-info')
def get_flower_info():
    """API endpoint to get flower information"""
    return jsonify(flower_info)

@app.route('/predict', methods=['POST'])
def predict():
    try:
        if model is None:
            raise Exception("Model failed to load")
            
        # Get form values as float
        features = [
            float(request.form['sepal_length']),
            float(request.form['sepal_width']),
            float(request.form['petal_length']),
            float(request.form['petal_width'])
        ]
        
        # Validate input ranges (basic sanity check)
        for i, feature in enumerate(features):
            if feature < 0 or feature > 15:  # Reasonable limits for iris measurements
                raise ValueError(f"Feature {i+1} value {feature} is outside reasonable range (0-15 cm)")
        
        # Make prediction
        prediction = model.predict([features])[0]
        
        # Get prediction probabilities for confidence
        prediction_proba = model.predict_proba([features])[0]
        confidence = max(prediction_proba) * 100
        
        # Get the class name (flower species)
        species = class_names[prediction]
        
        # Capitalize the species name for display
        species_display = f"Iris {species.capitalize()}"
        
        # Get additional flower information
        additional_info = flower_info.get(species, {})
        
        # Print debug info
        print(f"Input features: {features}")
        print(f"Prediction: {prediction}, Species: {species_display}")
        print(f"Confidence: {confidence:.1f}%")
        print(f"Probabilities: {prediction_proba}")
        
        return render_template('result.html', 
                             prediction=species_display,
                             confidence=confidence,
                             features=features,
                             flower_info=additional_info)
    
    except ValueError as ve:
        error_message = f"Invalid input: {str(ve)}"
        print(error_message)
        return render_template('result.html', 
                             prediction="Error: Invalid input values", 
                             error=error_message)
    except Exception as e:
        error_message = f"Error making prediction: {str(e)}"
        print(error_message)
        return render_template('result.html', 
                             prediction="Error: Could not make prediction", 
                             error=error_message)

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True)