Jayanthk2004 commited on
Commit
7d39c70
·
verified ·
1 Parent(s): 63f7ade

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import joblib
4
+ import pickle
5
+
6
+ # Load the trained model
7
+ with open('Fertilizer_recommender.pkl', 'rb') as f:
8
+ model = pickle.load(f)
9
+
10
+ # Step 4: Define prediction function with data preprocessing
11
+ def recommend_fertilizer(temperature, humidity, moisture, soil_type, crop_type, nitrogen, phosphorous, potassium):
12
+ # Example mappings for soil type and crop type (replace with your actual mappings)
13
+ soil_type_mapping = {"Sandy": 1, "Clay": 2, "Loam": 3}
14
+ crop_type_mapping = {"Wheat": 1, "Rice": 2, "Maize": 3}
15
+
16
+ # Convert soil_type and crop_type to numerical values using the mappings
17
+ soil_type_numerical = soil_type_mapping.get(soil_type, -1) # -1 for unknown soil type
18
+ crop_type_numerical = crop_type_mapping.get(crop_type, -1) # -1 for unknown crop type
19
+
20
+ # Prepare the input data for the model
21
+ input_data = [[temperature, humidity, moisture, soil_type_numerical, crop_type_numerical, nitrogen, phosphorous, potassium]]
22
+
23
+ # Make the prediction
24
+ prediction = model.predict(input_data)
25
+ return prediction[0]
26
+
27
+ # Step 5: Create Gradio interface
28
+ iface = gr.Interface(
29
+ fn=recommend_fertilizer,
30
+ inputs=[
31
+ gr.Number(label="Temperature"),
32
+ gr.Number(label="Humidity"),
33
+ gr.Number(label="Moisture"),
34
+ gr.Textbox(label="Soil Type"), # Use Textbox for soil type input
35
+ gr.Textbox(label="Crop Type"), # Use Textbox for crop type input
36
+ gr.Number(label="Nitrogen"),
37
+ gr.Number(label="Phosphorous"),
38
+ gr.Number(label="Potassium"),
39
+ ],
40
+ outputs=gr.Text(label="Recommended Fertilizer"),
41
+ title="Fertilizer Recommender",
42
+ description="Enter the environmental and crop details to get the best fertilizer recommendation.",
43
+ api_name="/api/predict_fertilizer"
44
+ )
45
+
46
+ # Step 6: Launch the app with show_error enabled
47
+ iface.launch(show_error=True) # Added show_error=True to see detailed error messages