Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,83 @@
|
|
| 1 |
-
from flask import Flask, render_template, request
|
| 2 |
-
import joblib
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import google.generativeai as genai
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
# Load the trained Random Forest models
|
| 11 |
+
rf_ferti_name = joblib.load('rf_ferti_name.pkl')
|
| 12 |
+
rf_ferti_value = joblib.load('rf_ferti_value.pkl')
|
| 13 |
+
|
| 14 |
+
# Manually define the encodings based on the provided dictionaries
|
| 15 |
+
soil_type_encodings = {'Black': 0, 'Clayey': 1, 'Loamy': 2, 'Red': 3, 'Sandy': 4}
|
| 16 |
+
crop_type_encodings = {'Barley': 0, 'Cotton': 1, 'Ground Nuts': 2, 'Maize': 3, 'Millets': 4,
|
| 17 |
+
'Oil seeds': 5, 'Other Variety': 6, 'Paddy': 7, 'Pulses': 8, 'Sugarcane': 9,
|
| 18 |
+
'Tobacco': 10, 'Wheat': 11}
|
| 19 |
+
fertilizer_name_encodings = {'10-26-26': 0, '14-35-14': 1, '15-15-15': 2, '17-17-17': 3, '20-20': 4,
|
| 20 |
+
'20-20-20': 5, '28-28': 6, 'Ammonium sulfate': 7, 'Biofertilizer (e.g., Rhizobium)': 8,
|
| 21 |
+
'Calcium nitrate': 9, 'DAP': 10, 'Ferrous sulfate': 11, 'Magnesium sulfate': 12,
|
| 22 |
+
'Potassium chloride/Muriate of potash (MOP)': 13, 'Potassium sulfate/Sulfate of potash (SOP)': 14,
|
| 23 |
+
'Rock phosphate (RP)': 15, 'Single superphosphate (SSP)': 16, 'Triple superphosphate (TSP)': 17,
|
| 24 |
+
'Urea': 18, 'Zinc sulfate': 19}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# AI configuration
|
| 28 |
+
api_key=os.getenv('GEMINI_API')
|
| 29 |
+
genai.configure(api_key=api_key)
|
| 30 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 31 |
+
|
| 32 |
+
def generate_ai_suggestions(pred_fertilizer_name):
|
| 33 |
+
prompt = (
|
| 34 |
+
f"For {pred_fertilizer_name} fertlizer, generate 3-4 sentences each on a new line, note text shoudl be jsutidied should not contian anyu special character"
|
| 35 |
+
)
|
| 36 |
+
response = model.generate_content(prompt)
|
| 37 |
+
return response.text
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 42 |
+
def index():
|
| 43 |
+
if request.method == 'POST':
|
| 44 |
+
# Retrieve form data
|
| 45 |
+
temperature = float(request.form['temperature'])
|
| 46 |
+
humidity = float(request.form['humidity'])
|
| 47 |
+
moisture = float(request.form['moisture'])
|
| 48 |
+
soil_type = request.form['soil_type']
|
| 49 |
+
crop_type = request.form['crop_type']
|
| 50 |
+
nitrogen = float(request.form['nitrogen'])
|
| 51 |
+
potassium = float(request.form['potassium'])
|
| 52 |
+
phosphorous = float(request.form['phosphorous'])
|
| 53 |
+
|
| 54 |
+
# Encode categorical data
|
| 55 |
+
soil_type_encoded = soil_type_encodings.get(soil_type, -1)
|
| 56 |
+
crop_type_encoded = crop_type_encodings.get(crop_type, -1)
|
| 57 |
+
|
| 58 |
+
# Create a DataFrame for the input
|
| 59 |
+
user_input = pd.DataFrame({
|
| 60 |
+
'Temperature': [temperature],
|
| 61 |
+
'Humidity': [humidity],
|
| 62 |
+
'Moisture': [moisture],
|
| 63 |
+
'Nitrogen': [nitrogen],
|
| 64 |
+
'Potassium': [potassium],
|
| 65 |
+
'Phosphorous': [phosphorous],
|
| 66 |
+
'Soil Type': [soil_type_encoded],
|
| 67 |
+
'Crop Type': [crop_type_encoded]
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
# Predict Fertilizer Name
|
| 71 |
+
pred_fertilizer_name = rf_ferti_name.predict(user_input)[0]
|
| 72 |
+
pred_fertilizer_name = [name for name, value in fertilizer_name_encodings.items() if value == pred_fertilizer_name][0]
|
| 73 |
+
|
| 74 |
+
# Predict Fertilizer Quantity
|
| 75 |
+
pred_fertilizer_qty = rf_ferti_value.predict(user_input)[0]
|
| 76 |
+
pred_info = generate_ai_suggestions(pred_fertilizer_name)
|
| 77 |
+
|
| 78 |
+
return render_template('index.html', prediction=True, fertilizer_name=pred_fertilizer_name,
|
| 79 |
+
fertilizer_qty=pred_fertilizer_qty, optimal_usage=pred_fertilizer_qty,pred_info=pred_info)
|
| 80 |
+
return render_template('index.html', prediction=False)
|
| 81 |
+
|
| 82 |
+
if __name__ == '__main__':
|
| 83 |
+
app.run(debug=True)
|