Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- app.py +136 -0
- crop.ipynb +0 -0
- img.jpg +0 -0
- minmaxscaler.pkl +3 -0
- model.pkl +3 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Set page configuration
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Agricultural AI Assistant and Crop Recommendation",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Load environment variables
|
| 16 |
+
load_dotenv()
|
| 17 |
+
os.environ['GROQ_API_KEY'] = os.getenv("GROQ_API_KEY")
|
| 18 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 19 |
+
chat = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.3-70b-versatile")
|
| 20 |
+
|
| 21 |
+
# Load the model and scaler
|
| 22 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
| 23 |
+
ms = pickle.load(open('minmaxscaler.pkl', 'rb'))
|
| 24 |
+
|
| 25 |
+
# Custom CSS for styling
|
| 26 |
+
st.markdown("""
|
| 27 |
+
<style>
|
| 28 |
+
body {
|
| 29 |
+
background: #BCBBB8;
|
| 30 |
+
}
|
| 31 |
+
.title {
|
| 32 |
+
text-align: center;
|
| 33 |
+
color: mediumseagreen;
|
| 34 |
+
}
|
| 35 |
+
.warning {
|
| 36 |
+
color: red;
|
| 37 |
+
font-weight: bold;
|
| 38 |
+
text-align: center;
|
| 39 |
+
}
|
| 40 |
+
.container {
|
| 41 |
+
background: #edf2f7;
|
| 42 |
+
font-weight: bold;
|
| 43 |
+
padding: 20px;
|
| 44 |
+
border-radius: 15px;
|
| 45 |
+
margin-top: 20px;
|
| 46 |
+
}
|
| 47 |
+
.stButton>button {
|
| 48 |
+
background-color: #007bff;
|
| 49 |
+
color: white;
|
| 50 |
+
font-size: 16px;
|
| 51 |
+
font-weight: bold;
|
| 52 |
+
border: none;
|
| 53 |
+
border-radius: 5px;
|
| 54 |
+
padding: 10px 20px;
|
| 55 |
+
}
|
| 56 |
+
.stTextInput>div>input {
|
| 57 |
+
border-radius: 5px;
|
| 58 |
+
border: 1px solid #007bff;
|
| 59 |
+
padding: 10px;
|
| 60 |
+
}
|
| 61 |
+
</style>
|
| 62 |
+
""", unsafe_allow_html=True)
|
| 63 |
+
|
| 64 |
+
# Initialize session state for chatbot messages
|
| 65 |
+
if 'flow_messages' not in st.session_state:
|
| 66 |
+
st.session_state['flow_messages'] = [
|
| 67 |
+
SystemMessage(content="You are a highly intelligent and friendly agricultural assistant. Provide accurate and relevant answers about crops, farming, and agricultural practices.")
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
# Define the chatbot response function
|
| 71 |
+
def get_response(question):
|
| 72 |
+
st.session_state['flow_messages'].append(HumanMessage(content=question))
|
| 73 |
+
answer = chat(st.session_state['flow_messages'])
|
| 74 |
+
st.session_state['flow_messages'].append(AIMessage(content=answer.content))
|
| 75 |
+
return answer.content
|
| 76 |
+
|
| 77 |
+
# App features
|
| 78 |
+
st.markdown('<h1 class="title">Agricultural AI Assistant 🌾</h1>', unsafe_allow_html=True)
|
| 79 |
+
st.sidebar.header("Features")
|
| 80 |
+
features = st.sidebar.radio("Choose a feature:", ("Crop Recommendation", "Conversational Q&A"))
|
| 81 |
+
|
| 82 |
+
if features == "Crop Recommendation":
|
| 83 |
+
st.write("""
|
| 84 |
+
### Provide the necessary agricultural parameters:
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
# Input fields for the parameters
|
| 88 |
+
N = st.number_input('Nitrogen', min_value=0, max_value=150, step=1)
|
| 89 |
+
P = st.number_input('Phosphorus', min_value=0, max_value=100, step=1)
|
| 90 |
+
K = st.number_input('Potassium', min_value=0, max_value=100, step=1)
|
| 91 |
+
temp = st.number_input('Temperature (°C)', min_value=-10.0, max_value=60.0, step=0.1)
|
| 92 |
+
humidity = st.number_input('Humidity (%)', min_value=0.0, max_value=100.0, step=0.1)
|
| 93 |
+
ph = st.number_input('pH', min_value=0.0, max_value=14.0, step=0.1)
|
| 94 |
+
rainfall = st.number_input('Rainfall (mm)', min_value=0.0, max_value=1000.0, step=1.0)
|
| 95 |
+
|
| 96 |
+
# Button to trigger prediction
|
| 97 |
+
if st.button('Get Recommendation'):
|
| 98 |
+
# Feature list and transformation
|
| 99 |
+
feature_list = [N, P, K, temp, humidity, ph, rainfall]
|
| 100 |
+
single_pred = np.array(feature_list).reshape(1, -1)
|
| 101 |
+
|
| 102 |
+
# Apply scaling
|
| 103 |
+
scaled_features = ms.transform(single_pred)
|
| 104 |
+
|
| 105 |
+
# Make prediction
|
| 106 |
+
prediction = model.predict(scaled_features)
|
| 107 |
+
|
| 108 |
+
# Dictionary to map predictions to crop names
|
| 109 |
+
crop_dict = {
|
| 110 |
+
1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange",
|
| 111 |
+
8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana",
|
| 112 |
+
14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans",
|
| 113 |
+
19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
# Display the result
|
| 117 |
+
if prediction[0] in crop_dict:
|
| 118 |
+
crop = crop_dict[prediction[0]]
|
| 119 |
+
result = f"**{crop}** is the best crop to be cultivated with the provided data."
|
| 120 |
+
st.success(result)
|
| 121 |
+
else:
|
| 122 |
+
result = "Sorry, we could not determine the best crop to be cultivated with the provided data."
|
| 123 |
+
st.error(result)
|
| 124 |
+
|
| 125 |
+
elif features == "Conversational Q&A":
|
| 126 |
+
st.write("""
|
| 127 |
+
### Ask any question about crops, farming, and agriculture:
|
| 128 |
+
""")
|
| 129 |
+
user_input = st.text_input("Your Question:")
|
| 130 |
+
if st.button("Ask Question"):
|
| 131 |
+
if user_input.strip():
|
| 132 |
+
response = get_response(user_input)
|
| 133 |
+
st.subheader("The Response is:")
|
| 134 |
+
st.write(response)
|
| 135 |
+
else:
|
| 136 |
+
st.warning("Please enter a question!")
|
crop.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
img.jpg
ADDED
|
minmaxscaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b075f12ce9474dc2d430a8eac974f4624f0878557846acb2e33bc2bb9209c345
|
| 3 |
+
size 760
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d1b84260229f84bff1f35dfbbf64eb3a84c6db1ab8090878c5a478305c0409d1
|
| 3 |
+
size 3524942
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
numpy
|
| 3 |
+
scikit-learn
|
| 4 |
+
langchain
|
| 5 |
+
langchain-groq
|
| 6 |
+
python-dotenv
|