sankalp2606 commited on
Commit
270da9b
·
verified ·
1 Parent(s): e1cf723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,32 +1,31 @@
1
  import gradio as gr
2
  import pickle
3
  import numpy as np
 
4
  import os
5
 
6
- # Class for loading model and predicting
7
- class CropYieldModel:
8
- def __init__(self, model_path):
9
- if not os.path.exists(model_path):
10
- raise FileNotFoundError(f"Model file not found at {model_path}")
11
- with open(model_path, 'rb') as file:
12
- self.model = pickle.load(file)
13
 
14
- def predict(self, feature1, feature2, feature3):
15
- try:
16
- # Prepare input features
17
- features = np.array([[feature1, feature2, feature3]])
18
- prediction = self.model.predict(features)
19
- return f"Predicted Crop Yield: {float(prediction[0]):.2f}"
20
- except Exception as e:
21
- return f"Prediction failed: {str(e)}"
22
 
23
- # Load model
24
- model_path = 'crop_yield_model.pkl' # Adjust the path if needed
25
- predictor = CropYieldModel(model_path)
 
 
 
 
 
 
 
 
 
26
 
27
  # Gradio Interface
28
  demo = gr.Interface(
29
- fn=predictor.predict,
30
  inputs=[
31
  gr.Number(label="Feature 1"),
32
  gr.Number(label="Feature 2"),
@@ -34,8 +33,8 @@ demo = gr.Interface(
34
  ],
35
  outputs=gr.Textbox(label="Prediction Result"),
36
  title="Crop Yield Prediction",
37
- description="Enter the values for the features to predict crop yield."
38
  )
39
 
40
  if __name__ == "__main__":
41
- demo.launch()
 
1
  import gradio as gr
2
  import pickle
3
  import numpy as np
4
+ import pandas as pd
5
  import os
6
 
7
+ # Load Model
8
+ model_path = "crop_yield_model.pkl"
 
 
 
 
 
9
 
10
+ if not os.path.exists(model_path):
11
+ raise FileNotFoundError(f"Model file not found at {model_path}")
 
 
 
 
 
 
12
 
13
+ with open(model_path, "rb") as file:
14
+ model = pickle.load(file)
15
+
16
+ # Prediction Function
17
+ def predict_yield(feature1, feature2, feature3):
18
+ try:
19
+ # Prepare input
20
+ features = np.array([[feature1, feature2, feature3]])
21
+ prediction = model.predict(features)
22
+ return f"Predicted Crop Yield: {float(prediction[0]):.2f}"
23
+ except Exception as e:
24
+ return f"Error in prediction: {str(e)}"
25
 
26
  # Gradio Interface
27
  demo = gr.Interface(
28
+ fn=predict_yield,
29
  inputs=[
30
  gr.Number(label="Feature 1"),
31
  gr.Number(label="Feature 2"),
 
33
  ],
34
  outputs=gr.Textbox(label="Prediction Result"),
35
  title="Crop Yield Prediction",
36
+ description="Enter the feature values to predict crop yield.",
37
  )
38
 
39
  if __name__ == "__main__":
40
+ demo.launch()