File size: 3,183 Bytes
12e43cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import gradio as gr

# -------------------------------
# 1. Load and Preprocess Data
# -------------------------------
file_path = "path_to_your_csv_file.csv"  # Replace with your actual file path
df = pd.read_csv(file_path)

# Handle Categorical Columns
label_encoders = {}
for col in ['Seed_Variety', 'Irrigation_Schedule']:
    label_encoders[col] = LabelEncoder()
    df[col] = label_encoders[col].fit_transform(df[col])

# Normalize Numerical Columns
scaler = StandardScaler()
numerical_cols = ['Soil_Quality', 'Fertilizer_Amount_kg_per_hectare', 'Sunny_Days', 'Rainfall_mm']
df[numerical_cols] = scaler.fit_transform(df[numerical_cols])

# Split Dataset
X = df.drop(columns=['Yield_kg_per_hectare'])
y = df['Yield_kg_per_hectare']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# -------------------------------
# 2. Train Model
# -------------------------------
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# -------------------------------
# 3. Prediction Function
# -------------------------------
def predict_yield(soil_quality, seed_variety, fertilizer_amount, sunny_days, rainfall, irrigation_schedule):
    # Preprocess Inputs
    input_data = pd.DataFrame({
        'Soil_Quality': [soil_quality],
        'Seed_Variety': [label_encoders['Seed_Variety'].transform([seed_variety])[0]],
        'Fertilizer_Amount_kg_per_hectare': [fertilizer_amount],
        'Sunny_Days': [sunny_days],
        'Rainfall_mm': [rainfall],
        'Irrigation_Schedule': [label_encoders['Irrigation_Schedule'].transform([irrigation_schedule])[0]],
    })
    input_data[numerical_cols] = scaler.transform(input_data[numerical_cols])
    
    # Prediction
    predicted_yield = model.predict(input_data)[0]
    
    # Insights (Static Example)
    insight = (
        f"To optimize yield, maintain fertilizer levels around {fertilizer_amount * 1.1:.2f} kg/hectare "
        f"and ensure consistent irrigation on {irrigation_schedule} schedule."
    )
    
    return f"""
- **Predicted Yield:** {predicted_yield:.2f} kg/hectare  
- **Optimal Fertilizer Usage:** {fertilizer_amount * 1.1:.2f} kg/hectare  
- **Insight:** {insight}
"""

# -------------------------------
# 4. User Interface (Gradio)
# -------------------------------
interface = gr.Interface(
    fn=predict_yield,
    inputs=[
        gr.Number(label="Soil Quality (0-1 normalized)"),
        gr.Textbox(label="Seed Variety"),
        gr.Number(label="Fertilizer Amount (kg/hectare)"),
        gr.Number(label="Sunny Days"),
        gr.Number(label="Rainfall (mm)"),
        gr.Textbox(label="Irrigation Schedule"),
    ],
    outputs="text",
    title="Crop Yield Prediction App",
    description="Enter crop parameters to predict yield and get professional agricultural insights."
)

# Launch App
if __name__ == "__main__":
    interface.launch()