Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app (2).py +114 -0
- crop_model_ro (1).txt +0 -0
- requirements.txt +9 -0
app (2).py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import lightgbm as lgb
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
from transformers import pipeline, Conversation, set_seed
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
import google.generativeai as genai
|
| 11 |
+
|
| 12 |
+
# ---------------------------
|
| 13 |
+
# Load environment variables
|
| 14 |
+
# ---------------------------
|
| 15 |
+
load_dotenv()
|
| 16 |
+
GOOGLE_API_KEY = os.getenv("GAPI")
|
| 17 |
+
|
| 18 |
+
# Configure Gemini API
|
| 19 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 20 |
+
gemini_model = genai.GenerativeModel("gemini-pro")
|
| 21 |
+
|
| 22 |
+
# ---------------------------
|
| 23 |
+
# CROP RECOMMENDATION SETUP
|
| 24 |
+
# ---------------------------
|
| 25 |
+
url = "https://raw.githubusercontent.com/89911384/CSV-Files/refs/heads/main/crop_cleaned%20data.csv"
|
| 26 |
+
data = pd.read_csv(url)
|
| 27 |
+
|
| 28 |
+
X = data.drop('label', axis=1)
|
| 29 |
+
y = data['label']
|
| 30 |
+
le = LabelEncoder()
|
| 31 |
+
y_encoded = le.fit_transform(y)
|
| 32 |
+
|
| 33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.3, random_state=0)
|
| 34 |
+
model = lgb.LGBMClassifier()
|
| 35 |
+
model.fit(X_train, y_train)
|
| 36 |
+
|
| 37 |
+
def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
| 38 |
+
input_data = np.array([[N, P, K, temperature, humidity, ph, rainfall]])
|
| 39 |
+
pred = model.predict(input_data)[0]
|
| 40 |
+
crop_name = le.inverse_transform([pred])[0]
|
| 41 |
+
return f"🌾 Recommended Crop: *{crop_name}*"
|
| 42 |
+
|
| 43 |
+
# ---------------------------
|
| 44 |
+
# CHATBOT SETUP
|
| 45 |
+
# ---------------------------
|
| 46 |
+
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 47 |
+
set_seed(42)
|
| 48 |
+
|
| 49 |
+
def chat_with_bot(user_input):
|
| 50 |
+
conversation = Conversation(user_input)
|
| 51 |
+
conversation = chatbot(conversation)
|
| 52 |
+
return conversation.generated_responses[-1]
|
| 53 |
+
|
| 54 |
+
def chat_with_gemini(prompt):
|
| 55 |
+
try:
|
| 56 |
+
response = gemini_model.generate_content(prompt)
|
| 57 |
+
return response.text
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"❌ Error from Gemini: {str(e)}"
|
| 60 |
+
|
| 61 |
+
def smart_chat(user_input, model_choice):
|
| 62 |
+
if model_choice == "Gemini":
|
| 63 |
+
return chat_with_gemini(user_input)
|
| 64 |
+
else:
|
| 65 |
+
return chat_with_bot(user_input)
|
| 66 |
+
|
| 67 |
+
# ---------------------------
|
| 68 |
+
# GRADIO APP UI
|
| 69 |
+
# ---------------------------
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("# 🌱 **AgroVision: Smart Assistant for Farmers**")
|
| 72 |
+
|
| 73 |
+
with gr.Tabs():
|
| 74 |
+
# TAB 1: CROP RECOMMENDATION
|
| 75 |
+
with gr.TabItem("🌾 Crop Recommendation"):
|
| 76 |
+
gr.Markdown("### Enter Soil & Climate Details to Predict Suitable Crop")
|
| 77 |
+
with gr.Row():
|
| 78 |
+
N = gr.Slider(minimum=0, maximum=300, step=1, label="Nitrogen (N) in kg/ha")
|
| 79 |
+
P = gr.Slider(minimum=0, maximum=200, step=1, label="Phosphorus (P) in kg/ha")
|
| 80 |
+
K = gr.Slider(minimum=0, maximum=200, step=1, label="Potassium (K) in kg/ha")
|
| 81 |
+
with gr.Row():
|
| 82 |
+
temperature = gr.Slider(minimum=-10, maximum=50, step=0.1, label="Temperature (°C)")
|
| 83 |
+
humidity = gr.Slider(minimum=0, maximum=100, step=1, label="Humidity (%)")
|
| 84 |
+
with gr.Row():
|
| 85 |
+
ph = gr.Slider(minimum=0, maximum=14, step=0.1, label="Soil pH")
|
| 86 |
+
rainfall = gr.Slider(minimum=0, maximum=500, step=1, label="Rainfall (mm)")
|
| 87 |
+
predict_btn = gr.Button("Predict Crop")
|
| 88 |
+
output_crop = gr.Markdown()
|
| 89 |
+
predict_btn.click(
|
| 90 |
+
fn=predict_crop,
|
| 91 |
+
inputs=[N, P, K, temperature, humidity, ph, rainfall],
|
| 92 |
+
outputs=output_crop
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# TAB 2: CROP DISEASE PREDICTION (Placeholder)
|
| 96 |
+
with gr.TabItem("🌿 Crop Disease Detection"):
|
| 97 |
+
gr.Markdown("### Upload an image of a crop leaf to detect disease (Coming Soon)")
|
| 98 |
+
gr.Image(label="Upload Crop Image", type="filepath")
|
| 99 |
+
gr.Button("Predict Disease (Coming Soon)")
|
| 100 |
+
gr.Textbox(label="Prediction Output", placeholder="Model response will appear here...")
|
| 101 |
+
|
| 102 |
+
# TAB 3: SMART CHATBOT
|
| 103 |
+
with gr.TabItem("💬 Farmer's Chatbot"):
|
| 104 |
+
gr.Markdown("### Ask any question related to farming 👨🌾")
|
| 105 |
+
|
| 106 |
+
with gr.Row():
|
| 107 |
+
user_input = gr.Textbox(label="Your Question")
|
| 108 |
+
model_selector = gr.Dropdown(["Gemini", "DialoGPT"], value="Gemini", label="Select Model")
|
| 109 |
+
chatbot_output = gr.Textbox(label="AgroVision Bot Response")
|
| 110 |
+
chatbot_btn = gr.Button("Ask")
|
| 111 |
+
chatbot_btn.click(fn=smart_chat, inputs=[user_input, model_selector], outputs=chatbot_output)
|
| 112 |
+
|
| 113 |
+
# Launch the app
|
| 114 |
+
demo.launch()
|
crop_model_ro (1).txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
lightgbm
|
| 4 |
+
numpy
|
| 5 |
+
scikit-learn
|
| 6 |
+
transformers==4.29.2
|
| 7 |
+
torch
|
| 8 |
+
google-generativeai
|
| 9 |
+
python-dotenv
|