isitraghav commited on
Commit
4d6c1d0
·
1 Parent(s): f5ae842
Files changed (1) hide show
  1. app.py +84 -63
app.py CHANGED
@@ -1,51 +1,60 @@
1
- import gradio as gr
2
  import pandas as pd
3
  from sklearn.preprocessing import LabelEncoder
 
4
 
5
  # Load data
6
- data = pd.read_csv('chittor_final1.csv')
7
-
8
- # Encoding
9
- encode_soil = LabelEncoder()
10
- data['Soil_Type'] = encode_soil.fit_transform(data['Soil_type'])
11
-
12
- encode_crop = LabelEncoder()
13
- data['Crop_Type'] = encode_crop.fit_transform(data['Crop_type'])
14
 
15
- # nutrient thresholds
16
- thresholds = {
17
- 'Avail_P': 10,
18
- 'Exch_K': 50,
19
- 'Avail_Ca': 200,
20
- 'Avail_Mg': 50,
21
- 'Avail_S': 10,
22
- 'Avail_Zn': 1,
23
- 'Avail_B': 0.5,
24
- 'Avail_Fe': 4,
25
- 'Avail_Cu': 0.3,
26
- 'Avail_Mn': 5
27
- }
28
 
29
- # application rates
30
- application_rates = {
31
- 'P': 30,
32
- 'K': 50,
33
- 'Ca': 40,
34
- 'Mg': 20,
35
- 'S': 25,
36
- 'Zn': 5,
37
- 'B': 2,
38
- 'Fe': 10,
39
- 'Cu': 1,
40
- 'Mn': 4
41
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- # soil density and depth
44
- soil_density = 1800
45
- soil_depth = 0.2
 
 
46
 
47
- # calculate amounts
48
- def get_fertilizer_recommendation(row, land_size_m2, fallow_years):
49
  deficiencies = []
50
  fertilizer_amounts = {}
51
  for nutrient, threshold in thresholds.items():
@@ -56,30 +65,42 @@ def get_fertilizer_recommendation(row, land_size_m2, fallow_years):
56
  total_amount = base_amount_per_m2 * land_size_m2 * (1 + 0.1 * fallow_years)
57
  fertilizer_amounts[nutrient_name] = round(total_amount, 2)
58
  if deficiencies:
59
- return 'Fertilizer needed for'+ ', '.join(deficiencies), fertilizer_amounts
60
  else:
61
- return 'No deficiency', {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # Gradio app
64
- demo = gr.Interface(
65
- fn=lambda soil_type, crop_type, land_size_m2, fallow_years: get_fertilizer_recommendation(
66
- data[(data['Soil_Type'] == encode_soil.transform([soil_type])[0]) & (data['Crop_Type'] == encode_crop.transform([crop_type])[0])].iloc[0],
67
- land_size_m2,
68
- fallow_years
69
- ),
70
- inputs=[
71
- gr.Dropdown(list(data['Soil_type'].unique()), label="Soil Type"), # Convert the numpy array to a list
72
- gr.Dropdown(list(data['Crop_type'].unique()), label="Crop Type"), # Convert the numpy array to a list
73
- gr.Number(label="Land Size (m²)"),
74
- gr.Number(label="Fallow Years")
75
- ],
76
- outputs=[
77
- gr.Textbox(label="Recommendation"),
78
- gr.JSON(label="Fertilizer Amounts (in kg)")
79
- ],
80
- title="Fertilizer Recommendation App",
81
- description="Get fertilizer recommendations based on soil type, crop type, land size, and fallow years."
82
- )
83
 
84
  if __name__ == "__main__":
85
- demo.launch()
 
 
1
  import pandas as pd
2
  from sklearn.preprocessing import LabelEncoder
3
+ import gradio as gr
4
 
5
  # Load data
6
+ def load_data(file_path):
7
+ try:
8
+ data = pd.read_csv(file_path)
9
+ return data
10
+ except Exception as e:
11
+ print(f"Error loading data: {e}")
12
+ return None
 
13
 
14
+ # Encode soil and crop types
15
+ def encode_soil_crop(data):
16
+ encode_soil = LabelEncoder()
17
+ data['Soil_Type'] = encode_soil.fit_transform(data['Soil_type'])
18
+ encode_crop = LabelEncoder()
19
+ data['Crop_Type'] = encode_crop.fit_transform(data['Crop_type'])
20
+ return data
 
 
 
 
 
 
21
 
22
+ # Define nutrient thresholds and application rates
23
+ def define_thresholds_application_rates():
24
+ thresholds = {
25
+ 'Avail_P': 10,
26
+ 'Exch_K': 50,
27
+ 'Avail_Ca': 200,
28
+ 'Avail_Mg': 50,
29
+ 'Avail_S': 10,
30
+ 'Avail_Zn': 1,
31
+ 'Avail_B': 0.5,
32
+ 'Avail_Fe': 4,
33
+ 'Avail_Cu': 0.3,
34
+ 'Avail_N': 5
35
+ }
36
+ application_rates = {
37
+ 'P': 30,
38
+ 'K': 50,
39
+ 'Ca': 40,
40
+ 'Mg': 20,
41
+ 'S': 25,
42
+ 'Zn': 5,
43
+ 'B': 2,
44
+ 'Fe': 10,
45
+ 'Cu': 1,
46
+ 'N': 4
47
+ }
48
+ return thresholds, application_rates
49
 
50
+ # Define soil density and depth
51
+ def define_soil_density_depth():
52
+ soil_density = 1800
53
+ soil_depth = 0.2
54
+ return soil_density, soil_depth
55
 
56
+ # Function to get fertilizer recommendation
57
+ def get_fertilizer_recommendation(row, land_size_m2, fallow_years, thresholds, application_rates):
58
  deficiencies = []
59
  fertilizer_amounts = {}
60
  for nutrient, threshold in thresholds.items():
 
65
  total_amount = base_amount_per_m2 * land_size_m2 * (1 + 0.1 * fallow_years)
66
  fertilizer_amounts[nutrient_name] = round(total_amount, 2)
67
  if deficiencies:
68
+ return 'Fertilizer needed for'', '.join(deficiencies), fertilizer_amounts
69
  else:
70
+ return 'No deficiency, Manure Recommended', {}
71
+
72
+ # Gradio application
73
+ def gradio_application():
74
+ file_path = 'chittor_final1.csv'
75
+ data = load_data(file_path)
76
+ if data is not None:
77
+ data = encode_soil_crop(data)
78
+ thresholds, application_rates = define_thresholds_application_rates()
79
+ soil_density, soil_depth = define_soil_density_depth()
80
+
81
+ def fertilizer_recommendation(soil_type_input, crop_type_input, land_size_m2, fallow_years):
82
+ filtered_data = data[(data['Soil_type'] == soil_type_input) & (data['Crop_type'] == crop_type_input)]
83
+ if filtered_data.empty:
84
+ return "No data available for the given soil type and crop type."
85
+ else:
86
+ row = filtered_data.iloc[0]
87
+ recommendation, amounts = get_fertilizer_recommendation(row, land_size_m2, fallow_years, thresholds, application_rates)
88
+ return f"Recommendation: {recommendation}\nFertilizer Amounts (in kg):\n" + "\n".join(f"{nutrient}: {amount} kg" for nutrient, amount in amounts.items())
89
+
90
+ demo = gr.Interface(
91
+ fn=fertilizer_recommendation,
92
+ inputs=[
93
+ gr.Dropdown(label="Soil Type", choices=list(data['Soil_type'].unique())),
94
+ gr.Dropdown(label="Crop Type", choices=list(data['Crop_type'].unique())),
95
+ gr.Number(label="Land Size (m2)"),
96
+ gr.Number(label="Fallow Years")
97
+ ],
98
+ outputs="text",
99
+ title="Fertilizer Recommendation App",
100
+ description="Enter the soil type, crop type, land size, and fallow years to get a fertilizer recommendation."
101
+ )
102
 
103
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  if __name__ == "__main__":
106
+ gradio_application()