Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# -------------------------------
|
| 10 |
+
# 1. Load and Preprocess Data
|
| 11 |
+
# -------------------------------
|
| 12 |
+
file_path = "path_to_your_csv_file.csv" # Replace with your actual file path
|
| 13 |
+
df = pd.read_csv(file_path)
|
| 14 |
+
|
| 15 |
+
# Handle Categorical Columns
|
| 16 |
+
label_encoders = {}
|
| 17 |
+
for col in ['Seed_Variety', 'Irrigation_Schedule']:
|
| 18 |
+
label_encoders[col] = LabelEncoder()
|
| 19 |
+
df[col] = label_encoders[col].fit_transform(df[col])
|
| 20 |
+
|
| 21 |
+
# Normalize Numerical Columns
|
| 22 |
+
scaler = StandardScaler()
|
| 23 |
+
numerical_cols = ['Soil_Quality', 'Fertilizer_Amount_kg_per_hectare', 'Sunny_Days', 'Rainfall_mm']
|
| 24 |
+
df[numerical_cols] = scaler.fit_transform(df[numerical_cols])
|
| 25 |
+
|
| 26 |
+
# Split Dataset
|
| 27 |
+
X = df.drop(columns=['Yield_kg_per_hectare'])
|
| 28 |
+
y = df['Yield_kg_per_hectare']
|
| 29 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 30 |
+
|
| 31 |
+
# -------------------------------
|
| 32 |
+
# 2. Train Model
|
| 33 |
+
# -------------------------------
|
| 34 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 35 |
+
model.fit(X_train, y_train)
|
| 36 |
+
|
| 37 |
+
# -------------------------------
|
| 38 |
+
# 3. Prediction Function
|
| 39 |
+
# -------------------------------
|
| 40 |
+
def predict_yield(soil_quality, seed_variety, fertilizer_amount, sunny_days, rainfall, irrigation_schedule):
|
| 41 |
+
# Preprocess Inputs
|
| 42 |
+
input_data = pd.DataFrame({
|
| 43 |
+
'Soil_Quality': [soil_quality],
|
| 44 |
+
'Seed_Variety': [label_encoders['Seed_Variety'].transform([seed_variety])[0]],
|
| 45 |
+
'Fertilizer_Amount_kg_per_hectare': [fertilizer_amount],
|
| 46 |
+
'Sunny_Days': [sunny_days],
|
| 47 |
+
'Rainfall_mm': [rainfall],
|
| 48 |
+
'Irrigation_Schedule': [label_encoders['Irrigation_Schedule'].transform([irrigation_schedule])[0]],
|
| 49 |
+
})
|
| 50 |
+
input_data[numerical_cols] = scaler.transform(input_data[numerical_cols])
|
| 51 |
+
|
| 52 |
+
# Prediction
|
| 53 |
+
predicted_yield = model.predict(input_data)[0]
|
| 54 |
+
|
| 55 |
+
# Insights (Static Example)
|
| 56 |
+
insight = (
|
| 57 |
+
f"To optimize yield, maintain fertilizer levels around {fertilizer_amount * 1.1:.2f} kg/hectare "
|
| 58 |
+
f"and ensure consistent irrigation on {irrigation_schedule} schedule."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return f"""
|
| 62 |
+
- **Predicted Yield:** {predicted_yield:.2f} kg/hectare
|
| 63 |
+
- **Optimal Fertilizer Usage:** {fertilizer_amount * 1.1:.2f} kg/hectare
|
| 64 |
+
- **Insight:** {insight}
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
# -------------------------------
|
| 68 |
+
# 4. User Interface (Gradio)
|
| 69 |
+
# -------------------------------
|
| 70 |
+
interface = gr.Interface(
|
| 71 |
+
fn=predict_yield,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.Number(label="Soil Quality (0-1 normalized)"),
|
| 74 |
+
gr.Textbox(label="Seed Variety"),
|
| 75 |
+
gr.Number(label="Fertilizer Amount (kg/hectare)"),
|
| 76 |
+
gr.Number(label="Sunny Days"),
|
| 77 |
+
gr.Number(label="Rainfall (mm)"),
|
| 78 |
+
gr.Textbox(label="Irrigation Schedule"),
|
| 79 |
+
],
|
| 80 |
+
outputs="text",
|
| 81 |
+
title="Crop Yield Prediction App",
|
| 82 |
+
description="Enter crop parameters to predict yield and get professional agricultural insights."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Launch App
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
interface.launch()
|