File size: 2,875 Bytes
a2e34d4
75c76b9
 
 
 
 
e12f110
8c61977
 
e12f110
 
75c76b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e12f110
 
 
 
 
 
75c76b9
e12f110
 
 
 
 
 
 
 
 
 
 
75c76b9
 
 
 
 
 
 
 
e12f110
75c76b9
 
e12f110
75c76b9
e12f110
 
75c76b9
 
e12f110
75c76b9
e12f110
75c76b9
 
 
 
e12f110
75c76b9
 
 
 
 
 
e12f110
75c76b9
 
 
fa9ee6a
 
 
 
75c76b9
 
 
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
89
90
91
92
93
94
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/sehajpreet22/data/refs/heads/main/cleaned_crop_data_with_pbi_labels.csv"
data = pd.read_csv(url)

# Define the crops in Punjabi
crops = [
    'ਚੌਲ',
    'ਮੱਕੀ',
    'ਛੋਲੇ',
    'ਰਾਜ਼ਮਾ',
    'ਅਰਹਰ ਦੀ ਦਾਲ',
    'ਮੋਠ ਦੀ ਦਾਲ',
    'ਮੂੰਗ ਦੀ ਦਾਲ',
    'ਮਾਂਹ ਦੀ ਦਾਲ',
    'ਮਸਰ ਦੀ ਦਾਲ',
    'ਅਨਾਰ',
    'ਕੇਲਾ',
    'ਅੰਬ',
    'ਤਰਬੂਜ਼',
    'ਖਰਬੂਜ਼ਾ',
    'ਸੰਤਰਾ',
    'ਪਪੀਤਾ',
    'ਨਾਰੀਅਲ',
    'ਕਪਾਹ',
    'ਜੂਟ',
    'ਕੌਫ਼ੀ'
]

# 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 = [
    'ਨਾਈਟ੍ਰੋਜਨ (kg/ha)',
    'ਫਾਸਫੋਰਸ (kg/ha)',
    'ਪੋਟਾਸ਼ੀਅਮ (kg/ha)',
    'ਤਾਪਮਾਨ (°C)',
    'ਨਮੀ (%)',
    'ਮਿੱਟੀ ਦਾ pH',
    'ਵਰਖਾ (mm)'
]

# 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"❌ '<b>{crop_name}</b>' ਲਈ ਡਾਟਾ ਨਹੀਂ ਮਿਲਿਆ।"

    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"<b>{k}</b>: {v}<br>"

    return formatted_output


    
with gr.Blocks() as demo:
    gr.Markdown("# 🌾 **AgroVision: ਫਸਲ ਤੋਂ ਪੈਰਾਮੀਟਰ**")
    crop_input = gr.Dropdown(choices=crops, label="🌿 ਫਸਲ ਦਾ ਨਾਂ ਲਿਖੋ")
    result_output = gr.Markdown(label="🧪 ਅਨੁਕੂਲ ਪੈਰਾਮੀਟਰ")
    run_btn = gr.Button("➡️ ਭਵਿੱਖਬਾਣੀ ਲਵੋ")
    run_btn.click(fn=predict_crop_parameters, inputs=[crop_input], outputs=[result_output])


demo.launch()