import gradio as gr import pickle import numpy as np import os # Class for loading model and predicting class CropYieldModel: def __init__(self, model_path): if not os.path.exists(model_path): raise FileNotFoundError(f"Model file not found at {model_path}") with open(model_path, 'rb') as file: self.model = pickle.load(file) def predict(self, feature1, feature2, feature3): try: # Prepare input features features = np.array([[feature1, feature2, feature3]]) prediction = self.model.predict(features) return f"Predicted Crop Yield: {float(prediction[0]):.2f}" except Exception as e: return f"Prediction failed: {str(e)}" # Load model model_path = 'model/crop_yield_model.pkl' # Adjust the path if needed predictor = CropYieldModel(model_path) # Gradio Interface demo = gr.Interface( fn=predictor.predict, inputs=[ gr.Number(label="Feature 1"), gr.Number(label="Feature 2"), gr.Number(label="Feature 3") ], outputs=gr.Textbox(label="Prediction Result"), title="Crop Yield Prediction", description="Enter the values for the features to predict crop yield." ) if __name__ == "__main__": demo.launch()