Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -82,31 +82,37 @@
|
|
| 82 |
# if __name__ == '__main__':
|
| 83 |
# app.run(port=7860,host='0.0.0.0')
|
| 84 |
|
| 85 |
-
|
| 86 |
import gradio as gr
|
| 87 |
import pandas as pd
|
| 88 |
import joblib
|
| 89 |
import os
|
| 90 |
import google.generativeai as genai
|
| 91 |
|
| 92 |
-
# Load models
|
| 93 |
rf_ferti_name = joblib.load("rf_ferti_name.pkl")
|
| 94 |
rf_ferti_value = joblib.load("rf_ferti_value.pkl")
|
| 95 |
|
| 96 |
# Encodings
|
| 97 |
-
soil_type_encodings = {
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
inv_fert_name_encodings = {v: k for k, v in fertilizer_name_encodings.items()}
|
| 108 |
|
| 109 |
-
#
|
| 110 |
genai.configure(api_key=os.getenv("GEMINI_API"))
|
| 111 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 112 |
|
|
@@ -119,11 +125,11 @@ def generate_ai_suggestions(fertilizer_name):
|
|
| 119 |
return response.text.strip()
|
| 120 |
|
| 121 |
def predict_fertilizer(temp, humidity, moisture, soil_type, crop_type, nitrogen, potassium, phosphorous):
|
| 122 |
-
# Encode categorical
|
| 123 |
soil_encoded = soil_type_encodings.get(soil_type, -1)
|
| 124 |
crop_encoded = crop_type_encodings.get(crop_type, -1)
|
| 125 |
|
| 126 |
-
# Prepare input
|
| 127 |
input_df = pd.DataFrame([{
|
| 128 |
"Temperature": temp,
|
| 129 |
"Humidity": humidity,
|
|
@@ -135,44 +141,43 @@ def predict_fertilizer(temp, humidity, moisture, soil_type, crop_type, nitrogen,
|
|
| 135 |
"Crop Type": crop_encoded,
|
| 136 |
}])
|
| 137 |
|
| 138 |
-
#
|
| 139 |
pred_name_encoded = rf_ferti_name.predict(input_df)[0]
|
| 140 |
fert_name = inv_fert_name_encodings[pred_name_encoded]
|
| 141 |
-
|
| 142 |
-
# Predict quantity
|
| 143 |
fert_qty = rf_ferti_value.predict(input_df)[0]
|
| 144 |
|
| 145 |
-
# AI
|
| 146 |
ai_info = generate_ai_suggestions(fert_name)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
f"
|
| 151 |
-
f"
|
|
|
|
| 152 |
)
|
|
|
|
| 153 |
|
| 154 |
-
# Gradio UI
|
| 155 |
soil_list = list(soil_type_encodings.keys())
|
| 156 |
crop_list = list(crop_type_encodings.keys())
|
| 157 |
|
| 158 |
interface = gr.Interface(
|
| 159 |
fn=predict_fertilizer,
|
| 160 |
inputs=[
|
| 161 |
-
gr.Slider(0, 50, label="Temperature (°C)"),
|
| 162 |
-
gr.Slider(0, 100, label="Humidity (%)"),
|
| 163 |
-
gr.Slider(0, 100, label="Moisture (%)"),
|
| 164 |
-
gr.Dropdown(soil_list, label="Soil Type"),
|
| 165 |
-
gr.Dropdown(crop_list, label="Crop Type"),
|
| 166 |
-
gr.Number(label="Nitrogen"),
|
| 167 |
-
gr.Number(label="Potassium"),
|
| 168 |
-
gr.Number(label="Phosphorous")
|
| 169 |
],
|
| 170 |
-
outputs=gr.Markdown(label="Result"),
|
| 171 |
title="🌿 Fertilizer Recommendation System",
|
| 172 |
-
description="Enter environmental and soil parameters to get
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
-
# API Launch
|
| 176 |
if __name__ == "__main__":
|
| 177 |
interface.launch()
|
| 178 |
-
|
|
|
|
| 82 |
# if __name__ == '__main__':
|
| 83 |
# app.run(port=7860,host='0.0.0.0')
|
| 84 |
|
|
|
|
| 85 |
import gradio as gr
|
| 86 |
import pandas as pd
|
| 87 |
import joblib
|
| 88 |
import os
|
| 89 |
import google.generativeai as genai
|
| 90 |
|
| 91 |
+
# Load trained models
|
| 92 |
rf_ferti_name = joblib.load("rf_ferti_name.pkl")
|
| 93 |
rf_ferti_value = joblib.load("rf_ferti_value.pkl")
|
| 94 |
|
| 95 |
# Encodings
|
| 96 |
+
soil_type_encodings = {
|
| 97 |
+
'Black': 0, 'Clayey': 1, 'Loamy': 2, 'Red': 3, 'Sandy': 4
|
| 98 |
+
}
|
| 99 |
+
crop_type_encodings = {
|
| 100 |
+
'Barley': 0, 'Cotton': 1, 'Ground Nuts': 2, 'Maize': 3, 'Millets': 4,
|
| 101 |
+
'Oil seeds': 5, 'Other Variety': 6, 'Paddy': 7, 'Pulses': 8, 'Sugarcane': 9,
|
| 102 |
+
'Tobacco': 10, 'Wheat': 11
|
| 103 |
+
}
|
| 104 |
+
fertilizer_name_encodings = {
|
| 105 |
+
'10-26-26': 0, '14-35-14': 1, '15-15-15': 2, '17-17-17': 3, '20-20': 4,
|
| 106 |
+
'20-20-20': 5, '28-28': 6, 'Ammonium sulfate': 7, 'Biofertilizer (e.g., Rhizobium)': 8,
|
| 107 |
+
'Calcium nitrate': 9, 'DAP': 10, 'Ferrous sulfate': 11, 'Magnesium sulfate': 12,
|
| 108 |
+
'Potassium chloride/Muriate of potash (MOP)': 13,
|
| 109 |
+
'Potassium sulfate/Sulfate of potash (SOP)': 14, 'Rock phosphate (RP)': 15,
|
| 110 |
+
'Single superphosphate (SSP)': 16, 'Triple superphosphate (TSP)': 17,
|
| 111 |
+
'Urea': 18, 'Zinc sulfate': 19
|
| 112 |
+
}
|
| 113 |
inv_fert_name_encodings = {v: k for k, v in fertilizer_name_encodings.items()}
|
| 114 |
|
| 115 |
+
# Configure Gemini
|
| 116 |
genai.configure(api_key=os.getenv("GEMINI_API"))
|
| 117 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 118 |
|
|
|
|
| 125 |
return response.text.strip()
|
| 126 |
|
| 127 |
def predict_fertilizer(temp, humidity, moisture, soil_type, crop_type, nitrogen, potassium, phosphorous):
|
| 128 |
+
# Encode categorical values
|
| 129 |
soil_encoded = soil_type_encodings.get(soil_type, -1)
|
| 130 |
crop_encoded = crop_type_encodings.get(crop_type, -1)
|
| 131 |
|
| 132 |
+
# Prepare input for model
|
| 133 |
input_df = pd.DataFrame([{
|
| 134 |
"Temperature": temp,
|
| 135 |
"Humidity": humidity,
|
|
|
|
| 141 |
"Crop Type": crop_encoded,
|
| 142 |
}])
|
| 143 |
|
| 144 |
+
# Model predictions
|
| 145 |
pred_name_encoded = rf_ferti_name.predict(input_df)[0]
|
| 146 |
fert_name = inv_fert_name_encodings[pred_name_encoded]
|
|
|
|
|
|
|
| 147 |
fert_qty = rf_ferti_value.predict(input_df)[0]
|
| 148 |
|
| 149 |
+
# AI Suggestions
|
| 150 |
ai_info = generate_ai_suggestions(fert_name)
|
| 151 |
|
| 152 |
+
# Output message
|
| 153 |
+
result = (
|
| 154 |
+
f"🌾 **Recommended Fertilizer**: `{fert_name}`\n"
|
| 155 |
+
f"📦 **Estimated Quantity**: `{fert_qty:.2f} units`\n\n"
|
| 156 |
+
f"📘 **AI Suggestions**:\n{ai_info}"
|
| 157 |
)
|
| 158 |
+
return result
|
| 159 |
|
| 160 |
+
# Gradio UI components
|
| 161 |
soil_list = list(soil_type_encodings.keys())
|
| 162 |
crop_list = list(crop_type_encodings.keys())
|
| 163 |
|
| 164 |
interface = gr.Interface(
|
| 165 |
fn=predict_fertilizer,
|
| 166 |
inputs=[
|
| 167 |
+
gr.Slider(0, 50, label="🌡️ Temperature (°C)"),
|
| 168 |
+
gr.Slider(0, 100, label="💧 Humidity (%)"),
|
| 169 |
+
gr.Slider(0, 100, label="🌱 Moisture (%)"),
|
| 170 |
+
gr.Dropdown(soil_list, label="🧱 Soil Type"),
|
| 171 |
+
gr.Dropdown(crop_list, label="🌾 Crop Type"),
|
| 172 |
+
gr.Number(label="🧪 Nitrogen Level"),
|
| 173 |
+
gr.Number(label="🧪 Potassium Level"),
|
| 174 |
+
gr.Number(label="🧪 Phosphorous Level")
|
| 175 |
],
|
| 176 |
+
outputs=gr.Markdown(label="🧾 Result"),
|
| 177 |
title="🌿 Fertilizer Recommendation System",
|
| 178 |
+
description="Enter environmental and soil parameters to get fertilizer suggestions powered by AI.",
|
| 179 |
+
allow_flagging="never" # Prevent write-permission errors on Hugging Face
|
| 180 |
)
|
| 181 |
|
|
|
|
| 182 |
if __name__ == "__main__":
|
| 183 |
interface.launch()
|
|
|