-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (56 loc) · 2.17 KB
/
app.py
File metadata and controls
71 lines (56 loc) · 2.17 KB
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
import numpy as np
import os
from flask import Flask, request, render_template
import pickle
import os
from tensorflow.keras.models import load_model
app = Flask(__name__)
# Paths
base_path = os.path.dirname(__file__)
model_path = os.path.join(base_path, 'crop_model.h5')
scaler_path = os.path.join(base_path, 'scaler.pkl')
encoder_path = os.path.join(base_path, 'label_encoder.pkl')
# Load model and preprocessing tools
try:
model = load_model(model_path)
with open(scaler_path, 'rb') as f:
scaler = pickle.load(f)
with open(encoder_path, 'rb') as f:
encoder = pickle.load(f)
except Exception as e:
raise Exception(f"Error loading model or tools: {e}")
@app.route('/')
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/FindYourCrop', methods=['GET', 'POST'])
def FindYourCrop():
if request.method == 'POST':
try:
# Get form input
N = float(request.form['N'])
P = float(request.form['P'])
K = float(request.form['K'])
temperature = float(request.form['temperature'])
humidity = float(request.form['humidity'])
ph = float(request.form['ph'])
rainfall = float(request.form['rainfall'])
# Log transform 'K'
K_log = np.log1p(K)
# Feature array
features = np.array([[N, P, K_log, temperature, humidity, ph, rainfall]])
# Scale features
features_scaled = scaler.transform(features)
# Predict
prediction_prob = model.predict(features_scaled)
predicted_class = np.argmax(prediction_prob)
predicted_crop = encoder.inverse_transform([predicted_class])[0]
return render_template('FindYourCrop.html', prediction=predicted_crop)
except Exception as e:
return render_template('FindYourCrop.html', prediction=f"⚠️ Error: {e}")
return render_template('FindYourCrop.html')
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000)) # get port from env or default 5000
app.run(host="0.0.0.0", port=port, debug=True)