Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = 'model/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"),
|
33 |
+
gr.Number(label="Feature 3")
|
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()
|