Spaces:
Runtime error
Runtime error
Upload 7 files
Browse files- .gitignore +2 -0
- Predictions.ipynb +180 -0
- app.py +123 -0
- explore.ipynb +0 -0
- model_selection.ipynb +818 -0
- requirements.txt +16 -0
- testing_prompts.ipynb +758 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
.env
|
Predictions.ipynb
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"With Random Forest"
|
| 8 |
+
]
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"cell_type": "code",
|
| 12 |
+
"execution_count": null,
|
| 13 |
+
"metadata": {},
|
| 14 |
+
"outputs": [],
|
| 15 |
+
"source": [
|
| 16 |
+
"import pickle\n",
|
| 17 |
+
"import pandas as pd\n",
|
| 18 |
+
"\n",
|
| 19 |
+
"model_path = r\"C:\\Users\\saipr\\Crop_Recommendation\\saved_models\\RF_Model.pkl\"\n",
|
| 20 |
+
"\n",
|
| 21 |
+
"with open(model_path, 'rb') as f:\n",
|
| 22 |
+
" rf_model = pickle.load(f)\n"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": null,
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [
|
| 30 |
+
{
|
| 31 |
+
"name": "stdout",
|
| 32 |
+
"output_type": "stream",
|
| 33 |
+
"text": [
|
| 34 |
+
"Predicted Crops: ['papaya' 'rice' 'rice']\n"
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
],
|
| 38 |
+
"source": [
|
| 39 |
+
"# Example input data \n",
|
| 40 |
+
"data = [\n",
|
| 41 |
+
" {\"N\": 56, \"P\": 48, \"K\": 28, \"temperature\": 28.5, \"humidity\": 89.0, \"ph\": 6.9, \"rainfall\": 220.0},\n",
|
| 42 |
+
" {\"N\": 64, \"P\": 55, \"K\": 33, \"temperature\": 22.0, \"humidity\": 78.0, \"ph\": 7.3, \"rainfall\": 200.0},\n",
|
| 43 |
+
" {\"N\": 98, \"P\": 47, \"K\": 49, \"temperature\": 22.8, \"humidity\": 89.0, \"ph\": 6.1, \"rainfall\": 202.9},\n",
|
| 44 |
+
"]\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"df = pd.DataFrame(data)\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"rf_predictions = rf_model.predict(df)\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"print(\"Predicted Crops:\", rf_predictions)"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "markdown",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"source": [
|
| 57 |
+
"## with SVC"
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"cell_type": "code",
|
| 62 |
+
"execution_count": null,
|
| 63 |
+
"metadata": {},
|
| 64 |
+
"outputs": [],
|
| 65 |
+
"source": [
|
| 66 |
+
"import pickle\n",
|
| 67 |
+
"import pandas as pd\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"model_path = r\"C:\\Users\\saipr\\Crop_Recommendation\\saved_models\\svc_model.pkl\"\n",
|
| 70 |
+
"\n",
|
| 71 |
+
"with open(model_path, 'rb') as f:\n",
|
| 72 |
+
" svc_model = pickle.load(f)\n"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": null,
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [
|
| 80 |
+
{
|
| 81 |
+
"name": "stdout",
|
| 82 |
+
"output_type": "stream",
|
| 83 |
+
"text": [
|
| 84 |
+
"Predicted Crops: ['coffee' 'rice' 'rice']\n"
|
| 85 |
+
]
|
| 86 |
+
}
|
| 87 |
+
],
|
| 88 |
+
"source": [
|
| 89 |
+
"data = [\n",
|
| 90 |
+
" {\"N\": 98, \"P\": 48, \"K\": 35, \"temperature\": 28.5, \"humidity\": 65.0, \"ph\": 6.9, \"rainfall\": 220.0},\n",
|
| 91 |
+
" {\"N\": 64, \"P\": 55, \"K\": 33, \"temperature\": 22.0, \"humidity\": 78.0, \"ph\": 7.3, \"rainfall\": 200.0},\n",
|
| 92 |
+
" {\"N\": 98, \"P\": 47, \"K\": 49, \"temperature\": 22.8, \"humidity\": 89.0, \"ph\": 6.1, \"rainfall\": 202.9},\n",
|
| 93 |
+
"]\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"df = pd.DataFrame(data)\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"svc_predictions = svc_model.predict(df)\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"print(\"Predicted Crops:\", svc_predictions)"
|
| 100 |
+
]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "markdown",
|
| 104 |
+
"metadata": {},
|
| 105 |
+
"source": [
|
| 106 |
+
"Gradient Boosting Model"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"cell_type": "code",
|
| 111 |
+
"execution_count": null,
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [],
|
| 114 |
+
"source": [
|
| 115 |
+
"import pickle\n",
|
| 116 |
+
"import pandas as pd\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"model_path = r\"C:\\Users\\saipr\\Crop_Recommendation\\saved_models\\gb_model.pkl\"\n",
|
| 119 |
+
"\n",
|
| 120 |
+
"with open(model_path, 'rb') as f:\n",
|
| 121 |
+
" gb_model = pickle.load(f)\n"
|
| 122 |
+
]
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"cell_type": "code",
|
| 126 |
+
"execution_count": 19,
|
| 127 |
+
"metadata": {},
|
| 128 |
+
"outputs": [
|
| 129 |
+
{
|
| 130 |
+
"name": "stdout",
|
| 131 |
+
"output_type": "stream",
|
| 132 |
+
"text": [
|
| 133 |
+
"Predicted Crops: ['maize' 'rice' 'rice']\n"
|
| 134 |
+
]
|
| 135 |
+
}
|
| 136 |
+
],
|
| 137 |
+
"source": [
|
| 138 |
+
"data = [\n",
|
| 139 |
+
" {\"N\": 98, \"P\": 48, \"K\": 35, \"temperature\": 28.5, \"humidity\": 65.0, \"ph\": 6.9, \"rainfall\": 100.0},\n",
|
| 140 |
+
" {\"N\": 64, \"P\": 55, \"K\": 33, \"temperature\": 22.0, \"humidity\": 78.0, \"ph\": 7.3, \"rainfall\": 200.0},\n",
|
| 141 |
+
" {\"N\": 98, \"P\": 47, \"K\": 49, \"temperature\": 22.8, \"humidity\": 89.0, \"ph\": 6.1, \"rainfall\": 202.9},\n",
|
| 142 |
+
"]\n",
|
| 143 |
+
"\n",
|
| 144 |
+
"df = pd.DataFrame(data)\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"gb_predictions = gb_model.predict(df)\n",
|
| 147 |
+
"\n",
|
| 148 |
+
"print(\"Predicted Crops:\", gb_predictions)"
|
| 149 |
+
]
|
| 150 |
+
},
|
| 151 |
+
{
|
| 152 |
+
"cell_type": "code",
|
| 153 |
+
"execution_count": null,
|
| 154 |
+
"metadata": {},
|
| 155 |
+
"outputs": [],
|
| 156 |
+
"source": []
|
| 157 |
+
}
|
| 158 |
+
],
|
| 159 |
+
"metadata": {
|
| 160 |
+
"kernelspec": {
|
| 161 |
+
"display_name": "Python 3",
|
| 162 |
+
"language": "python",
|
| 163 |
+
"name": "python3"
|
| 164 |
+
},
|
| 165 |
+
"language_info": {
|
| 166 |
+
"codemirror_mode": {
|
| 167 |
+
"name": "ipython",
|
| 168 |
+
"version": 3
|
| 169 |
+
},
|
| 170 |
+
"file_extension": ".py",
|
| 171 |
+
"mimetype": "text/x-python",
|
| 172 |
+
"name": "python",
|
| 173 |
+
"nbconvert_exporter": "python",
|
| 174 |
+
"pygments_lexer": "ipython3",
|
| 175 |
+
"version": "3.10.0"
|
| 176 |
+
}
|
| 177 |
+
},
|
| 178 |
+
"nbformat": 4,
|
| 179 |
+
"nbformat_minor": 2
|
| 180 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
import os
|
| 5 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
from langchain.chains import LLMChain
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
# Set Streamlit Page Config
|
| 12 |
+
st.set_page_config(
|
| 13 |
+
page_title="Agricultural AI Assistant ๐ฑ",
|
| 14 |
+
layout="wide"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
load_dotenv()
|
| 18 |
+
os.environ['GROQ_API_KEY'] = os.getenv("GROQ_API_KEY")
|
| 19 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 20 |
+
chat = ChatGroq(groq_api_key=groq_api_key, model_name="llama-3.3-70b-versatile")
|
| 21 |
+
|
| 22 |
+
model_path = r"C:\Users\saipr\Crop_Recommendation\saved_models\RF_Model.pkl"
|
| 23 |
+
model = pickle.load(open(model_path, 'rb'))
|
| 24 |
+
|
| 25 |
+
st.markdown("""
|
| 26 |
+
<style>
|
| 27 |
+
.title { text-align: center; color: mediumseagreen; }
|
| 28 |
+
.warning { color: red; font-weight: bold; text-align: center; }
|
| 29 |
+
.container {
|
| 30 |
+
background: #edf2f7; font-weight: bold;
|
| 31 |
+
padding: 20px; border-radius: 15px; margin-top: 20px;
|
| 32 |
+
}
|
| 33 |
+
.stButton>button {
|
| 34 |
+
background-color: #007bff; color: white;
|
| 35 |
+
font-size: 16px; font-weight: bold; border: none;
|
| 36 |
+
border-radius: 5px; padding: 10px 20px;
|
| 37 |
+
}
|
| 38 |
+
.stTextInput>div>input {
|
| 39 |
+
border-radius: 5px; border: 1px solid #007bff; padding: 10px;
|
| 40 |
+
}
|
| 41 |
+
</style>
|
| 42 |
+
""", unsafe_allow_html=True)
|
| 43 |
+
|
| 44 |
+
if 'flow_messages' not in st.session_state:
|
| 45 |
+
st.session_state['flow_messages'] = [
|
| 46 |
+
SystemMessage(content="You are a highly intelligent and friendly agricultural assistant. Provide accurate and relevant answers about crops, farming, and agricultural practices.")
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
def get_response(question):
|
| 50 |
+
st.session_state['flow_messages'].append(HumanMessage(content=question))
|
| 51 |
+
answer = chat(st.session_state['flow_messages'])
|
| 52 |
+
st.session_state['flow_messages'].append(AIMessage(content=answer.content))
|
| 53 |
+
return answer.content
|
| 54 |
+
|
| 55 |
+
st.markdown('<h1 class="title">๐พ Agricultural AI Assistant</h1>', unsafe_allow_html=True)
|
| 56 |
+
st.sidebar.header("๐น Features")
|
| 57 |
+
features = st.sidebar.radio("Choose a feature:", ("Crop Recommendation", "Crop Disease Diagnosis", "Conversational Q&A"))
|
| 58 |
+
|
| 59 |
+
if features == "Crop Recommendation":
|
| 60 |
+
st.write("### ๐ Provide the necessary agricultural parameters:")
|
| 61 |
+
|
| 62 |
+
N = st.number_input('Nitrogen', min_value=0, max_value=150, step=1)
|
| 63 |
+
P = st.number_input('Phosphorus', min_value=0, max_value=100, step=1)
|
| 64 |
+
K = st.number_input('Potassium', min_value=0, max_value=100, step=1)
|
| 65 |
+
temp = st.number_input('Temperature (ยฐC)', min_value=-10.0, max_value=60.0, step=0.1)
|
| 66 |
+
humidity = st.number_input('Humidity (%)', min_value=0.0, max_value=100.0, step=0.1)
|
| 67 |
+
ph = st.number_input('pH', min_value=0.0, max_value=14.0, step=0.1)
|
| 68 |
+
rainfall = st.number_input('Rainfall (mm)', min_value=0.0, max_value=1000.0, step=1.0)
|
| 69 |
+
|
| 70 |
+
if st.button('๐ฑ Get Recommendation'):
|
| 71 |
+
feature_list = [N, P, K, temp, humidity, ph, rainfall]
|
| 72 |
+
single_pred = np.array(feature_list).reshape(1, -1)
|
| 73 |
+
|
| 74 |
+
prediction = model.predict(single_pred)[0]
|
| 75 |
+
|
| 76 |
+
crop = str(prediction).strip().title()
|
| 77 |
+
|
| 78 |
+
st.success(f"๐พ **{crop}** is the best crop for the provided data!")
|
| 79 |
+
|
| 80 |
+
elif features == "Crop Disease Diagnosis":
|
| 81 |
+
st.write("### ๐ฆ Diagnose Crop Diseases")
|
| 82 |
+
|
| 83 |
+
symptoms = st.text_input("๐ Enter Symptoms (e.g., yellow leaves, wilting):")
|
| 84 |
+
crop = st.text_input("๐ฑ Enter Crop Name (e.g., Tomato, Wheat):")
|
| 85 |
+
location = st.text_input("๐ Enter Location (e.g., Punjab, India):")
|
| 86 |
+
season = st.selectbox("๐ Select Season:", ["Summer", "Winter", "Rainy", "Spring", "Autumn"])
|
| 87 |
+
|
| 88 |
+
disease_prompt = PromptTemplate(
|
| 89 |
+
input_variables=["symptoms", "crop", "location", "season"],
|
| 90 |
+
template=(
|
| 91 |
+
"You are an expert plant pathologist assisting farmers in diagnosing crop diseases.\n\n"
|
| 92 |
+
"๐ **Symptoms:** {symptoms}\n"
|
| 93 |
+
"๐ฑ **Crop:** {crop}\n"
|
| 94 |
+
"๐ **Location:** {location}\n"
|
| 95 |
+
"๐ **Season:** {season}\n\n"
|
| 96 |
+
"### ๐ฆ Possible Disease(s) and Causes:\n"
|
| 97 |
+
"- Analyze symptoms and list possible diseases.\n"
|
| 98 |
+
"- Mention environmental and pest-related causes.\n\n"
|
| 99 |
+
"### ๐ Treatment & Remedies:\n"
|
| 100 |
+
"- Suggest **organic** and **chemical** treatments.\n"
|
| 101 |
+
"- Recommend suitable pesticides or fungicides (if needed).\n\n"
|
| 102 |
+
"### ๐ก Preventive Measures:\n"
|
| 103 |
+
"- Guide the farmer on crop rotation, irrigation, and soil treatment.\n"
|
| 104 |
+
"- Suggest resistant crop varieties if available."
|
| 105 |
+
)
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
if st.button("๐ฉบ Diagnose"):
|
| 109 |
+
chain = LLMChain(llm=chat, prompt=disease_prompt)
|
| 110 |
+
response = chain.run(symptoms=symptoms, crop=crop, location=location, season=season)
|
| 111 |
+
st.write(response)
|
| 112 |
+
|
| 113 |
+
elif features == "Conversational Q&A":
|
| 114 |
+
st.write("### ๐ฌ Ask an Agriculture-related Question")
|
| 115 |
+
user_input = st.text_input("Your Question:")
|
| 116 |
+
if st.button("๐ค Ask AI"):
|
| 117 |
+
if user_input.strip():
|
| 118 |
+
response = get_response(user_input)
|
| 119 |
+
st.subheader("AI Response:")
|
| 120 |
+
st.write(response)
|
| 121 |
+
else:
|
| 122 |
+
st.warning("โ ๏ธ Please enter a question!")
|
| 123 |
+
|
explore.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model_selection.ipynb
ADDED
|
@@ -0,0 +1,818 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 25,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import pandas as pd \n",
|
| 10 |
+
"import numpy as np \n",
|
| 11 |
+
"import seaborn as sns\n",
|
| 12 |
+
"import matplotlib.pyplot as plt"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "code",
|
| 17 |
+
"execution_count": 26,
|
| 18 |
+
"metadata": {},
|
| 19 |
+
"outputs": [],
|
| 20 |
+
"source": [
|
| 21 |
+
"df=pd.read_csv(\"C:\\\\Users\\\\saipr\\\\Crop_Recommendation\\\\data\\\\Crop_recommendation.csv\")"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 27,
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"outputs": [
|
| 29 |
+
{
|
| 30 |
+
"data": {
|
| 31 |
+
"text/html": [
|
| 32 |
+
"<div>\n",
|
| 33 |
+
"<style scoped>\n",
|
| 34 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 35 |
+
" vertical-align: middle;\n",
|
| 36 |
+
" }\n",
|
| 37 |
+
"\n",
|
| 38 |
+
" .dataframe tbody tr th {\n",
|
| 39 |
+
" vertical-align: top;\n",
|
| 40 |
+
" }\n",
|
| 41 |
+
"\n",
|
| 42 |
+
" .dataframe thead th {\n",
|
| 43 |
+
" text-align: right;\n",
|
| 44 |
+
" }\n",
|
| 45 |
+
"</style>\n",
|
| 46 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 47 |
+
" <thead>\n",
|
| 48 |
+
" <tr style=\"text-align: right;\">\n",
|
| 49 |
+
" <th></th>\n",
|
| 50 |
+
" <th>N</th>\n",
|
| 51 |
+
" <th>P</th>\n",
|
| 52 |
+
" <th>K</th>\n",
|
| 53 |
+
" <th>temperature</th>\n",
|
| 54 |
+
" <th>humidity</th>\n",
|
| 55 |
+
" <th>ph</th>\n",
|
| 56 |
+
" <th>rainfall</th>\n",
|
| 57 |
+
" <th>label</th>\n",
|
| 58 |
+
" </tr>\n",
|
| 59 |
+
" </thead>\n",
|
| 60 |
+
" <tbody>\n",
|
| 61 |
+
" <tr>\n",
|
| 62 |
+
" <th>0</th>\n",
|
| 63 |
+
" <td>90</td>\n",
|
| 64 |
+
" <td>42</td>\n",
|
| 65 |
+
" <td>43</td>\n",
|
| 66 |
+
" <td>20.879744</td>\n",
|
| 67 |
+
" <td>82.002744</td>\n",
|
| 68 |
+
" <td>6.502985</td>\n",
|
| 69 |
+
" <td>202.935536</td>\n",
|
| 70 |
+
" <td>rice</td>\n",
|
| 71 |
+
" </tr>\n",
|
| 72 |
+
" <tr>\n",
|
| 73 |
+
" <th>1</th>\n",
|
| 74 |
+
" <td>85</td>\n",
|
| 75 |
+
" <td>58</td>\n",
|
| 76 |
+
" <td>41</td>\n",
|
| 77 |
+
" <td>21.770462</td>\n",
|
| 78 |
+
" <td>80.319644</td>\n",
|
| 79 |
+
" <td>7.038096</td>\n",
|
| 80 |
+
" <td>226.655537</td>\n",
|
| 81 |
+
" <td>rice</td>\n",
|
| 82 |
+
" </tr>\n",
|
| 83 |
+
" <tr>\n",
|
| 84 |
+
" <th>2</th>\n",
|
| 85 |
+
" <td>60</td>\n",
|
| 86 |
+
" <td>55</td>\n",
|
| 87 |
+
" <td>44</td>\n",
|
| 88 |
+
" <td>23.004459</td>\n",
|
| 89 |
+
" <td>82.320763</td>\n",
|
| 90 |
+
" <td>7.840207</td>\n",
|
| 91 |
+
" <td>263.964248</td>\n",
|
| 92 |
+
" <td>rice</td>\n",
|
| 93 |
+
" </tr>\n",
|
| 94 |
+
" <tr>\n",
|
| 95 |
+
" <th>3</th>\n",
|
| 96 |
+
" <td>74</td>\n",
|
| 97 |
+
" <td>35</td>\n",
|
| 98 |
+
" <td>40</td>\n",
|
| 99 |
+
" <td>26.491096</td>\n",
|
| 100 |
+
" <td>80.158363</td>\n",
|
| 101 |
+
" <td>6.980401</td>\n",
|
| 102 |
+
" <td>242.864034</td>\n",
|
| 103 |
+
" <td>rice</td>\n",
|
| 104 |
+
" </tr>\n",
|
| 105 |
+
" <tr>\n",
|
| 106 |
+
" <th>4</th>\n",
|
| 107 |
+
" <td>78</td>\n",
|
| 108 |
+
" <td>42</td>\n",
|
| 109 |
+
" <td>42</td>\n",
|
| 110 |
+
" <td>20.130175</td>\n",
|
| 111 |
+
" <td>81.604873</td>\n",
|
| 112 |
+
" <td>7.628473</td>\n",
|
| 113 |
+
" <td>262.717340</td>\n",
|
| 114 |
+
" <td>rice</td>\n",
|
| 115 |
+
" </tr>\n",
|
| 116 |
+
" <tr>\n",
|
| 117 |
+
" <th>...</th>\n",
|
| 118 |
+
" <td>...</td>\n",
|
| 119 |
+
" <td>...</td>\n",
|
| 120 |
+
" <td>...</td>\n",
|
| 121 |
+
" <td>...</td>\n",
|
| 122 |
+
" <td>...</td>\n",
|
| 123 |
+
" <td>...</td>\n",
|
| 124 |
+
" <td>...</td>\n",
|
| 125 |
+
" <td>...</td>\n",
|
| 126 |
+
" </tr>\n",
|
| 127 |
+
" <tr>\n",
|
| 128 |
+
" <th>2195</th>\n",
|
| 129 |
+
" <td>107</td>\n",
|
| 130 |
+
" <td>34</td>\n",
|
| 131 |
+
" <td>32</td>\n",
|
| 132 |
+
" <td>26.774637</td>\n",
|
| 133 |
+
" <td>66.413269</td>\n",
|
| 134 |
+
" <td>6.780064</td>\n",
|
| 135 |
+
" <td>177.774507</td>\n",
|
| 136 |
+
" <td>coffee</td>\n",
|
| 137 |
+
" </tr>\n",
|
| 138 |
+
" <tr>\n",
|
| 139 |
+
" <th>2196</th>\n",
|
| 140 |
+
" <td>99</td>\n",
|
| 141 |
+
" <td>15</td>\n",
|
| 142 |
+
" <td>27</td>\n",
|
| 143 |
+
" <td>27.417112</td>\n",
|
| 144 |
+
" <td>56.636362</td>\n",
|
| 145 |
+
" <td>6.086922</td>\n",
|
| 146 |
+
" <td>127.924610</td>\n",
|
| 147 |
+
" <td>coffee</td>\n",
|
| 148 |
+
" </tr>\n",
|
| 149 |
+
" <tr>\n",
|
| 150 |
+
" <th>2197</th>\n",
|
| 151 |
+
" <td>118</td>\n",
|
| 152 |
+
" <td>33</td>\n",
|
| 153 |
+
" <td>30</td>\n",
|
| 154 |
+
" <td>24.131797</td>\n",
|
| 155 |
+
" <td>67.225123</td>\n",
|
| 156 |
+
" <td>6.362608</td>\n",
|
| 157 |
+
" <td>173.322839</td>\n",
|
| 158 |
+
" <td>coffee</td>\n",
|
| 159 |
+
" </tr>\n",
|
| 160 |
+
" <tr>\n",
|
| 161 |
+
" <th>2198</th>\n",
|
| 162 |
+
" <td>117</td>\n",
|
| 163 |
+
" <td>32</td>\n",
|
| 164 |
+
" <td>34</td>\n",
|
| 165 |
+
" <td>26.272418</td>\n",
|
| 166 |
+
" <td>52.127394</td>\n",
|
| 167 |
+
" <td>6.758793</td>\n",
|
| 168 |
+
" <td>127.175293</td>\n",
|
| 169 |
+
" <td>coffee</td>\n",
|
| 170 |
+
" </tr>\n",
|
| 171 |
+
" <tr>\n",
|
| 172 |
+
" <th>2199</th>\n",
|
| 173 |
+
" <td>104</td>\n",
|
| 174 |
+
" <td>18</td>\n",
|
| 175 |
+
" <td>30</td>\n",
|
| 176 |
+
" <td>23.603016</td>\n",
|
| 177 |
+
" <td>60.396475</td>\n",
|
| 178 |
+
" <td>6.779833</td>\n",
|
| 179 |
+
" <td>140.937041</td>\n",
|
| 180 |
+
" <td>coffee</td>\n",
|
| 181 |
+
" </tr>\n",
|
| 182 |
+
" </tbody>\n",
|
| 183 |
+
"</table>\n",
|
| 184 |
+
"<p>2200 rows ร 8 columns</p>\n",
|
| 185 |
+
"</div>"
|
| 186 |
+
],
|
| 187 |
+
"text/plain": [
|
| 188 |
+
" N P K temperature humidity ph rainfall label\n",
|
| 189 |
+
"0 90 42 43 20.879744 82.002744 6.502985 202.935536 rice\n",
|
| 190 |
+
"1 85 58 41 21.770462 80.319644 7.038096 226.655537 rice\n",
|
| 191 |
+
"2 60 55 44 23.004459 82.320763 7.840207 263.964248 rice\n",
|
| 192 |
+
"3 74 35 40 26.491096 80.158363 6.980401 242.864034 rice\n",
|
| 193 |
+
"4 78 42 42 20.130175 81.604873 7.628473 262.717340 rice\n",
|
| 194 |
+
"... ... .. .. ... ... ... ... ...\n",
|
| 195 |
+
"2195 107 34 32 26.774637 66.413269 6.780064 177.774507 coffee\n",
|
| 196 |
+
"2196 99 15 27 27.417112 56.636362 6.086922 127.924610 coffee\n",
|
| 197 |
+
"2197 118 33 30 24.131797 67.225123 6.362608 173.322839 coffee\n",
|
| 198 |
+
"2198 117 32 34 26.272418 52.127394 6.758793 127.175293 coffee\n",
|
| 199 |
+
"2199 104 18 30 23.603016 60.396475 6.779833 140.937041 coffee\n",
|
| 200 |
+
"\n",
|
| 201 |
+
"[2200 rows x 8 columns]"
|
| 202 |
+
]
|
| 203 |
+
},
|
| 204 |
+
"execution_count": 27,
|
| 205 |
+
"metadata": {},
|
| 206 |
+
"output_type": "execute_result"
|
| 207 |
+
}
|
| 208 |
+
],
|
| 209 |
+
"source": [
|
| 210 |
+
"df "
|
| 211 |
+
]
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"cell_type": "code",
|
| 215 |
+
"execution_count": 28,
|
| 216 |
+
"metadata": {},
|
| 217 |
+
"outputs": [
|
| 218 |
+
{
|
| 219 |
+
"data": {
|
| 220 |
+
"text/plain": [
|
| 221 |
+
"label\n",
|
| 222 |
+
"rice 100\n",
|
| 223 |
+
"maize 100\n",
|
| 224 |
+
"jute 100\n",
|
| 225 |
+
"cotton 100\n",
|
| 226 |
+
"coconut 100\n",
|
| 227 |
+
"papaya 100\n",
|
| 228 |
+
"orange 100\n",
|
| 229 |
+
"apple 100\n",
|
| 230 |
+
"muskmelon 100\n",
|
| 231 |
+
"watermelon 100\n",
|
| 232 |
+
"grapes 100\n",
|
| 233 |
+
"mango 100\n",
|
| 234 |
+
"banana 100\n",
|
| 235 |
+
"pomegranate 100\n",
|
| 236 |
+
"lentil 100\n",
|
| 237 |
+
"blackgram 100\n",
|
| 238 |
+
"mungbean 100\n",
|
| 239 |
+
"mothbeans 100\n",
|
| 240 |
+
"pigeonpeas 100\n",
|
| 241 |
+
"kidneybeans 100\n",
|
| 242 |
+
"chickpea 100\n",
|
| 243 |
+
"coffee 100\n",
|
| 244 |
+
"Name: count, dtype: int64"
|
| 245 |
+
]
|
| 246 |
+
},
|
| 247 |
+
"execution_count": 28,
|
| 248 |
+
"metadata": {},
|
| 249 |
+
"output_type": "execute_result"
|
| 250 |
+
}
|
| 251 |
+
],
|
| 252 |
+
"source": [
|
| 253 |
+
"df['label'].value_counts()"
|
| 254 |
+
]
|
| 255 |
+
},
|
| 256 |
+
{
|
| 257 |
+
"cell_type": "markdown",
|
| 258 |
+
"metadata": {},
|
| 259 |
+
"source": [
|
| 260 |
+
"## Splitting the Data"
|
| 261 |
+
]
|
| 262 |
+
},
|
| 263 |
+
{
|
| 264 |
+
"cell_type": "code",
|
| 265 |
+
"execution_count": 29,
|
| 266 |
+
"metadata": {},
|
| 267 |
+
"outputs": [],
|
| 268 |
+
"source": [
|
| 269 |
+
"X=df.drop('label',axis=1)\n",
|
| 270 |
+
"y=df['label']"
|
| 271 |
+
]
|
| 272 |
+
},
|
| 273 |
+
{
|
| 274 |
+
"cell_type": "code",
|
| 275 |
+
"execution_count": 30,
|
| 276 |
+
"metadata": {},
|
| 277 |
+
"outputs": [],
|
| 278 |
+
"source": [
|
| 279 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 280 |
+
"\n",
|
| 281 |
+
"# Assuming X is your feature set and y is the corresponding label\n",
|
| 282 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)\n"
|
| 283 |
+
]
|
| 284 |
+
},
|
| 285 |
+
{
|
| 286 |
+
"cell_type": "code",
|
| 287 |
+
"execution_count": 67,
|
| 288 |
+
"metadata": {},
|
| 289 |
+
"outputs": [
|
| 290 |
+
{
|
| 291 |
+
"name": "stderr",
|
| 292 |
+
"output_type": "stream",
|
| 293 |
+
"text": [
|
| 294 |
+
"2025/02/16 01:16:03 INFO mlflow.tracking.fluent: Experiment with name 'RandomForest_GridSearch' does not exist. Creating a new experiment.\n"
|
| 295 |
+
]
|
| 296 |
+
},
|
| 297 |
+
{
|
| 298 |
+
"name": "stdout",
|
| 299 |
+
"output_type": "stream",
|
| 300 |
+
"text": [
|
| 301 |
+
"Fitting 5 folds for each of 216 candidates, totalling 1080 fits\n"
|
| 302 |
+
]
|
| 303 |
+
},
|
| 304 |
+
{
|
| 305 |
+
"name": "stderr",
|
| 306 |
+
"output_type": "stream",
|
| 307 |
+
"text": [
|
| 308 |
+
"2025/02/16 01:18:02 WARNING mlflow.models.model: Model logged without a signature and input example. Please set `input_example` parameter when logging the model to auto infer the model signature.\n"
|
| 309 |
+
]
|
| 310 |
+
},
|
| 311 |
+
{
|
| 312 |
+
"name": "stdout",
|
| 313 |
+
"output_type": "stream",
|
| 314 |
+
"text": [
|
| 315 |
+
"Best Parameters: {'criterion': 'gini', 'max_depth': 10, 'min_samples_leaf': 1, 'min_samples_split': 5, 'n_estimators': 100}\n",
|
| 316 |
+
"Best Accuracy: 0.9960227272727271\n",
|
| 317 |
+
"๐ View run illustrious-cub-960 at: http://127.0.0.1:5000/#/experiments/809510633914373352/runs/7be5a439659d4274a47adfc717813c77\n",
|
| 318 |
+
"๐งช View experiment at: http://127.0.0.1:5000/#/experiments/809510633914373352\n"
|
| 319 |
+
]
|
| 320 |
+
}
|
| 321 |
+
],
|
| 322 |
+
"source": [
|
| 323 |
+
"import mlflow\n",
|
| 324 |
+
"import mlflow.sklearn\n",
|
| 325 |
+
"from sklearn.ensemble import RandomForestClassifier\n",
|
| 326 |
+
"from sklearn.model_selection import GridSearchCV\n",
|
| 327 |
+
"\n",
|
| 328 |
+
"mlflow.set_tracking_uri(\"http://127.0.0.1:5000\")\n",
|
| 329 |
+
"\n",
|
| 330 |
+
"mlflow.set_experiment(\"RandomForest_GridSearch\")\n",
|
| 331 |
+
"\n",
|
| 332 |
+
"with mlflow.start_run():\n",
|
| 333 |
+
" rf = RandomForestClassifier(random_state=42)\n",
|
| 334 |
+
"\n",
|
| 335 |
+
" params = {\n",
|
| 336 |
+
" 'n_estimators': [50, 100, 200],\n",
|
| 337 |
+
" 'max_depth': [None, 10, 20, 30], \n",
|
| 338 |
+
" 'min_samples_split': [2, 5, 10], \n",
|
| 339 |
+
" 'min_samples_leaf': [1, 2, 4], \n",
|
| 340 |
+
" 'criterion': ['gini', 'entropy'] \n",
|
| 341 |
+
" }\n",
|
| 342 |
+
"\n",
|
| 343 |
+
" grid_search_rf = GridSearchCV(estimator=rf, param_grid=params, \n",
|
| 344 |
+
" cv=5, scoring='accuracy', n_jobs=-1, verbose=2)\n",
|
| 345 |
+
"\n",
|
| 346 |
+
" grid_search_rf.fit(X_train, y_train)\n",
|
| 347 |
+
"\n",
|
| 348 |
+
" best_params = grid_search_rf.best_params_\n",
|
| 349 |
+
" best_score = grid_search_rf.best_score_\n",
|
| 350 |
+
"\n",
|
| 351 |
+
" mlflow.log_params(best_params)\n",
|
| 352 |
+
" mlflow.log_metric(\"best_accuracy\", best_score)\n",
|
| 353 |
+
"\n",
|
| 354 |
+
" mlflow.sklearn.log_model(grid_search_rf.best_estimator_, \"best_random_forest_model\")\n",
|
| 355 |
+
"\n",
|
| 356 |
+
" print(\"Best Parameters:\", best_params)\n",
|
| 357 |
+
" print(\"Best Accuracy:\", best_score)\n",
|
| 358 |
+
"\n",
|
| 359 |
+
" mlflow.end_run()\n"
|
| 360 |
+
]
|
| 361 |
+
},
|
| 362 |
+
{
|
| 363 |
+
"cell_type": "code",
|
| 364 |
+
"execution_count": 68,
|
| 365 |
+
"metadata": {},
|
| 366 |
+
"outputs": [],
|
| 367 |
+
"source": [
|
| 368 |
+
"model_rfc=grid_search_rf.best_estimator_"
|
| 369 |
+
]
|
| 370 |
+
},
|
| 371 |
+
{
|
| 372 |
+
"cell_type": "code",
|
| 373 |
+
"execution_count": 69,
|
| 374 |
+
"metadata": {},
|
| 375 |
+
"outputs": [
|
| 376 |
+
{
|
| 377 |
+
"data": {
|
| 378 |
+
"text/plain": [
|
| 379 |
+
"{'criterion': 'gini',\n",
|
| 380 |
+
" 'max_depth': 10,\n",
|
| 381 |
+
" 'min_samples_leaf': 1,\n",
|
| 382 |
+
" 'min_samples_split': 5,\n",
|
| 383 |
+
" 'n_estimators': 100}"
|
| 384 |
+
]
|
| 385 |
+
},
|
| 386 |
+
"execution_count": 69,
|
| 387 |
+
"metadata": {},
|
| 388 |
+
"output_type": "execute_result"
|
| 389 |
+
}
|
| 390 |
+
],
|
| 391 |
+
"source": [
|
| 392 |
+
"grid_search_rf.best_params_"
|
| 393 |
+
]
|
| 394 |
+
},
|
| 395 |
+
{
|
| 396 |
+
"cell_type": "code",
|
| 397 |
+
"execution_count": 70,
|
| 398 |
+
"metadata": {},
|
| 399 |
+
"outputs": [
|
| 400 |
+
{
|
| 401 |
+
"data": {
|
| 402 |
+
"text/plain": [
|
| 403 |
+
"0.9960227272727271"
|
| 404 |
+
]
|
| 405 |
+
},
|
| 406 |
+
"execution_count": 70,
|
| 407 |
+
"metadata": {},
|
| 408 |
+
"output_type": "execute_result"
|
| 409 |
+
}
|
| 410 |
+
],
|
| 411 |
+
"source": [
|
| 412 |
+
"grid_search_rf.best_score_"
|
| 413 |
+
]
|
| 414 |
+
},
|
| 415 |
+
{
|
| 416 |
+
"cell_type": "code",
|
| 417 |
+
"execution_count": 71,
|
| 418 |
+
"metadata": {},
|
| 419 |
+
"outputs": [
|
| 420 |
+
{
|
| 421 |
+
"data": {
|
| 422 |
+
"text/plain": [
|
| 423 |
+
"0.9954545454545455"
|
| 424 |
+
]
|
| 425 |
+
},
|
| 426 |
+
"execution_count": 71,
|
| 427 |
+
"metadata": {},
|
| 428 |
+
"output_type": "execute_result"
|
| 429 |
+
}
|
| 430 |
+
],
|
| 431 |
+
"source": [
|
| 432 |
+
"from sklearn.metrics import accuracy_score,confusion_matrix\n",
|
| 433 |
+
"y_pred=model_rfc.predict(X_test)\n",
|
| 434 |
+
"accuracy_score(y_test,y_pred)"
|
| 435 |
+
]
|
| 436 |
+
},
|
| 437 |
+
{
|
| 438 |
+
"cell_type": "code",
|
| 439 |
+
"execution_count": 72,
|
| 440 |
+
"metadata": {},
|
| 441 |
+
"outputs": [
|
| 442 |
+
{
|
| 443 |
+
"name": "stdout",
|
| 444 |
+
"output_type": "stream",
|
| 445 |
+
"text": [
|
| 446 |
+
"[[20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 447 |
+
" [ 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 448 |
+
" [ 0 0 19 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]\n",
|
| 449 |
+
" [ 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 450 |
+
" [ 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 451 |
+
" [ 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 452 |
+
" [ 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 453 |
+
" [ 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 454 |
+
" [ 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 455 |
+
" [ 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 456 |
+
" [ 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 457 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0]\n",
|
| 458 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0]\n",
|
| 459 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0]\n",
|
| 460 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0]\n",
|
| 461 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
|
| 462 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0]\n",
|
| 463 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0]\n",
|
| 464 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0]\n",
|
| 465 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
|
| 466 |
+
" [ 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 19 0]\n",
|
| 467 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20]]\n"
|
| 468 |
+
]
|
| 469 |
+
}
|
| 470 |
+
],
|
| 471 |
+
"source": [
|
| 472 |
+
"print(confusion_matrix(y_test,y_pred))"
|
| 473 |
+
]
|
| 474 |
+
},
|
| 475 |
+
{
|
| 476 |
+
"cell_type": "markdown",
|
| 477 |
+
"metadata": {},
|
| 478 |
+
"source": [
|
| 479 |
+
"## Trying Out Support Vector Machines Algorithm"
|
| 480 |
+
]
|
| 481 |
+
},
|
| 482 |
+
{
|
| 483 |
+
"cell_type": "code",
|
| 484 |
+
"execution_count": 73,
|
| 485 |
+
"metadata": {},
|
| 486 |
+
"outputs": [
|
| 487 |
+
{
|
| 488 |
+
"name": "stderr",
|
| 489 |
+
"output_type": "stream",
|
| 490 |
+
"text": [
|
| 491 |
+
"2025/02/16 01:21:04 INFO mlflow.tracking.fluent: Experiment with name 'SVC_GridSearch' does not exist. Creating a new experiment.\n"
|
| 492 |
+
]
|
| 493 |
+
},
|
| 494 |
+
{
|
| 495 |
+
"name": "stdout",
|
| 496 |
+
"output_type": "stream",
|
| 497 |
+
"text": [
|
| 498 |
+
"Fitting 5 folds for each of 16 candidates, totalling 80 fits\n"
|
| 499 |
+
]
|
| 500 |
+
},
|
| 501 |
+
{
|
| 502 |
+
"name": "stderr",
|
| 503 |
+
"output_type": "stream",
|
| 504 |
+
"text": [
|
| 505 |
+
"2025/02/16 01:21:10 WARNING mlflow.models.model: Model logged without a signature and input example. Please set `input_example` parameter when logging the model to auto infer the model signature.\n"
|
| 506 |
+
]
|
| 507 |
+
},
|
| 508 |
+
{
|
| 509 |
+
"name": "stdout",
|
| 510 |
+
"output_type": "stream",
|
| 511 |
+
"text": [
|
| 512 |
+
"Best Parameters: {'C': 0.1, 'kernel': 'linear'}\n",
|
| 513 |
+
"Best Accuracy: 0.9857954545454545\n",
|
| 514 |
+
"๐ View run agreeable-goose-248 at: http://127.0.0.1:5000/#/experiments/308727505154579858/runs/b513565508424ee1883922873cb0fa35\n",
|
| 515 |
+
"๐งช View experiment at: http://127.0.0.1:5000/#/experiments/308727505154579858\n"
|
| 516 |
+
]
|
| 517 |
+
}
|
| 518 |
+
],
|
| 519 |
+
"source": [
|
| 520 |
+
"import mlflow\n",
|
| 521 |
+
"import mlflow.sklearn\n",
|
| 522 |
+
"from sklearn.svm import SVC\n",
|
| 523 |
+
"from sklearn.model_selection import GridSearchCV\n",
|
| 524 |
+
"\n",
|
| 525 |
+
"mlflow.set_tracking_uri(\"http://127.0.0.1:5000\")\n",
|
| 526 |
+
"\n",
|
| 527 |
+
"mlflow.set_experiment(\"SVC_GridSearch\")\n",
|
| 528 |
+
"\n",
|
| 529 |
+
"with mlflow.start_run():\n",
|
| 530 |
+
" svc = SVC()\n",
|
| 531 |
+
"\n",
|
| 532 |
+
" params = {\n",
|
| 533 |
+
" 'C': [0.1, 1, 10, 100], \n",
|
| 534 |
+
" 'kernel': ['linear', 'rbf', 'poly', 'sigmoid']\n",
|
| 535 |
+
" }\n",
|
| 536 |
+
"\n",
|
| 537 |
+
" grid_search_svc = GridSearchCV(estimator=svc, param_grid=params, \n",
|
| 538 |
+
" cv=5, scoring='accuracy', n_jobs=-1, verbose=2)\n",
|
| 539 |
+
"\n",
|
| 540 |
+
" grid_search_svc.fit(X_train, y_train)\n",
|
| 541 |
+
"\n",
|
| 542 |
+
" best_params = grid_search_svc.best_params_\n",
|
| 543 |
+
" best_score = grid_search_svc.best_score_\n",
|
| 544 |
+
"\n",
|
| 545 |
+
" mlflow.log_params(best_params)\n",
|
| 546 |
+
" mlflow.log_metric(\"best_accuracy\", best_score)\n",
|
| 547 |
+
"\n",
|
| 548 |
+
" mlflow.sklearn.log_model(grid_search_svc.best_estimator_, \"best_svc_model\")\n",
|
| 549 |
+
"\n",
|
| 550 |
+
" print(\"Best Parameters:\", best_params)\n",
|
| 551 |
+
" print(\"Best Accuracy:\", best_score)\n",
|
| 552 |
+
"\n",
|
| 553 |
+
" mlflow.end_run()\n"
|
| 554 |
+
]
|
| 555 |
+
},
|
| 556 |
+
{
|
| 557 |
+
"cell_type": "code",
|
| 558 |
+
"execution_count": 74,
|
| 559 |
+
"metadata": {},
|
| 560 |
+
"outputs": [],
|
| 561 |
+
"source": [
|
| 562 |
+
"model_svc=grid_search_svc.best_estimator_"
|
| 563 |
+
]
|
| 564 |
+
},
|
| 565 |
+
{
|
| 566 |
+
"cell_type": "code",
|
| 567 |
+
"execution_count": 75,
|
| 568 |
+
"metadata": {},
|
| 569 |
+
"outputs": [],
|
| 570 |
+
"source": [
|
| 571 |
+
"y_pred1=model_svc.predict(X_test)"
|
| 572 |
+
]
|
| 573 |
+
},
|
| 574 |
+
{
|
| 575 |
+
"cell_type": "code",
|
| 576 |
+
"execution_count": 76,
|
| 577 |
+
"metadata": {},
|
| 578 |
+
"outputs": [
|
| 579 |
+
{
|
| 580 |
+
"data": {
|
| 581 |
+
"text/plain": [
|
| 582 |
+
"0.9931818181818182"
|
| 583 |
+
]
|
| 584 |
+
},
|
| 585 |
+
"execution_count": 76,
|
| 586 |
+
"metadata": {},
|
| 587 |
+
"output_type": "execute_result"
|
| 588 |
+
}
|
| 589 |
+
],
|
| 590 |
+
"source": [
|
| 591 |
+
"accuracy_score(y_test,y_pred1)"
|
| 592 |
+
]
|
| 593 |
+
},
|
| 594 |
+
{
|
| 595 |
+
"cell_type": "code",
|
| 596 |
+
"execution_count": 77,
|
| 597 |
+
"metadata": {},
|
| 598 |
+
"outputs": [
|
| 599 |
+
{
|
| 600 |
+
"name": "stdout",
|
| 601 |
+
"output_type": "stream",
|
| 602 |
+
"text": [
|
| 603 |
+
"[[20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 604 |
+
" [ 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 605 |
+
" [ 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 606 |
+
" [ 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 607 |
+
" [ 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 608 |
+
" [ 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 609 |
+
" [ 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 610 |
+
" [ 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 611 |
+
" [ 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 612 |
+
" [ 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 613 |
+
" [ 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 614 |
+
" [ 0 0 0 0 0 0 1 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
|
| 615 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0]\n",
|
| 616 |
+
" [ 0 0 1 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0]\n",
|
| 617 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0]\n",
|
| 618 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
|
| 619 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0]\n",
|
| 620 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0]\n",
|
| 621 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0]\n",
|
| 622 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
|
| 623 |
+
" [ 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 19 0]\n",
|
| 624 |
+
" [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20]]\n"
|
| 625 |
+
]
|
| 626 |
+
}
|
| 627 |
+
],
|
| 628 |
+
"source": [
|
| 629 |
+
"print(confusion_matrix(y_test,y_pred1))"
|
| 630 |
+
]
|
| 631 |
+
},
|
| 632 |
+
{
|
| 633 |
+
"cell_type": "markdown",
|
| 634 |
+
"metadata": {},
|
| 635 |
+
"source": [
|
| 636 |
+
"## Gradient Boosting"
|
| 637 |
+
]
|
| 638 |
+
},
|
| 639 |
+
{
|
| 640 |
+
"cell_type": "code",
|
| 641 |
+
"execution_count": 78,
|
| 642 |
+
"metadata": {},
|
| 643 |
+
"outputs": [
|
| 644 |
+
{
|
| 645 |
+
"name": "stderr",
|
| 646 |
+
"output_type": "stream",
|
| 647 |
+
"text": [
|
| 648 |
+
"2025/02/16 01:24:17 INFO mlflow.tracking.fluent: Experiment with name 'GradientBoosting_GridSearch' does not exist. Creating a new experiment.\n"
|
| 649 |
+
]
|
| 650 |
+
},
|
| 651 |
+
{
|
| 652 |
+
"name": "stdout",
|
| 653 |
+
"output_type": "stream",
|
| 654 |
+
"text": [
|
| 655 |
+
"Fitting 5 folds for each of 9 candidates, totalling 45 fits\n"
|
| 656 |
+
]
|
| 657 |
+
},
|
| 658 |
+
{
|
| 659 |
+
"name": "stderr",
|
| 660 |
+
"output_type": "stream",
|
| 661 |
+
"text": [
|
| 662 |
+
"2025/02/16 01:25:36 WARNING mlflow.models.model: Model logged without a signature and input example. Please set `input_example` parameter when logging the model to auto infer the model signature.\n"
|
| 663 |
+
]
|
| 664 |
+
},
|
| 665 |
+
{
|
| 666 |
+
"name": "stdout",
|
| 667 |
+
"output_type": "stream",
|
| 668 |
+
"text": [
|
| 669 |
+
"Best Parameters: {'learning_rate': 0.1, 'n_estimators': 100}\n",
|
| 670 |
+
"Best Accuracy: 0.9875\n",
|
| 671 |
+
"๐ View run rare-auk-421 at: http://127.0.0.1:5000/#/experiments/148959594547896690/runs/417ba78c49f845bd9ff43a5f9a381fb2\n",
|
| 672 |
+
"๐งช View experiment at: http://127.0.0.1:5000/#/experiments/148959594547896690\n"
|
| 673 |
+
]
|
| 674 |
+
}
|
| 675 |
+
],
|
| 676 |
+
"source": [
|
| 677 |
+
"import mlflow\n",
|
| 678 |
+
"import mlflow.sklearn\n",
|
| 679 |
+
"from sklearn.ensemble import GradientBoostingClassifier\n",
|
| 680 |
+
"from sklearn.model_selection import GridSearchCV\n",
|
| 681 |
+
"\n",
|
| 682 |
+
"mlflow.set_tracking_uri(\"http://127.0.0.1:5000\")\n",
|
| 683 |
+
"\n",
|
| 684 |
+
"mlflow.set_experiment(\"GradientBoosting_GridSearch\")\n",
|
| 685 |
+
"\n",
|
| 686 |
+
"with mlflow.start_run():\n",
|
| 687 |
+
" gb = GradientBoostingClassifier(random_state=42)\n",
|
| 688 |
+
"\n",
|
| 689 |
+
" params = {\n",
|
| 690 |
+
" 'n_estimators': [50, 100, 150], \n",
|
| 691 |
+
" 'learning_rate': [0.01, 0.05, 0.1], \n",
|
| 692 |
+
" }\n",
|
| 693 |
+
"\n",
|
| 694 |
+
" grid_search_gb = GridSearchCV(estimator=gb, param_grid=params, \n",
|
| 695 |
+
" cv=5, scoring='accuracy', n_jobs=-1, verbose=2)\n",
|
| 696 |
+
"\n",
|
| 697 |
+
" grid_search_gb.fit(X_train, y_train)\n",
|
| 698 |
+
"\n",
|
| 699 |
+
" best_params = grid_search_gb.best_params_\n",
|
| 700 |
+
" best_score = grid_search_gb.best_score_\n",
|
| 701 |
+
"\n",
|
| 702 |
+
" mlflow.log_params(best_params)\n",
|
| 703 |
+
" mlflow.log_metric(\"best_accuracy\", best_score)\n",
|
| 704 |
+
"\n",
|
| 705 |
+
" mlflow.sklearn.log_model(grid_search_gb.best_estimator_, \"best_gradient_boosting_model\")\n",
|
| 706 |
+
"\n",
|
| 707 |
+
" print(\"Best Parameters:\", best_params)\n",
|
| 708 |
+
" print(\"Best Accuracy:\", best_score)\n",
|
| 709 |
+
"\n",
|
| 710 |
+
" mlflow.end_run()\n"
|
| 711 |
+
]
|
| 712 |
+
},
|
| 713 |
+
{
|
| 714 |
+
"cell_type": "code",
|
| 715 |
+
"execution_count": 79,
|
| 716 |
+
"metadata": {},
|
| 717 |
+
"outputs": [],
|
| 718 |
+
"source": [
|
| 719 |
+
"model_gb=grid_search_gb.best_estimator_"
|
| 720 |
+
]
|
| 721 |
+
},
|
| 722 |
+
{
|
| 723 |
+
"cell_type": "code",
|
| 724 |
+
"execution_count": 80,
|
| 725 |
+
"metadata": {},
|
| 726 |
+
"outputs": [],
|
| 727 |
+
"source": [
|
| 728 |
+
"y_pred2=model_gb.predict(X_test)"
|
| 729 |
+
]
|
| 730 |
+
},
|
| 731 |
+
{
|
| 732 |
+
"cell_type": "code",
|
| 733 |
+
"execution_count": 81,
|
| 734 |
+
"metadata": {},
|
| 735 |
+
"outputs": [
|
| 736 |
+
{
|
| 737 |
+
"data": {
|
| 738 |
+
"text/plain": [
|
| 739 |
+
"0.9886363636363636"
|
| 740 |
+
]
|
| 741 |
+
},
|
| 742 |
+
"execution_count": 81,
|
| 743 |
+
"metadata": {},
|
| 744 |
+
"output_type": "execute_result"
|
| 745 |
+
}
|
| 746 |
+
],
|
| 747 |
+
"source": [
|
| 748 |
+
"accuracy_score(y_test,y_pred2)"
|
| 749 |
+
]
|
| 750 |
+
},
|
| 751 |
+
{
|
| 752 |
+
"cell_type": "code",
|
| 753 |
+
"execution_count": 82,
|
| 754 |
+
"metadata": {},
|
| 755 |
+
"outputs": [
|
| 756 |
+
{
|
| 757 |
+
"name": "stdout",
|
| 758 |
+
"output_type": "stream",
|
| 759 |
+
"text": [
|
| 760 |
+
"[[20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 761 |
+
" [ 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 762 |
+
" [ 0 0 19 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0]\n",
|
| 763 |
+
" [ 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 764 |
+
" [ 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
|
| 765 |
+
" [ 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n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 |
+
],
|
| 785 |
+
"source": [
|
| 786 |
+
"print(confusion_matrix(y_test,y_pred2))"
|
| 787 |
+
]
|
| 788 |
+
},
|
| 789 |
+
{
|
| 790 |
+
"cell_type": "code",
|
| 791 |
+
"execution_count": null,
|
| 792 |
+
"metadata": {},
|
| 793 |
+
"outputs": [],
|
| 794 |
+
"source": []
|
| 795 |
+
}
|
| 796 |
+
],
|
| 797 |
+
"metadata": {
|
| 798 |
+
"kernelspec": {
|
| 799 |
+
"display_name": "Python 3",
|
| 800 |
+
"language": "python",
|
| 801 |
+
"name": "python3"
|
| 802 |
+
},
|
| 803 |
+
"language_info": {
|
| 804 |
+
"codemirror_mode": {
|
| 805 |
+
"name": "ipython",
|
| 806 |
+
"version": 3
|
| 807 |
+
},
|
| 808 |
+
"file_extension": ".py",
|
| 809 |
+
"mimetype": "text/x-python",
|
| 810 |
+
"name": "python",
|
| 811 |
+
"nbconvert_exporter": "python",
|
| 812 |
+
"pygments_lexer": "ipython3",
|
| 813 |
+
"version": "3.10.0"
|
| 814 |
+
}
|
| 815 |
+
},
|
| 816 |
+
"nbformat": 4,
|
| 817 |
+
"nbformat_minor": 2
|
| 818 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
seaborn
|
| 4 |
+
matplotlib
|
| 5 |
+
scikit-learn
|
| 6 |
+
langchain
|
| 7 |
+
langchain_groq
|
| 8 |
+
python-dotenv
|
| 9 |
+
xgboost
|
| 10 |
+
mlflow
|
| 11 |
+
pypdf
|
| 12 |
+
langchain_community
|
| 13 |
+
sentence-transformers
|
| 14 |
+
faiss-cpu
|
| 15 |
+
langchain_hugginface
|
| 16 |
+
rank_bm25
|
testing_prompts.ipynb
ADDED
|
@@ -0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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"data": {
|
| 10 |
+
"text/plain": [
|
| 11 |
+
"True"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
"execution_count": 1,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"output_type": "execute_result"
|
| 17 |
+
}
|
| 18 |
+
],
|
| 19 |
+
"source": [
|
| 20 |
+
"from dotenv import load_dotenv\n",
|
| 21 |
+
"load_dotenv()"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 2,
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"outputs": [],
|
| 29 |
+
"source": [
|
| 30 |
+
"from langchain_groq import ChatGroq\n",
|
| 31 |
+
"import os\n",
|
| 32 |
+
"GROQ_API_KEY=os.getenv(\"GROQ_API_KEY\")"
|
| 33 |
+
]
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"cell_type": "code",
|
| 37 |
+
"execution_count": 3,
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [],
|
| 40 |
+
"source": [
|
| 41 |
+
"llm=ChatGroq(model_name=\"gemma2-9b-it\",api_key=GROQ_API_KEY)"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "code",
|
| 46 |
+
"execution_count": 4,
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"outputs": [],
|
| 49 |
+
"source": [
|
| 50 |
+
"response=llm.invoke(\"What is Crop Optimization\")"
|
| 51 |
+
]
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"cell_type": "code",
|
| 55 |
+
"execution_count": 5,
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [
|
| 58 |
+
{
|
| 59 |
+
"name": "stdout",
|
| 60 |
+
"output_type": "stream",
|
| 61 |
+
"text": [
|
| 62 |
+
"Crop optimization is a multi-faceted approach aiming to maximize the yield and quality of agricultural produce while minimizing resource use and environmental impact. \n",
|
| 63 |
+
"\n",
|
| 64 |
+
"Here's a breakdown:\n",
|
| 65 |
+
"\n",
|
| 66 |
+
"**Goals of Crop Optimization:**\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"* **Increased Yield:** Producing more crops per unit of land, leading to higher economic returns for farmers.\n",
|
| 69 |
+
"* **Improved Quality:** Enhancing the size, shape, color, taste, and nutritional value of crops.\n",
|
| 70 |
+
"* **Resource Efficiency:** Optimizing the use of water, fertilizers, pesticides, and other inputs, reducing costs and environmental pollution.\n",
|
| 71 |
+
"* **Sustainability:** Promoting environmentally friendly practices that conserve natural resources and protect biodiversity.\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"**Methods Used in Crop Optimization:**\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"Crop optimization leverages a variety of techniques, including:\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"* **Precision Agriculture:** Using technologies like GPS, sensors, and drones to collect data on soil conditions, crop health, and weather patterns. This data is then analyzed to make site-specific decisions about irrigation, fertilization, and pest control.\n",
|
| 78 |
+
"* **Data Analytics:** Analyzing historical yield data, weather patterns, and market trends to identify optimal planting times, crop varieties, and management practices.\n",
|
| 79 |
+
"* **Crop Modeling:** Using mathematical models to simulate crop growth and development under different conditions. These models can help predict yield potential and identify potential risks.\n",
|
| 80 |
+
"* **Genetic Engineering:** Developing crops with improved traits such as resistance to pests, diseases, and drought.\n",
|
| 81 |
+
"* **Integrated Pest Management (IPM):** Implementing a holistic approach to pest control that emphasizes prevention, monitoring, and targeted interventions.\n",
|
| 82 |
+
"* **Conservation Agriculture:** Promoting practices like no-till farming, crop rotation, and cover cropping to improve soil health and reduce erosion.\n",
|
| 83 |
+
"\n",
|
| 84 |
+
"**Benefits of Crop Optimization:**\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"* **Increased profitability for farmers:** Higher yields and reduced input costs can lead to significant economic gains.\n",
|
| 87 |
+
"* **Enhanced food security:** Optimizing crop production can help meet the growing demand for food in a sustainable way.\n",
|
| 88 |
+
"* **Environmental protection:** Reducing resource use and minimizing pollution can contribute to a healthier planet.\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"**Challenges of Crop Optimization:**\n",
|
| 91 |
+
"\n",
|
| 92 |
+
"* **Cost of technology:** Implementing precision agriculture and other advanced technologies can be expensive.\n",
|
| 93 |
+
"* **Data management:** Collecting, storing, and analyzing large amounts of data can be challenging.\n",
|
| 94 |
+
"* **Knowledge gap:** Farmers may need training and support to effectively use new technologies and practices.\n",
|
| 95 |
+
"* **Regulatory hurdles:** Genetic engineering and other innovations may face regulatory challenges.\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"Overall, crop optimization is a promising approach to improving agricultural productivity and sustainability. By combining innovative technologies with sound farming practices, we can create a more efficient, resilient, and environmentally friendly food system.\n",
|
| 101 |
+
"\n"
|
| 102 |
+
]
|
| 103 |
+
}
|
| 104 |
+
],
|
| 105 |
+
"source": [
|
| 106 |
+
"print(response.content)"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"cell_type": "markdown",
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"source": [
|
| 113 |
+
"Testing Basic Prompts"
|
| 114 |
+
]
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"cell_type": "code",
|
| 118 |
+
"execution_count": 6,
|
| 119 |
+
"metadata": {},
|
| 120 |
+
"outputs": [],
|
| 121 |
+
"source": [
|
| 122 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 123 |
+
"\n",
|
| 124 |
+
"template = \"\"\"You are an AI farming assistant designed to provide accurate, practical, and easy-to-understand agricultural advice. Your goal is to assist farmers in crop management, pest control, soil health, irrigation techniques, weather forecasting, livestock care, and sustainable farming practices. Always provide region-specific and season-specific recommendations. If a farmer asks about something outside agriculture, politely redirect them back to farming topics.\n",
|
| 125 |
+
"Use simple language and practical solutions tailored for small and large-scale farmers. \n",
|
| 126 |
+
"Maintain a helpful, supportive, and problem-solving tone.\n",
|
| 127 |
+
" \n",
|
| 128 |
+
"Question: {question}\"\"\"\n",
|
| 129 |
+
"\n",
|
| 130 |
+
"prompt = PromptTemplate(\n",
|
| 131 |
+
" input_variables=['question'], \n",
|
| 132 |
+
" template=template \n",
|
| 133 |
+
")\n"
|
| 134 |
+
]
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"cell_type": "code",
|
| 138 |
+
"execution_count": 7,
|
| 139 |
+
"metadata": {},
|
| 140 |
+
"outputs": [
|
| 141 |
+
{
|
| 142 |
+
"name": "stderr",
|
| 143 |
+
"output_type": "stream",
|
| 144 |
+
"text": [
|
| 145 |
+
"C:\\Users\\saipr\\AppData\\Local\\Temp\\ipykernel_23300\\3966671106.py:2: LangChainDeprecationWarning: The class `LLMChain` was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use :meth:`~RunnableSequence, e.g., `prompt | llm`` instead.\n",
|
| 146 |
+
" llm_chain=LLMChain(llm=llm,prompt=prompt)\n"
|
| 147 |
+
]
|
| 148 |
+
}
|
| 149 |
+
],
|
| 150 |
+
"source": [
|
| 151 |
+
"from langchain.chains import LLMChain\n",
|
| 152 |
+
"llm_chain=LLMChain(llm=llm,prompt=prompt)"
|
| 153 |
+
]
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"cell_type": "code",
|
| 157 |
+
"execution_count": 8,
|
| 158 |
+
"metadata": {},
|
| 159 |
+
"outputs": [],
|
| 160 |
+
"source": [
|
| 161 |
+
"response=llm.invoke(\"What is Crop Optimization\")"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "code",
|
| 166 |
+
"execution_count": 9,
|
| 167 |
+
"metadata": {},
|
| 168 |
+
"outputs": [
|
| 169 |
+
{
|
| 170 |
+
"name": "stdout",
|
| 171 |
+
"output_type": "stream",
|
| 172 |
+
"text": [
|
| 173 |
+
"Crop optimization refers to a set of practices and technologies aimed at **maximizing the yield and quality of crops while minimizing the environmental impact and resource consumption**. \n",
|
| 174 |
+
"\n",
|
| 175 |
+
"It's a multi-faceted approach that combines various techniques, including:\n",
|
| 176 |
+
"\n",
|
| 177 |
+
"**1. Precision Agriculture:**\n",
|
| 178 |
+
"\n",
|
| 179 |
+
"* **Data-driven decision making:** Utilizing sensor data, satellite imagery, and weather forecasts to understand specific field conditions and tailor management practices accordingly.\n",
|
| 180 |
+
"* **Variable rate technology:** Applying inputs like fertilizers, pesticides, and water at varying rates across a field based on precise needs identified through data analysis.\n",
|
| 181 |
+
"\n",
|
| 182 |
+
"**2. Crop Management Practices:**\n",
|
| 183 |
+
"\n",
|
| 184 |
+
"* **Optimal planting density:** Determining the ideal number of plants per unit area to maximize sunlight capture and resource utilization.\n",
|
| 185 |
+
"* **Integrated Pest Management (IPM):** Employing a combination of biological, cultural, and chemical methods to control pests and diseases, minimizing reliance on pesticides.\n",
|
| 186 |
+
"* **Nutrient Management:** Utilizing soil tests and crop requirements to optimize fertilizer application, reducing nutrient runoff and promoting soil health.\n",
|
| 187 |
+
"\n",
|
| 188 |
+
"**3. Technological Advancements:**\n",
|
| 189 |
+
"\n",
|
| 190 |
+
"* **Drones and Robotics:** Utilizing drones for aerial imagery, crop monitoring, and targeted spraying, while robots can automate tasks like planting, weeding, and harvesting.\n",
|
| 191 |
+
"* **Artificial Intelligence (AI):** Applying machine learning algorithms to analyze vast amounts of data and predict crop yields, identify disease outbreaks, and optimize irrigation schedules.\n",
|
| 192 |
+
"\n",
|
| 193 |
+
"**4. Sustainable Practices:**\n",
|
| 194 |
+
"\n",
|
| 195 |
+
"* **Conservation tillage:** Minimizing soil disturbance to preserve soil structure, reduce erosion, and enhance water infiltration.\n",
|
| 196 |
+
"* **Crop rotation:** Alternating different crops in a field to break pest cycles, improve soil fertility, and reduce reliance on chemical inputs.\n",
|
| 197 |
+
"\n",
|
| 198 |
+
"**Benefits of Crop Optimization:**\n",
|
| 199 |
+
"\n",
|
| 200 |
+
"* **Increased yield and profitability:** By maximizing resource utilization and minimizing losses, farmers can achieve higher crop yields and increase their income.\n",
|
| 201 |
+
"* **Reduced environmental impact:** Sustainable practices like precision irrigation and reduced pesticide use minimize water and chemical pollution.\n",
|
| 202 |
+
"* **Enhanced resource efficiency:** Optimizing nutrient and water application reduces waste and promotes responsible resource management.\n",
|
| 203 |
+
"* **Improved food security:** By increasing crop production, crop optimization contributes to global food security and supports a growing population.\n",
|
| 204 |
+
"\n",
|
| 205 |
+
"\n",
|
| 206 |
+
"Overall, crop optimization is a crucial strategy for ensuring sustainable and efficient agricultural practices in the face of growing global food demands and environmental challenges.\n",
|
| 207 |
+
"\n"
|
| 208 |
+
]
|
| 209 |
+
}
|
| 210 |
+
],
|
| 211 |
+
"source": [
|
| 212 |
+
"print(response.content)"
|
| 213 |
+
]
|
| 214 |
+
},
|
| 215 |
+
{
|
| 216 |
+
"cell_type": "markdown",
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"source": [
|
| 219 |
+
"With a well-designed prompt, the LLM:\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"โ Produces structured, detailed, and farmer-focused answers.\n",
|
| 222 |
+
"\n",
|
| 223 |
+
"โ Provides practical advice instead of just a theoretical explanation.\n",
|
| 224 |
+
"\n",
|
| 225 |
+
"โ Makes technology more accessible and actionable.\n",
|
| 226 |
+
"\n",
|
| 227 |
+
"โ Uses clear, engaging, and farmer-friendly language.\n",
|
| 228 |
+
"\n"
|
| 229 |
+
]
|
| 230 |
+
},
|
| 231 |
+
{
|
| 232 |
+
"cell_type": "code",
|
| 233 |
+
"execution_count": null,
|
| 234 |
+
"metadata": {},
|
| 235 |
+
"outputs": [
|
| 236 |
+
{
|
| 237 |
+
"name": "stdout",
|
| 238 |
+
"output_type": "stream",
|
| 239 |
+
"text": [
|
| 240 |
+
"๐ AI Response: content=\"That's a great question! Choosing the best irrigation method for rice farming depends on a few things, like the size of your farm, your soil type, and the amount of water available to you. \\n\\nHere are some popular options:\\n\\n**1. Flood Irrigation:** This is the traditional method, where the entire field is flooded with water. \\n\\n* **Pros:** Relatively simple and inexpensive to set up. \\n* **Cons:** Can waste a lot of water, increase the risk of weed growth, and may not be suitable for all soil types.\\n\\n**2. Alternate Wetting and Drying (AWD):** This method involves flooding the field for a period of time, then allowing it to dry out partially before reflooding.\\n\\n* **Pros:** Saves water compared to flood irrigation, reduces weed growth, and can improve soil health.\\n* **Cons:** Requires more careful monitoring and management.\\n\\n**3. System of Rice Intensification (SRI):** This method uses a combination of techniques, including reduced water use, wider spacing between plants, and the use of seedlings that are already a few weeks old.\\n\\n* **Pros:** Significantly reduces water use, increases yields, and can improve soil fertility.\\n* **Cons:** Requires more labor and time, and may not be suitable for all rice varieties.\\n\\n**4. Sprinkler Irrigation:** This method uses sprinklers to deliver water directly to the rice plants.\\n\\n* **Pros:** More efficient than flood irrigation, can be used on sloped land, and can be automated.\\n* **Cons:** More expensive to set up than flood irrigation, and can be affected by wind.\\n\\n**5. Drip Irrigation:** This method delivers water directly to the roots of the rice plants through a network of tubes.\\n\\n* **Pros:** Most efficient irrigation method, saves water, and can improve yields.\\n* **Cons:** Most expensive to set up, and requires careful maintenance.\\n\\n**To get the best advice for your specific situation, I recommend talking to your local agricultural extension office. They can help you assess your needs and recommend the most suitable irrigation method for your farm.**\\n\\nGood luck with your rice farming! \\n\" additional_kwargs={} response_metadata={'token_usage': {'completion_tokens': 451, 'prompt_tokens': 119, 'total_tokens': 570, 'completion_time': 0.82, 'prompt_time': 0.003625076, 'queue_time': 0.056550666, 'total_time': 0.823625076}, 'model_name': 'gemma2-9b-it', 'system_fingerprint': 'fp_10c08bf97d', 'finish_reason': 'stop', 'logprobs': None} id='run-ce8e13b3-fb33-4602-afd0-c44d5a81501c-0' usage_metadata={'input_tokens': 119, 'output_tokens': 451, 'total_tokens': 570}\n"
|
| 241 |
+
]
|
| 242 |
+
}
|
| 243 |
+
],
|
| 244 |
+
"source": [
|
| 245 |
+
"import os\n",
|
| 246 |
+
"from dotenv import load_dotenv\n",
|
| 247 |
+
"from langchain_groq import ChatGroq\n",
|
| 248 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 249 |
+
"\n",
|
| 250 |
+
"load_dotenv()\n",
|
| 251 |
+
"GROQ_API_KEY = os.getenv(\"GROQ_API_KEY\")\n",
|
| 252 |
+
"\n",
|
| 253 |
+
"llm = ChatGroq(model_name=\"gemma2-9b-it\", api_key=GROQ_API_KEY)\n",
|
| 254 |
+
"\n",
|
| 255 |
+
"template = \"\"\"You are an AI farming assistant designed to provide accurate, practical, and easy-to-understand agricultural advice. \n",
|
| 256 |
+
"Your goal is to assist farmers in crop management, pest control, soil health, irrigation techniques, weather forecasting, \n",
|
| 257 |
+
"livestock care, and sustainable farming practices. Always provide region-specific and season-specific recommendations. \n",
|
| 258 |
+
"\n",
|
| 259 |
+
"Use simple language and practical solutions tailored for small and large-scale farmers. Maintain a helpful, supportive, and problem-solving tone.\n",
|
| 260 |
+
"\n",
|
| 261 |
+
"Question: {question}\"\"\"\n",
|
| 262 |
+
"\n",
|
| 263 |
+
"prompt = PromptTemplate(input_variables=['question'], template=template)\n",
|
| 264 |
+
"\n",
|
| 265 |
+
"def agriculture_rag(query):\n",
|
| 266 |
+
" formatted_prompt = prompt.format(question=query)\n",
|
| 267 |
+
"\n",
|
| 268 |
+
" response = llm.invoke(formatted_prompt)\n",
|
| 269 |
+
"\n",
|
| 270 |
+
" return response\n",
|
| 271 |
+
"\n",
|
| 272 |
+
"query = \"What is the best irrigation method for rice farming?\"\n",
|
| 273 |
+
"response = agriculture_rag(query)\n",
|
| 274 |
+
"print(\"๐ AI Response:\", response)\n"
|
| 275 |
+
]
|
| 276 |
+
},
|
| 277 |
+
{
|
| 278 |
+
"cell_type": "code",
|
| 279 |
+
"execution_count": 18,
|
| 280 |
+
"metadata": {},
|
| 281 |
+
"outputs": [],
|
| 282 |
+
"source": [
|
| 283 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 284 |
+
"\n",
|
| 285 |
+
"agriculture_prompt = PromptTemplate(\n",
|
| 286 |
+
" input_variables=[\"question\", \"location\", \"crop\", \"season\"],\n",
|
| 287 |
+
" template=(\n",
|
| 288 |
+
" \"You are an expert agriculture assistant helping farmers with their queries. \"\n",
|
| 289 |
+
" \"Provide a detailed yet simple answer for the given question.\\n\\n\"\n",
|
| 290 |
+
" \"Farmer's Question: {question}\\n\"\n",
|
| 291 |
+
" \"Location: {location}\\n\"\n",
|
| 292 |
+
" \"Crop Type: {crop}\\n\"\n",
|
| 293 |
+
" \"Current Season: {season}\\n\\n\"\n",
|
| 294 |
+
" \"Answer the question in a way that a farmer with basic knowledge can understand. \"\n",
|
| 295 |
+
" \"Use practical advice, avoiding overly technical terms unless necessary.\"\n",
|
| 296 |
+
" )\n",
|
| 297 |
+
")\n",
|
| 298 |
+
"\n",
|
| 299 |
+
"\n"
|
| 300 |
+
]
|
| 301 |
+
},
|
| 302 |
+
{
|
| 303 |
+
"cell_type": "code",
|
| 304 |
+
"execution_count": 19,
|
| 305 |
+
"metadata": {},
|
| 306 |
+
"outputs": [],
|
| 307 |
+
"source": [
|
| 308 |
+
"chain=LLMChain(llm=llm,prompt=agriculture_prompt)"
|
| 309 |
+
]
|
| 310 |
+
},
|
| 311 |
+
{
|
| 312 |
+
"cell_type": "code",
|
| 313 |
+
"execution_count": null,
|
| 314 |
+
"metadata": {},
|
| 315 |
+
"outputs": [
|
| 316 |
+
{
|
| 317 |
+
"name": "stdout",
|
| 318 |
+
"output_type": "stream",
|
| 319 |
+
"text": [
|
| 320 |
+
"Mangu, also known as citrus canker, is a serious disease that can harm your orange trees in Nalgonda's summer heat. Here's what you can do to prevent it:\n",
|
| 321 |
+
"\n",
|
| 322 |
+
"**1. Choose Resistant Varieties:**\n",
|
| 323 |
+
"\n",
|
| 324 |
+
"* Talk to your local agricultural officer or nursery owners about orange varieties that are less susceptible to mangu in your area. \n",
|
| 325 |
+
"\n",
|
| 326 |
+
"**2. Healthy Planting Material:**\n",
|
| 327 |
+
"\n",
|
| 328 |
+
"* **Never** use diseased saplings or cuttings. Buy your plants from reliable nurseries that follow good practices and offer disease-free stock.\n",
|
| 329 |
+
"\n",
|
| 330 |
+
"**3. Keep Trees Clean:**\n",
|
| 331 |
+
"\n",
|
| 332 |
+
"* Regularly remove fallen leaves and fruit from around the base of your trees. This helps prevent the disease from spreading.\n",
|
| 333 |
+
"\n",
|
| 334 |
+
"**4. Avoid Overcrowding:**\n",
|
| 335 |
+
"\n",
|
| 336 |
+
"* Give your orange trees enough space to grow. Crowded trees have poor air circulation, which makes them more vulnerable to diseases.\n",
|
| 337 |
+
"\n",
|
| 338 |
+
"**5. Proper Watering:**\n",
|
| 339 |
+
"\n",
|
| 340 |
+
"* Water your trees deeply but infrequently. Avoid overhead watering, as water droplets can spread the disease.\n",
|
| 341 |
+
"\n",
|
| 342 |
+
"**6. Avoid Injury:**\n",
|
| 343 |
+
"\n",
|
| 344 |
+
"* Handle your trees carefully to avoid damaging the bark. Cuts and wounds can provide entry points for the disease.\n",
|
| 345 |
+
"\n",
|
| 346 |
+
"**7. Copper-Based Sprays:**\n",
|
| 347 |
+
"\n",
|
| 348 |
+
"* Ask your local agricultural officer about copper-based fungicides. These can be applied as a preventive measure, especially during the summer months when the disease is more active. **Always follow instructions carefully** and use protective gear.\n",
|
| 349 |
+
"\n",
|
| 350 |
+
"**8. Early Detection:**\n",
|
| 351 |
+
"\n",
|
| 352 |
+
"* Learn to recognize the symptoms of mangu: small, dark spots on leaves, fruits, and twigs. If you see any signs, isolate the affected trees and consult your agricultural officer immediately.\n",
|
| 353 |
+
"\n",
|
| 354 |
+
"Remember, prevention is key to managing mangu. By following these practices, you can help keep your orange trees healthy and productive. \n",
|
| 355 |
+
"\n",
|
| 356 |
+
"\n",
|
| 357 |
+
"\n"
|
| 358 |
+
]
|
| 359 |
+
}
|
| 360 |
+
],
|
| 361 |
+
"source": [
|
| 362 |
+
"response = chain.run(\n",
|
| 363 |
+
" question=\"How can I prevent mangu \",\n",
|
| 364 |
+
" location=\"Nalgonda, India\",\n",
|
| 365 |
+
" crop=\"orange\",\n",
|
| 366 |
+
" season=\"Summer\"\n",
|
| 367 |
+
")\n",
|
| 368 |
+
"\n",
|
| 369 |
+
"print(response)\n"
|
| 370 |
+
]
|
| 371 |
+
},
|
| 372 |
+
{
|
| 373 |
+
"cell_type": "code",
|
| 374 |
+
"execution_count": 22,
|
| 375 |
+
"metadata": {},
|
| 376 |
+
"outputs": [
|
| 377 |
+
{
|
| 378 |
+
"name": "stdout",
|
| 379 |
+
"output_type": "stream",
|
| 380 |
+
"text": [
|
| 381 |
+
"You are an expert agriculture assistant helping farmers with their queries. Provide a detailed yet simple answer for the given question.\n",
|
| 382 |
+
"\n",
|
| 383 |
+
"๐ **Farmer's Question:** How can I prevent pests in my tomato crop?\n",
|
| 384 |
+
"๐ **Location:** Maharashtra, India\n",
|
| 385 |
+
"๐พ **Crop Type:** Tomato\n",
|
| 386 |
+
"๐ **Current Season:** Summer\n",
|
| 387 |
+
"\n",
|
| 388 |
+
"### ๐ฑ Problem Analysis\n",
|
| 389 |
+
"1๏ธโฃ **Possible Reasons:**\n",
|
| 390 |
+
"- Identify the key reasons causing this issue.\n",
|
| 391 |
+
"- Mention environmental, soil, or pest-related causes if relevant.\n",
|
| 392 |
+
"\n",
|
| 393 |
+
"2๏ธโฃ **Solution Approach:**\n",
|
| 394 |
+
"- Provide practical and actionable steps to resolve the issue.\n",
|
| 395 |
+
"- Include organic and chemical solutions if applicable.\n",
|
| 396 |
+
"\n",
|
| 397 |
+
"3๏ธโฃ **Preventive Measures:**\n",
|
| 398 |
+
"- Suggest best farming practices to avoid this issue in the future.\n",
|
| 399 |
+
"- Mention crop rotation, irrigation techniques, or natural remedies.\n",
|
| 400 |
+
"\n",
|
| 401 |
+
"4๏ธโฃ **Expert Tips:**\n",
|
| 402 |
+
"- Provide any additional insights from agricultural experts.\n",
|
| 403 |
+
"- Mention tools, fertilizers, or techniques that could be useful.\n",
|
| 404 |
+
"\n",
|
| 405 |
+
"๐ข Provide your response in **simple and easy-to-understand** language so that farmers can easily apply the solution.\n"
|
| 406 |
+
]
|
| 407 |
+
}
|
| 408 |
+
],
|
| 409 |
+
"source": [
|
| 410 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 411 |
+
"\n",
|
| 412 |
+
"agriculture_prompt = PromptTemplate(\n",
|
| 413 |
+
" input_variables=[\"question\", \"location\", \"crop\", \"season\"],\n",
|
| 414 |
+
" template=(\n",
|
| 415 |
+
" \"You are an expert agriculture assistant helping farmers with their queries. \"\n",
|
| 416 |
+
" \"Provide a detailed yet simple answer for the given question.\\n\\n\"\n",
|
| 417 |
+
" \"๐ **Farmer's Question:** {question}\\n\"\n",
|
| 418 |
+
" \"๐ **Location:** {location}\\n\"\n",
|
| 419 |
+
" \"๐พ **Crop Type:** {crop}\\n\"\n",
|
| 420 |
+
" \"๐ **Current Season:** {season}\\n\\n\"\n",
|
| 421 |
+
" \"### ๐ฑ Problem Analysis\\n\"\n",
|
| 422 |
+
" \"1๏ธโฃ **Possible Reasons:**\\n\"\n",
|
| 423 |
+
" \"- Identify the key reasons causing this issue.\\n\"\n",
|
| 424 |
+
" \"- Mention environmental, soil, or pest-related causes if relevant.\\n\\n\"\n",
|
| 425 |
+
" \"2๏ธโฃ **Solution Approach:**\\n\"\n",
|
| 426 |
+
" \"- Provide practical and actionable steps to resolve the issue.\\n\"\n",
|
| 427 |
+
" \"- Include organic and chemical solutions if applicable.\\n\\n\"\n",
|
| 428 |
+
" \"3๏ธโฃ **Preventive Measures:**\\n\"\n",
|
| 429 |
+
" \"- Suggest best farming practices to avoid this issue in the future.\\n\"\n",
|
| 430 |
+
" \"- Mention crop rotation, irrigation techniques, or natural remedies.\\n\\n\"\n",
|
| 431 |
+
" \"4๏ธโฃ **Expert Tips:**\\n\"\n",
|
| 432 |
+
" \"- Provide any additional insights from agricultural experts.\\n\"\n",
|
| 433 |
+
" \"- Mention tools, fertilizers, or techniques that could be useful.\\n\\n\"\n",
|
| 434 |
+
" \"๐ข Provide your response in **simple and easy-to-understand** language \"\n",
|
| 435 |
+
" \"so that farmers can easily apply the solution.\"\n",
|
| 436 |
+
" )\n",
|
| 437 |
+
")\n",
|
| 438 |
+
"\n",
|
| 439 |
+
"# Example usage:\n",
|
| 440 |
+
"query = agriculture_prompt.format(\n",
|
| 441 |
+
" question=\"How can I prevent pests in my tomato crop?\",\n",
|
| 442 |
+
" location=\"Maharashtra, India\",\n",
|
| 443 |
+
" crop=\"Tomato\",\n",
|
| 444 |
+
" season=\"Summer\"\n",
|
| 445 |
+
")\n",
|
| 446 |
+
"\n",
|
| 447 |
+
"print(query)\n"
|
| 448 |
+
]
|
| 449 |
+
},
|
| 450 |
+
{
|
| 451 |
+
"cell_type": "code",
|
| 452 |
+
"execution_count": 23,
|
| 453 |
+
"metadata": {},
|
| 454 |
+
"outputs": [],
|
| 455 |
+
"source": [
|
| 456 |
+
"chain=LLMChain(llm=llm,prompt=agriculture_prompt)"
|
| 457 |
+
]
|
| 458 |
+
},
|
| 459 |
+
{
|
| 460 |
+
"cell_type": "code",
|
| 461 |
+
"execution_count": 26,
|
| 462 |
+
"metadata": {},
|
| 463 |
+
"outputs": [
|
| 464 |
+
{
|
| 465 |
+
"name": "stdout",
|
| 466 |
+
"output_type": "stream",
|
| 467 |
+
"text": [
|
| 468 |
+
"## Keeping Pests Away from Your Tomatoes in Maharashtra's Summer\n",
|
| 469 |
+
"\n",
|
| 470 |
+
"Hello! Summer in Maharashtra can be tough on tomatoes, especially with all the pests that come along. Don't worry, here's how to protect your crop:\n",
|
| 471 |
+
"\n",
|
| 472 |
+
"**1. What's Bugging Your Tomatoes?**\n",
|
| 473 |
+
"\n",
|
| 474 |
+
"* **Aphids:** Tiny green or black bugs sucking sap from leaves. They make your plants weak.\n",
|
| 475 |
+
"* **Whiteflies:** Tiny, white, flying insects that also suck sap. They can make leaves yellow and drop.\n",
|
| 476 |
+
"* **Fruitworms:** Caterpillars that eat into your tomatoes. \n",
|
| 477 |
+
"* **Cutworms:** These fat, grey caterpillars cut off young tomato plants at the base.\n",
|
| 478 |
+
"\n",
|
| 479 |
+
"**2. Fighting Back!**\n",
|
| 480 |
+
"\n",
|
| 481 |
+
"* **For Aphids & Whiteflies:**\n",
|
| 482 |
+
"\n",
|
| 483 |
+
" * **Neem Oil:** Mix neem oil with water and spray on your plants. It's a natural insecticide that keeps these pests away.\n",
|
| 484 |
+
" * **Soap Spray:** Mix a little soap with water and spray it on your plants. This can also help control these small pests.\n",
|
| 485 |
+
"* **For Fruitworms:**\n",
|
| 486 |
+
"\n",
|
| 487 |
+
" * **Traps:** Set up sticky traps near your tomato plants to catch adult moths.\n",
|
| 488 |
+
" * **Handpicking:** Check your plants daily and remove any caterpillars you find.\n",
|
| 489 |
+
"* **For Cutworms:**\n",
|
| 490 |
+
"\n",
|
| 491 |
+
" * **Protect Young Plants:** Wrap the base of young plants with cardboard collars to prevent cutworms from reaching them.\n",
|
| 492 |
+
"\n",
|
| 493 |
+
"**3. Prevention is Key!**\n",
|
| 494 |
+
"\n",
|
| 495 |
+
"* **Healthy Soil:** Use compost to improve your soil. Healthy soil makes strong plants that are less likely to be attacked by pests.\n",
|
| 496 |
+
"* **Crop Rotation:** Don't plant tomatoes in the same place year after year. Rotate with other crops like beans or corn.\n",
|
| 497 |
+
"* **Companion Planting:** Plant basil, marigolds, or onions near your tomatoes. They can help repel pests naturally.\n",
|
| 498 |
+
"* **Proper Watering:** Don't overwater your tomatoes, as this can attract pests. Water deeply but infrequently.\n",
|
| 499 |
+
"* **Monitor Regularly:** Check your plants often for signs of pests. Early detection is key to stopping an infestation!\n",
|
| 500 |
+
"\n",
|
| 501 |
+
"**4. Expert Tips:**\n",
|
| 502 |
+
"\n",
|
| 503 |
+
"* **Contact your local agricultural extension office:** They can provide specific advice for your region.\n",
|
| 504 |
+
"* **Learn about Integrated Pest Management (IPM):** This approach combines different pest control methods, including natural ones, to minimize harm to the environment.\n",
|
| 505 |
+
"\n",
|
| 506 |
+
"\n",
|
| 507 |
+
"Remember, protecting your tomato crop takes a little effort, but with these tips, you can enjoy a bountiful harvest!\n",
|
| 508 |
+
"\n"
|
| 509 |
+
]
|
| 510 |
+
}
|
| 511 |
+
],
|
| 512 |
+
"source": [
|
| 513 |
+
"# Run the chain with inputs\n",
|
| 514 |
+
"response = chain.run(\n",
|
| 515 |
+
" question=\"How can I prevent pests in my tomato crop?\",\n",
|
| 516 |
+
" location=\"Maharashtra, India\",\n",
|
| 517 |
+
" crop=\"Tomato\",\n",
|
| 518 |
+
" season=\"Summer\"\n",
|
| 519 |
+
")\n",
|
| 520 |
+
"\n",
|
| 521 |
+
"print(response)\n"
|
| 522 |
+
]
|
| 523 |
+
},
|
| 524 |
+
{
|
| 525 |
+
"cell_type": "code",
|
| 526 |
+
"execution_count": 27,
|
| 527 |
+
"metadata": {},
|
| 528 |
+
"outputs": [],
|
| 529 |
+
"source": [
|
| 530 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 531 |
+
"\n",
|
| 532 |
+
"crop_disease_prompt = PromptTemplate(\n",
|
| 533 |
+
" input_variables=[\"symptoms\", \"crop\", \"location\", \"season\"],\n",
|
| 534 |
+
" template=(\n",
|
| 535 |
+
" \"You are an expert plant pathologist assisting farmers in diagnosing crop diseases.\\n\\n\"\n",
|
| 536 |
+
" \"๐ **Farmer's Observation:** {symptoms}\\n\"\n",
|
| 537 |
+
" \"๐ฑ **Crop:** {crop}\\n\"\n",
|
| 538 |
+
" \"๐ **Location:** {location}\\n\"\n",
|
| 539 |
+
" \"๐ **Current Season:** {season}\\n\\n\"\n",
|
| 540 |
+
" \"### ๐ฆ Possible Disease(s) and Causes:\\n\"\n",
|
| 541 |
+
" \"- Analyze the symptoms and identify possible diseases.\\n\"\n",
|
| 542 |
+
" \"- Mention environmental and pest-related causes.\\n\\n\"\n",
|
| 543 |
+
" \"### ๐ Treatment & Remedies:\\n\"\n",
|
| 544 |
+
" \"- Suggest **organic** and **chemical** treatments.\\n\"\n",
|
| 545 |
+
" \"- Recommend suitable pesticides or fungicides (if needed).\\n\\n\"\n",
|
| 546 |
+
" \"### ๐ก Preventive Measures:\\n\"\n",
|
| 547 |
+
" \"- Guide the farmer on crop rotation, irrigation, and soil treatment.\\n\"\n",
|
| 548 |
+
" \"- Suggest resistant crop varieties if available.\\n\\n\"\n",
|
| 549 |
+
" \"Provide clear, easy-to-follow instructions that farmers can apply practically.\"\n",
|
| 550 |
+
" )\n",
|
| 551 |
+
")\n"
|
| 552 |
+
]
|
| 553 |
+
},
|
| 554 |
+
{
|
| 555 |
+
"cell_type": "code",
|
| 556 |
+
"execution_count": 28,
|
| 557 |
+
"metadata": {},
|
| 558 |
+
"outputs": [],
|
| 559 |
+
"source": [
|
| 560 |
+
"chain=LLMChain(llm=llm,prompt=crop_disease_prompt)"
|
| 561 |
+
]
|
| 562 |
+
},
|
| 563 |
+
{
|
| 564 |
+
"cell_type": "code",
|
| 565 |
+
"execution_count": 34,
|
| 566 |
+
"metadata": {},
|
| 567 |
+
"outputs": [
|
| 568 |
+
{
|
| 569 |
+
"name": "stdout",
|
| 570 |
+
"output_type": "stream",
|
| 571 |
+
"text": [
|
| 572 |
+
"## Yellow Spots on Tomato Leaves: A Guide for Punjab Farmers\n",
|
| 573 |
+
"\n",
|
| 574 |
+
"**Possible Diseases:**\n",
|
| 575 |
+
"\n",
|
| 576 |
+
"Based on your observation of yellow spots on tomato leaves and stunted growth during summer in Punjab, here are some possible diseases:\n",
|
| 577 |
+
"\n",
|
| 578 |
+
"* **Early Blight (Alternaria solani):** This fungal disease is very common in tomato during hot and humid weather. It causes small, brown, target-like lesions with yellow halos on the lower leaves, eventually spreading upwards. \n",
|
| 579 |
+
"* **Septoria Leaf Spot (Septoria lycopersici):** Another fungal disease, Septoria leaf spot appears as small, circular, dark brown to black spots with yellow halos on the leaves. \n",
|
| 580 |
+
"* **Tomato Mosaic Virus (ToMV):** This viral disease can cause mosaic patterns (yellowing and greening) on leaves, stunted growth, and fruit deformities.\n",
|
| 581 |
+
"\n",
|
| 582 |
+
"**Environmental & Pest-related Causes:**\n",
|
| 583 |
+
"\n",
|
| 584 |
+
"* **High humidity and temperatures:** Both early blight and septoria leaf spot thrive in warm, humid conditions common in Punjab summers.\n",
|
| 585 |
+
"* **Overwatering:** Excessive watering can create ideal conditions for fungal diseases.\n",
|
| 586 |
+
"* **Poor air circulation:** Dense planting or lack of pruning can hinder air flow, promoting fungal growth.\n",
|
| 587 |
+
"\n",
|
| 588 |
+
"**Treatment & Remedies:**\n",
|
| 589 |
+
"\n",
|
| 590 |
+
"**Organic Options:**\n",
|
| 591 |
+
"\n",
|
| 592 |
+
"* **Neem oil:** Mix 2-3 teaspoons of neem oil with 1 liter of water and spray on affected plants. Neem oil has antifungal and insecticidal properties.\n",
|
| 593 |
+
"* **Copper fungicide:** Apply a copper-based fungicide according to label instructions.\n",
|
| 594 |
+
"\n",
|
| 595 |
+
"**Chemical Options:**\n",
|
| 596 |
+
"\n",
|
| 597 |
+
"* **Mancozeb:** This fungicide is effective against early blight and septoria leaf spot.\n",
|
| 598 |
+
"* **Chlorothalonil:** Another broad-spectrum fungicide that can be used.\n",
|
| 599 |
+
"\n",
|
| 600 |
+
"**Always follow label instructions for application rates and safety precautions.**\n",
|
| 601 |
+
"\n",
|
| 602 |
+
"**Preventive Measures:**\n",
|
| 603 |
+
"\n",
|
| 604 |
+
"* **Crop rotation:** Avoid planting tomatoes in the same field year after year. \n",
|
| 605 |
+
"* **Proper irrigation:** Water deeply but infrequently, ensuring good drainage. \n",
|
| 606 |
+
"* **Ensure good air circulation:** Space plants adequately and prune suckers to improve airflow.\n",
|
| 607 |
+
"* **Resistant varieties:** Choose tomato varieties resistant to early blight and septoria leaf spot. Ask your local agricultural extension office for recommendations.\n",
|
| 608 |
+
"* **Healthy soil:** Conduct soil tests and amend as needed.\n",
|
| 609 |
+
"\n",
|
| 610 |
+
"**Additional Tips:**\n",
|
| 611 |
+
"\n",
|
| 612 |
+
"* **Monitor your plants regularly.** Early detection and intervention are crucial for managing diseases.\n",
|
| 613 |
+
"* **Remove infected leaves and dispose of them properly.** This helps prevent the spread of disease.\n",
|
| 614 |
+
"* **Consider consulting a plant pathologist for a definitive diagnosis and tailored advice.**\n",
|
| 615 |
+
"\n",
|
| 616 |
+
"\n",
|
| 617 |
+
"Remember, a healthy crop starts with a healthy soil and proper management practices. \n",
|
| 618 |
+
"\n",
|
| 619 |
+
"\n",
|
| 620 |
+
"\n"
|
| 621 |
+
]
|
| 622 |
+
}
|
| 623 |
+
],
|
| 624 |
+
"source": [
|
| 625 |
+
"response = chain.run(\n",
|
| 626 |
+
" symptoms=\"Yellow spots on leaves, stunted growth.\",\n",
|
| 627 |
+
" crop=\"Tomato\",\n",
|
| 628 |
+
" location=\"Punjab, India\",\n",
|
| 629 |
+
" season=\"Summer\"\n",
|
| 630 |
+
")\n",
|
| 631 |
+
"print(response)"
|
| 632 |
+
]
|
| 633 |
+
},
|
| 634 |
+
{
|
| 635 |
+
"cell_type": "code",
|
| 636 |
+
"execution_count": 35,
|
| 637 |
+
"metadata": {},
|
| 638 |
+
"outputs": [],
|
| 639 |
+
"source": [
|
| 640 |
+
"from langchain.prompts import PromptTemplate\n",
|
| 641 |
+
"\n",
|
| 642 |
+
"soil_health_prompt = PromptTemplate(\n",
|
| 643 |
+
" input_variables=[\"soil_type\", \"pH\", \"nutrient_levels\", \"crop\"],\n",
|
| 644 |
+
" template=(\n",
|
| 645 |
+
" \"You are a soil scientist helping farmers analyze soil health and recommend fertilizers.\\n\\n\"\n",
|
| 646 |
+
" \"๐งช **Soil Type:** {soil_type}\\n\"\n",
|
| 647 |
+
" \"๐ **pH Level:** {pH}\\n\"\n",
|
| 648 |
+
" \"๐ฑ **Nutrient Levels (NPK & others):** {nutrient_levels}\\n\"\n",
|
| 649 |
+
" \"๐พ **Crop Being Grown:** {crop}\\n\\n\"\n",
|
| 650 |
+
" \"### ๐ Soil Health Analysis:\\n\"\n",
|
| 651 |
+
" \"- Explain the current condition of the soil.\\n\"\n",
|
| 652 |
+
" \"- Identify deficiencies or imbalances.\\n\\n\"\n",
|
| 653 |
+
" \"### ๐ Fertilizer & Soil Treatment Recommendations:\\n\"\n",
|
| 654 |
+
" \"- Suggest suitable **organic** and **chemical** fertilizers.\\n\"\n",
|
| 655 |
+
" \"- Mention appropriate dosages and application frequency.\\n\\n\"\n",
|
| 656 |
+
" \"### ๐ฟ Long-Term Soil Improvement:\\n\"\n",
|
| 657 |
+
" \"- Recommend crop rotation strategies.\\n\"\n",
|
| 658 |
+
" \"- Suggest composting or natural soil enrichment techniques.\\n\"\n",
|
| 659 |
+
" \"Provide advice in an **easy-to-follow** format for farmers.\"\n",
|
| 660 |
+
" )\n",
|
| 661 |
+
")\n"
|
| 662 |
+
]
|
| 663 |
+
},
|
| 664 |
+
{
|
| 665 |
+
"cell_type": "code",
|
| 666 |
+
"execution_count": 36,
|
| 667 |
+
"metadata": {},
|
| 668 |
+
"outputs": [],
|
| 669 |
+
"source": [
|
| 670 |
+
"chain=LLMChain(llm=llm,prompt=soil_health_prompt)"
|
| 671 |
+
]
|
| 672 |
+
},
|
| 673 |
+
{
|
| 674 |
+
"cell_type": "code",
|
| 675 |
+
"execution_count": 38,
|
| 676 |
+
"metadata": {},
|
| 677 |
+
"outputs": [
|
| 678 |
+
{
|
| 679 |
+
"name": "stdout",
|
| 680 |
+
"output_type": "stream",
|
| 681 |
+
"text": [
|
| 682 |
+
"## Your Tomato Soil Check-Up:\n",
|
| 683 |
+
"\n",
|
| 684 |
+
"**Current Condition:**\n",
|
| 685 |
+
"\n",
|
| 686 |
+
"Your loamy soil is a great foundation for tomatoes! It's well-draining and holds moisture nicely. The pH of 6.2 is also ideal for tomato growth. \n",
|
| 687 |
+
"\n",
|
| 688 |
+
"**Nutrient Needs:**\n",
|
| 689 |
+
"\n",
|
| 690 |
+
"Your soil is a bit low on nitrogen, which is essential for leafy growth and overall plant vigor. Phosphorus levels are moderate, good for root development and flowering.\n",
|
| 691 |
+
"\n",
|
| 692 |
+
"**Fertilizer & Treatment Recommendations:**\n",
|
| 693 |
+
"\n",
|
| 694 |
+
"**Organic Options:**\n",
|
| 695 |
+
"\n",
|
| 696 |
+
"* **Compost:** This is a powerhouse! Mix in 2-3 inches of well-rotted compost before planting to boost all nutrients, improve soil structure, and feed beneficial microbes.\n",
|
| 697 |
+
"* **Blood Meal:** High in nitrogen, apply 1-2 tablespoons per tomato plant every 4-6 weeks.\n",
|
| 698 |
+
"* **Bone Meal:** Adds phosphorus and calcium, apply 1-2 tablespoons per plant at planting time and again in mid-season.\n",
|
| 699 |
+
"\n",
|
| 700 |
+
"**Chemical Options:**\n",
|
| 701 |
+
"\n",
|
| 702 |
+
"* **Granular NPK Fertilizer:** Look for an NPK ratio like 12-6-6. Apply 1-2 tablespoons per plant at planting time and again every 4-6 weeks.\n",
|
| 703 |
+
"* **Liquid Nitrogen Fertilizer:** Apply every 2-3 weeks during the growing season, following instructions on the label.\n",
|
| 704 |
+
"\n",
|
| 705 |
+
"**Important Note:** Always apply fertilizers according to package instructions and avoid over-fertilizing, which can harm your plants. \n",
|
| 706 |
+
"\n",
|
| 707 |
+
"**Long-Term Soil Improvement:**\n",
|
| 708 |
+
"\n",
|
| 709 |
+
"* **Crop Rotation:** Avoid planting tomatoes in the same spot year after year. Rotate with crops like beans, squash, or corn to replenish nutrients and break pest cycles.\n",
|
| 710 |
+
"* **Cover Crops:** Plant clover or rye in the off-season to prevent erosion, suppress weeds, and fix nitrogen in the soil.\n",
|
| 711 |
+
"\n",
|
| 712 |
+
"By following these tips, you can keep your soil healthy and your tomatoes thriving!\n",
|
| 713 |
+
"\n",
|
| 714 |
+
"\n",
|
| 715 |
+
"\n"
|
| 716 |
+
]
|
| 717 |
+
}
|
| 718 |
+
],
|
| 719 |
+
"source": [
|
| 720 |
+
"response = chain.run(\n",
|
| 721 |
+
" soil_type=\"Loamy\",\n",
|
| 722 |
+
" pH=\"6.2\",\n",
|
| 723 |
+
" nutrient_levels=\"Low nitrogen, moderate phosphorus\",\n",
|
| 724 |
+
" crop=\"Tomato\"\n",
|
| 725 |
+
")\n",
|
| 726 |
+
"print(response)"
|
| 727 |
+
]
|
| 728 |
+
},
|
| 729 |
+
{
|
| 730 |
+
"cell_type": "code",
|
| 731 |
+
"execution_count": null,
|
| 732 |
+
"metadata": {},
|
| 733 |
+
"outputs": [],
|
| 734 |
+
"source": []
|
| 735 |
+
}
|
| 736 |
+
],
|
| 737 |
+
"metadata": {
|
| 738 |
+
"kernelspec": {
|
| 739 |
+
"display_name": "Python 3",
|
| 740 |
+
"language": "python",
|
| 741 |
+
"name": "python3"
|
| 742 |
+
},
|
| 743 |
+
"language_info": {
|
| 744 |
+
"codemirror_mode": {
|
| 745 |
+
"name": "ipython",
|
| 746 |
+
"version": 3
|
| 747 |
+
},
|
| 748 |
+
"file_extension": ".py",
|
| 749 |
+
"mimetype": "text/x-python",
|
| 750 |
+
"name": "python",
|
| 751 |
+
"nbconvert_exporter": "python",
|
| 752 |
+
"pygments_lexer": "ipython3",
|
| 753 |
+
"version": "3.10.0"
|
| 754 |
+
}
|
| 755 |
+
},
|
| 756 |
+
"nbformat": 4,
|
| 757 |
+
"nbformat_minor": 2
|
| 758 |
+
}
|