Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import lightgbm as lgb
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
import torch
|
| 8 |
+
from torchvision import models, transforms
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# ---------------------------
|
| 13 |
+
# Reverse Prediction Setup
|
| 14 |
+
# ---------------------------
|
| 15 |
+
|
| 16 |
+
crops = [
|
| 17 |
+
'ਚੌਲ',
|
| 18 |
+
'ਮੱਕੀ',
|
| 19 |
+
'ਛੋਲੇ',
|
| 20 |
+
'ਰਾਜ਼ਮਾ',
|
| 21 |
+
'ਅਰਹਰ ਦੀ ਦਾਲ',
|
| 22 |
+
'ਮੋਠ ਦੀ ਦਾਲ',
|
| 23 |
+
'ਮੂੰਗ ਦੀ ਦਾਲ',
|
| 24 |
+
'ਮਾਂਹ ਦੀ ਦਾਲ',
|
| 25 |
+
'ਮਸਰ ਦੀ ਦਾਲ',
|
| 26 |
+
'ਅਨਾਰ',
|
| 27 |
+
'ਕੇਲਾ',
|
| 28 |
+
'ਅੰਬ',
|
| 29 |
+
'ਤਰਬੂਜ਼',
|
| 30 |
+
'ਖਰਬੂਜ਼ਾ',
|
| 31 |
+
'ਸੰਤਰਾ',
|
| 32 |
+
'ਪਪੀਤਾ',
|
| 33 |
+
'ਨਾਰੀਅਲ',
|
| 34 |
+
'ਕਪਾਹ',
|
| 35 |
+
'ਜੂਟ',
|
| 36 |
+
'ਕੌਫ਼ੀ'
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
data['crop_encoded'] = le.transform(data['label'])
|
| 40 |
+
reverse_X = data[['crop_encoded']]
|
| 41 |
+
y_cols = ['ਨਾਈਟ੍ਰੋਜਨ (kg/ha)', 'ਫਾਸਫੋਰਸ (kg/ha)', 'ਪੋਟਾਸ਼ੀਅਮ (kg/ha)', 'ਤਾਪਮਾਨ (°C)', 'ਨਮੀ (%)', 'ਮਿੱਟੀ ਦਾ pH', 'ਵਰਖਾ (mm)']
|
| 42 |
+
reverse_models = {}
|
| 43 |
+
for col in y_cols:
|
| 44 |
+
y = data[col]
|
| 45 |
+
X_train, X_test, y_train, y_test = train_test_split(reverse_X, y, test_size=0.2, random_state=42)
|
| 46 |
+
model_r = lgb.LGBMRegressor()
|
| 47 |
+
model_r.fit(X_train, y_train)
|
| 48 |
+
reverse_models[col] = model_r
|
| 49 |
+
|
| 50 |
+
label_to_encoded = {label: le.transform([label])[0] for label in le.classes_}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def predict_crop_parameters(crop_name):
|
| 54 |
+
crop_name_lower = crop_name.lower()
|
| 55 |
+
if crop_name_lower not in label_to_encoded:
|
| 56 |
+
return f"❌ '<b>{crop_name}</b>' ਲਈ ਡਾਟਾ ਨਹੀਂ ਮਿਲਿਆ।"
|
| 57 |
+
|
| 58 |
+
encoded_value = label_to_encoded[crop_name_lower]
|
| 59 |
+
input_data = [[encoded_value]]
|
| 60 |
+
|
| 61 |
+
predictions = {}
|
| 62 |
+
for param, model_r in reverse_models.items():
|
| 63 |
+
predicted_value = model_r.predict(input_data)[0]
|
| 64 |
+
predictions[param] = round(predicted_value, 2)
|
| 65 |
+
|
| 66 |
+
formatted_output = ""
|
| 67 |
+
for k, v in predictions.items():
|
| 68 |
+
formatted_output += f"<b>{k}</b>: {v}<br>"
|
| 69 |
+
|
| 70 |
+
return formatted_output
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("# 🌾 **AgroVision: ਫਸਲ ਤੋਂ ਪੈਰਾਮੀਟਰ**")
|
| 75 |
+
crop_input = gr.Dropdown(choices=crops, label="🌿 ਫਸਲ ਦਾ ਨਾਂ ਲਿਖੋ")
|
| 76 |
+
result_output = gr.Markdown(label="🧪 ਅਨੁਕੂਲ ਪੈਰਾਮੀਟਰ")
|
| 77 |
+
run_btn = gr.Button("➡️ ਭਵਿੱਖਬਾਣੀ ਲਵੋ")
|
| 78 |
+
run_btn.click(fn=predict_crop_parameters, inputs=[crop_input], outputs=[result_output])
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
demo.launch()
|