Update app.py
Browse files
app.py
CHANGED
@@ -1,75 +1,75 @@
|
|
1 |
-
from flask import Flask, request, render_template, jsonify
|
2 |
-
import joblib
|
3 |
-
import pandas as pd
|
4 |
-
|
5 |
-
app = Flask(__name__)
|
6 |
-
|
7 |
-
# Load the trained LightGBM model
|
8 |
-
lgb_model = joblib.load('lgb_model_cropseason.pkl')
|
9 |
-
|
10 |
-
url='https://drive.google.com/file/d/1_vd4HISZB2h2--CiXKezeWDXHHo2fY23/view?usp=sharing'
|
11 |
-
data = pd.read_csv('https://drive.usercontent.google.com/download?id={}&export=download&authuser=0&confirm=t'.format(url.split('/')[-2]))
|
12 |
-
|
13 |
-
# Extract unique values for states, districts, crops, and seasons
|
14 |
-
unique_states = data['State'].unique()
|
15 |
-
unique_crops = data['Crop'].unique()
|
16 |
-
|
17 |
-
# Descriptions for each cropping season
|
18 |
-
season_descriptions = {
|
19 |
-
'Kharif': 'Kharif season occurs from June to October, associated with the monsoon. Crops are usually sown at the start of the rainy season.',
|
20 |
-
'Rabi': 'Rabi season spans from October to March, during the winter cropping season, with crops like wheat and barley.',
|
21 |
-
'Summer': 'Summer season is from April to June, suitable for crops that need warmer temperatures.',
|
22 |
-
'Winter': 'Winter cropping season occurs from November to February, including cold-weather crops.',
|
23 |
-
'Whole Year': 'Crops can be grown throughout the year, without seasonal limitations.',
|
24 |
-
'Autumn': 'Autumn season, from September to November, accommodates crops suited to a post-monsoon environment.'
|
25 |
-
}
|
26 |
-
|
27 |
-
@app.route('/')
|
28 |
-
def home():
|
29 |
-
return render_template('index.html', states=unique_states, crops=unique_crops, seasons=season_descriptions.keys())
|
30 |
-
|
31 |
-
@app.route('/filter_districts', methods=['POST'])
|
32 |
-
def filter_districts():
|
33 |
-
state = request.form.get('state')
|
34 |
-
filtered_districts = data[data['State'] == state]['District'].unique()
|
35 |
-
return jsonify({'districts': list(filtered_districts)})
|
36 |
-
|
37 |
-
@app.route('/predict', methods=['POST'])
|
38 |
-
def predict():
|
39 |
-
state = request.form.get('state')
|
40 |
-
district = request.form.get('district')
|
41 |
-
crop_year = int(request.form.get('crop_year'))
|
42 |
-
crop = request.form.get('crop')
|
43 |
-
area = float(request.form.get('area'))
|
44 |
-
|
45 |
-
input_data = pd.DataFrame({
|
46 |
-
'State': [state],
|
47 |
-
'District': [district],
|
48 |
-
'Crop_Year': [crop_year],
|
49 |
-
'Crop': [crop],
|
50 |
-
'Area': [area]
|
51 |
-
})
|
52 |
-
|
53 |
-
input_data['State'] = input_data['State'].astype('category')
|
54 |
-
input_data['District'] = input_data['District'].astype('category')
|
55 |
-
input_data['Crop'] = input_data['Crop'].astype('category')
|
56 |
-
|
57 |
-
predicted_season = lgb_model.predict(input_data)[0]
|
58 |
-
|
59 |
-
# Debug: print the predicted season to console
|
60 |
-
print(f"Predicted Season: {predicted_season}")
|
61 |
-
|
62 |
-
# Ensure the predicted season is treated as a string for matching
|
63 |
-
predicted_season_str = str(predicted_season) # Ensure it's a stri
|
64 |
-
|
65 |
-
# Check if the predicted season is in the descriptions
|
66 |
-
if predicted_season in season_descriptions:
|
67 |
-
season_description = season_descriptions[predicted_season]
|
68 |
-
else:
|
69 |
-
season_description = 'No description available'
|
70 |
-
|
71 |
-
return render_template('index.html', states=unique_states, crops=unique_crops, seasons=season_descriptions.keys(),
|
72 |
-
predicted_season=predicted_season, season_description=season_description)
|
73 |
-
|
74 |
-
if __name__ == '__main__':
|
75 |
-
app.run(
|
|
|
1 |
+
from flask import Flask, request, render_template, jsonify
|
2 |
+
import joblib
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Load the trained LightGBM model
|
8 |
+
lgb_model = joblib.load('lgb_model_cropseason.pkl')
|
9 |
+
|
10 |
+
url='https://drive.google.com/file/d/1_vd4HISZB2h2--CiXKezeWDXHHo2fY23/view?usp=sharing'
|
11 |
+
data = pd.read_csv('https://drive.usercontent.google.com/download?id={}&export=download&authuser=0&confirm=t'.format(url.split('/')[-2]))
|
12 |
+
|
13 |
+
# Extract unique values for states, districts, crops, and seasons
|
14 |
+
unique_states = data['State'].unique()
|
15 |
+
unique_crops = data['Crop'].unique()
|
16 |
+
|
17 |
+
# Descriptions for each cropping season
|
18 |
+
season_descriptions = {
|
19 |
+
'Kharif': 'Kharif season occurs from June to October, associated with the monsoon. Crops are usually sown at the start of the rainy season.',
|
20 |
+
'Rabi': 'Rabi season spans from October to March, during the winter cropping season, with crops like wheat and barley.',
|
21 |
+
'Summer': 'Summer season is from April to June, suitable for crops that need warmer temperatures.',
|
22 |
+
'Winter': 'Winter cropping season occurs from November to February, including cold-weather crops.',
|
23 |
+
'Whole Year': 'Crops can be grown throughout the year, without seasonal limitations.',
|
24 |
+
'Autumn': 'Autumn season, from September to November, accommodates crops suited to a post-monsoon environment.'
|
25 |
+
}
|
26 |
+
|
27 |
+
@app.route('/')
|
28 |
+
def home():
|
29 |
+
return render_template('index.html', states=unique_states, crops=unique_crops, seasons=season_descriptions.keys())
|
30 |
+
|
31 |
+
@app.route('/filter_districts', methods=['POST'])
|
32 |
+
def filter_districts():
|
33 |
+
state = request.form.get('state')
|
34 |
+
filtered_districts = data[data['State'] == state]['District'].unique()
|
35 |
+
return jsonify({'districts': list(filtered_districts)})
|
36 |
+
|
37 |
+
@app.route('/predict', methods=['POST'])
|
38 |
+
def predict():
|
39 |
+
state = request.form.get('state')
|
40 |
+
district = request.form.get('district')
|
41 |
+
crop_year = int(request.form.get('crop_year'))
|
42 |
+
crop = request.form.get('crop')
|
43 |
+
area = float(request.form.get('area'))
|
44 |
+
|
45 |
+
input_data = pd.DataFrame({
|
46 |
+
'State': [state],
|
47 |
+
'District': [district],
|
48 |
+
'Crop_Year': [crop_year],
|
49 |
+
'Crop': [crop],
|
50 |
+
'Area': [area]
|
51 |
+
})
|
52 |
+
|
53 |
+
input_data['State'] = input_data['State'].astype('category')
|
54 |
+
input_data['District'] = input_data['District'].astype('category')
|
55 |
+
input_data['Crop'] = input_data['Crop'].astype('category')
|
56 |
+
|
57 |
+
predicted_season = lgb_model.predict(input_data)[0]
|
58 |
+
|
59 |
+
# Debug: print the predicted season to console
|
60 |
+
print(f"Predicted Season: {predicted_season}")
|
61 |
+
|
62 |
+
# Ensure the predicted season is treated as a string for matching
|
63 |
+
predicted_season_str = str(predicted_season) # Ensure it's a stri
|
64 |
+
|
65 |
+
# Check if the predicted season is in the descriptions
|
66 |
+
if predicted_season in season_descriptions:
|
67 |
+
season_description = season_descriptions[predicted_season]
|
68 |
+
else:
|
69 |
+
season_description = 'No description available'
|
70 |
+
|
71 |
+
return render_template('index.html', states=unique_states, crops=unique_crops, seasons=season_descriptions.keys(),
|
72 |
+
predicted_season=predicted_season, season_description=season_description)
|
73 |
+
|
74 |
+
if __name__ == '__main__':
|
75 |
+
app.run(port=7860,host='0.0.0.0')
|