import gradio as gr import pandas as pd import lightgbm as lgb from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder # Load dataset url = "https://raw.githubusercontent.com/Pushpinder-Singh06/CSV-Files/refs/heads/main/crop_cleaned%20data.csv " data = pd.read_csv(url) # Define the crops in Punjabi crops = [ 'rice', 'maize', 'chickpea', 'kidneybeans', 'pigeonpeas', 'mothbeans', 'mungbean', 'blackgram', 'lentil', 'pomegranate', 'banana', 'mango', 'watermelon', 'muskmelon', 'orange', 'papaya', 'coconut', 'cotton', 'jute', 'coffee' ] # Fit the label encoder le = LabelEncoder() le.fit(data['label']) # โœ… Fit first data['crop_encoded'] = le.transform(data['label']) # โœ… Then transform # Prepare for reverse prediction reverse_X = data[['crop_encoded']] y_cols = ['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall'] # Train reverse models reverse_models = {} for col in y_cols: y = data[col] X_train, X_test, y_train, y_test = train_test_split(reverse_X, y, test_size=0.2, random_state=42) model_r = lgb.LGBMRegressor() model_r.fit(X_train, y_train) reverse_models[col] = model_r # Mapping from crop name to encoded value label_to_encoded = {label: le.transform([label])[0] for label in le.classes_} # Reverse prediction function def predict_crop_parameters(crop_name): crop_name = crop_name.strip() if crop_name not in label_to_encoded: return f"โŒ No data found for '{crop_name}'." encoded_value = label_to_encoded[crop_name] input_data = [[encoded_value]] predictions = {} for param, model_r in reverse_models.items(): predicted_value = model_r.predict(input_data)[0] predictions[param] = round(predicted_value, 2) formatted_output = "" for k, v in predictions.items(): formatted_output += f"{k}: {v}
" return formatted_output with gr.Blocks() as demo: gr.Markdown("# ๐ŸŒพ **AgroVision: Crop-Based Parameters**") crop_input = gr.Dropdown(choices=crops, label="๐ŸŒฟ Select Crop Name") result_output = gr.Markdown(label="๐Ÿงช Recommended Parameters") run_btn = gr.Button("โžก๏ธ Predict Parameters") run_btn.click(fn=predict_crop_parameters, inputs=[crop_input], outputs=[result_output]) demo.launch()