isitraghav commited on
Commit
4765a68
·
1 Parent(s): 33ad754
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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():
52
+ if row[nutrient] < threshold:
53
+ nutrient_name = nutrient.split('_')[-1]
54
+ deficiencies.append(nutrient_name)
55
+ base_amount_per_m2 = application_rates[nutrient_name] / 10000
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()