Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 os
|
| 8 |
+
import torch
|
| 9 |
+
from torchvision import models, transforms
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# ---------------------------
|
| 14 |
+
# Crop Recommendation Setup
|
| 15 |
+
# ---------------------------
|
| 16 |
+
url = "https://raw.githubusercontent.com/sehajpreet22/data/refs/heads/main/cleaned_crop_data_with_pbi_labels.csv"
|
| 17 |
+
data = pd.read_csv(url)
|
| 18 |
+
|
| 19 |
+
X = data.drop('label', axis=1)
|
| 20 |
+
y = data['label']
|
| 21 |
+
le = LabelEncoder()
|
| 22 |
+
y_encoded = le.fit_transform(y)
|
| 23 |
+
|
| 24 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.3, random_state=0)
|
| 25 |
+
model = lgb.LGBMClassifier()
|
| 26 |
+
model.fit(X_train, y_train)
|
| 27 |
+
|
| 28 |
+
def predict_crop(ਨਾਈਟ੍ਰੋਜਨ, ਫਾਸਫੋਰਸ, ਪੋਟਾਸ਼ੀਅਮ, ਤਾਪਮਾਨ, ਨਮੀ, ਮਿੱਟੀ_pH, ਵਰਖਾ):
|
| 29 |
+
input_data = np.array([[ਨਾਈਟ੍ਰੋਜਨ, ਫਾਸਫੋਰਸ, ਪੋਟਾਸ਼ੀਅਮ, ਤਾਪਮਾਨ, ਨਮੀ, ਮਿੱਟੀ_pH, ਵਰਖਾ]])
|
| 30 |
+
pred = model.predict(input_data)[0]
|
| 31 |
+
crop_name = le.inverse_transform([pred])[0]
|
| 32 |
+
image_path = f"crop_images/{crop_name}.jpeg"
|
| 33 |
+
if not os.path.exists(image_path):
|
| 34 |
+
image_path = None
|
| 35 |
+
return image_path, f"🌾ਤੁਹਾਡੇ ਖੇਤ ਲਈ ਸੁਝਾਈ ਗਈ ਫਸਲ: *{crop_name}*"
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# 🌾 **ਕਿਹੜੀ ਫਸਲ ਲਾਈਏ?**")
|
| 39 |
+
|
| 40 |
+
with gr.Tabs():
|
| 41 |
+
with gr.TabItem("🌾ਕਿਹੜੀ ਫਸਲ ਲਾਈਏ? "):
|
| 42 |
+
with gr.Row():
|
| 43 |
+
ਨਾਈਟ੍ਰੋਜਨ= gr.Slider(0, 140, step=1, label="ਨਾਈਟ੍ਰੋਜਨ (kg/ha)")
|
| 44 |
+
ਫਾਸਫੋਰਸ= gr.Slider(5, 95, step=1, label="ਫਾਸਫੋਰਸ (kg/ha)")
|
| 45 |
+
ਪੋਟਾਸ਼ੀਅਮ= gr.Slider(5, 82, step=1, label="ਪੋਟਾਸ਼ੀਅਮ (kg/ha)")
|
| 46 |
+
with gr.Row():
|
| 47 |
+
ਤਾਪਮਾਨ= gr.Slider(15.63, 36.32, step=0.1, label="ਤਾਪਮਾਨ (°C)")
|
| 48 |
+
ਨਮੀ= gr.Slider(14.2,99.98 , step=1, label="ਨਮੀ (%)")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
ਮਿੱਟੀ_pH= gr.Slider(0, 14, step=0.1, label="ਮਿੱਟੀ ਦਾ pH")
|
| 51 |
+
ਵਰਖਾ= gr.Slider(20.21, 253.72, step=1, label="ਵਰਖਾ (mm)")
|
| 52 |
+
predict_btn = gr.Button("ਫਸਲ ਦੀ ਭਵਿੱਖਬਾਣੀ ਕਰੋ")
|
| 53 |
+
crop_image_output = gr.Image(label="🌿 ਫਸਲ ਦੀ ਤਸਵੀਰ")
|
| 54 |
+
crop_text_output = gr.Markdown()
|
| 55 |
+
predict_btn.click(fn=predict_crop,
|
| 56 |
+
inputs=[ਨਾਈਟ੍ਰੋਜਨ,ਫਾਸਫੋਰਸ,ਪੋਟਾਸ਼ੀਅਮ,ਤਾਪਮਾਨ,ਨਮੀ,ਮਿੱਟੀ_pH,ਵਰਖਾ],
|
| 57 |
+
outputs=[crop_image_output, crop_text_output])
|
| 58 |
+
demo.launch()
|