File size: 16,870 Bytes
2624bae
 
02508bd
 
2624bae
02508bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2624bae
 
 
02508bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2624bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c13ad74
02508bd
 
 
 
ebd8505
02508bd
 
 
 
 
 
 
c13ad74
02508bd
 
 
 
ebd8505
 
 
 
 
 
02508bd
 
 
 
 
 
 
 
c13ad74
 
 
ebd8505
 
c13ad74
 
ebd8505
02508bd
 
ebd8505
02508bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c13ad74
ebd8505
 
02508bd
c13ad74
 
02508bd
 
 
2624bae
 
 
 
 
 
 
 
 
 
 
02508bd
 
 
 
 
 
 
 
 
 
 
 
 
c13ad74
ebd8505
 
c13ad74
 
 
ebd8505
 
 
 
 
 
 
2624bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# New code in which sensor data is taken directly from thinkspace.

import os
import json
import requests
from datetime import datetime
import streamlit as st
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain_groq import ChatGroq
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from deep_translator import GoogleTranslator

# Directory paths and configurations
working_dir = os.path.dirname(os.path.abspath(__file__))
config_data = json.load(open(f"{working_dir}/config.json"))
GROQ_API_KEY = config_data["GROQ_API_KEY"]
os.environ["GROQ_API_KEY"] = GROQ_API_KEY

# ThinkSpace API details
THINGSPEAK_API_URL = "https://api.thingspeak.com/channels/2485113/feeds.json?results=2"

# Vectorstore setup
def setup_vectorstore():
    embeddings = HuggingFaceEmbeddings()
    vectorstore = Chroma(persist_directory="soil_vectordb", embedding_function=embeddings)
    return vectorstore

# Chatbot chain setup
def chat_chain(vectorstore):
    llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0)
    retriever = vectorstore.as_retriever()
    memory = ConversationBufferMemory(
        llm=llm,
        output_key="answer",
        memory_key="chat_history",
        return_messages=True
    )
    chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=retriever,
        chain_type="stuff",
        memory=memory,
        verbose=True,
        return_source_documents=True
    )
    return chain

# Fetch sensor data from ThinkSpace API
def fetch_sensor_data():
    try:
        response = requests.get(THINGSPEAK_API_URL)
        if response.status_code == 200:
            data = response.json()
            feeds = data.get("feeds", [])
            if feeds:
                latest_feed = feeds[-1]  # Get the latest feed
                return {
                    "pH": float(latest_feed.get("field1", 0)),
                    "moisture": float(latest_feed.get("field2", 0)),
                    "temperature": float(latest_feed.get("field3", 0)),
                    "air_quality": float(latest_feed.get("field4", 0)),
                }
        else:
            st.error("Failed to fetch data from ThinkSpace API. Please check the API URL or connectivity.")
            return None
    except Exception as e:
        st.error(f"An error occurred while fetching sensor data: {e}")
        return None

# Updated Streamlit setup with language selection dropdown
st.set_page_config(page_title="Soil.Ai", page_icon="🌱", layout="centered")
st.title("🌱 Soil.Ai - Smart Farming Recommendations")
st.subheader("AI-driven solutions for modern farming!")

# Initialize session state
if "username" not in st.session_state:
    username = st.text_input("Enter your name to proceed:")
    if username:
        with st.spinner("Loading AI interface..."):
            st.session_state.username = username
            st.session_state.vectorstore = setup_vectorstore()
            st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
            st.session_state.selected_language = "English"  # Default language
            st.success(f"Welcome, {username}! Start by choosing an option.")
else:
    username = st.session_state.username

# Language options
languages = [
    "English", "Marathi", "Hindi", "Bengali", "Gujarati", "Kannada", "Malayalam",
    "Odia", "Punjabi", "Tamil", "Telugu", "Urdu", "Spanish", "French", "German"
]

# Main interface
if "conversational_chain" not in st.session_state:
    st.session_state.vectorstore = setup_vectorstore()
    st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)

if "username" in st.session_state:
    st.subheader(f"Hello {username}, choose your option below:")

    # Dropdown for selecting output language
    st.session_state.selected_language = st.selectbox(
        "Select output language:",
        languages,
        index=languages.index(st.session_state.get("selected_language", "English"))
    )

    # Option selection
    option = st.radio(
        "Choose an action:",
        ("Ask a general agriculture-related question", "Input sensor data for recommendations", "Satellite Data", "FAQ Section")
    )

    # Option 1: Ask AI any agriculture-related question
    if option == "Ask a general agriculture-related question":
        user_query = st.chat_input("Ask AI anything about agriculture...")
        if user_query:
            with st.spinner("Processing your query..."):
                # Display user's query
                with st.chat_message("user"):
                    st.markdown(user_query)

                # Get assistant's response
                with st.chat_message("assistant"):
                    response = st.session_state.conversational_chain({"question": user_query})
                    assistant_response = response["answer"]

                    # Translate response based on selected language
                    translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
                    translated_response = translator.translate(assistant_response)

                    # Display response in selected language
                    st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")

    # Option 2: Input sensor data for recommendations
    elif option == "Input sensor data for recommendations":
        st.markdown("### Fetching data from sensors...")
        sensor_data = fetch_sensor_data()
        if sensor_data:
            ph = sensor_data["pH"]
            moisture = sensor_data["moisture"]
            temperature = sensor_data["temperature"]
            air_quality = sensor_data["air_quality"]

            st.markdown(f"**Sensor Data:**\n- pH: {ph}\n- Moisture: {moisture}%\n- Temperature: {temperature}°C\n- Air Quality: {air_quality}")

            if st.button("Get Recommendations"):
                with st.spinner("Analyzing data..."):
                    # Prepare input query
                    user_input = f"Recommendations for:\n- pH: {ph}\n- Moisture: {moisture}%\n- Temperature: {temperature}°C\n- Air Quality: {air_quality}"

                    # Display user's input
                    with st.chat_message("user"):
                        st.markdown(user_input)

                    # Get assistant's response
                    with st.chat_message("assistant"):
                        response = st.session_state.conversational_chain({"question": user_input})
                        assistant_response = response["answer"]

                        # Translate response based on selected language
                        translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
                        translated_response = translator.translate(assistant_response)

                        # Display response in selected language
                        st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")

    # Option 3: Satellite Data
    elif option == "Satellite Data":
        st.markdown("### Satellite Data Functionality Coming Soon!")

    # Option 4: FAQ Section
    elif option == "FAQ Section":
        st.markdown("### FAQs Coming Soon!")











# OLD code

# import os
# import json
# from datetime import datetime
# import streamlit as st
# from langchain_huggingface import HuggingFaceEmbeddings
# from langchain_chroma import Chroma
# from langchain_groq import ChatGroq
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import ConversationalRetrievalChain
# from deep_translator import GoogleTranslator

# # Directory paths and configurations
# working_dir = os.path.dirname(os.path.abspath(__file__))
# config_data = json.load(open(f"{working_dir}/config.json"))
# GROQ_API_KEY = config_data["GROQ_API_KEY"]
# os.environ["GROQ_API_KEY"] = GROQ_API_KEY

# # Vectorstore setup
# def setup_vectorstore():
#     embeddings = HuggingFaceEmbeddings()
#     vectorstore = Chroma(persist_directory="soil_vectordb", embedding_function=embeddings)
#     return vectorstore

# # Chatbot chain setup
# def chat_chain(vectorstore):
#     llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0)
#     retriever = vectorstore.as_retriever()
#     memory = ConversationBufferMemory(
#         llm=llm,
#         output_key="answer",
#         memory_key="chat_history",
#         return_messages=True
#     )
#     chain = ConversationalRetrievalChain.from_llm(
#         llm=llm,
#         retriever=retriever,
#         chain_type="stuff",
#         memory=memory,
#         verbose=True,
#         return_source_documents=True
#     )
#     return chain

# # Updated Streamlit setup with language selection dropdown
# st.set_page_config(page_title="Soil.Ai", page_icon="🌱", layout="centered")
# st.title("🌱 Soil.Ai - Smart Farming Recommendations")
# st.subheader("AI-driven solutions for modern farming!")

# # Initialize session state
# if "username" not in st.session_state:
#     username = st.text_input("Enter your name to proceed:")
#     if username:
#         with st.spinner("Loading AI interface..."):
#             st.session_state.username = username
#             st.session_state.vectorstore = setup_vectorstore()
#             st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
#             st.session_state.selected_language = "English"  # Default language
#             st.success(f"Welcome, {username}! Start by choosing an option.")
# else:
#     username = st.session_state.username

# # Language options
# languages = [
#     "English", "Marathi", "Hindi", "Bengali", "Gujarati", "Kannada", "Malayalam",
#     "Odia", "Punjabi", "Tamil", "Telugu", "Urdu", "Spanish", "French", "German"
# ]

# # Main interface
# if "conversational_chain" not in st.session_state:
#     st.session_state.vectorstore = setup_vectorstore()
#     st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)

# if "username" in st.session_state:
#     st.subheader(f"Hello {username}, choose your option below:")

#     # Dropdown for selecting output language
#     st.session_state.selected_language = st.selectbox(
#         "Select output language:",
#         languages,
#         index=languages.index(st.session_state.get("selected_language", "English"))
#     )

#     # Option selection
#     option = st.radio(
#         "Choose an action:",
#         ("Ask a general agriculture-related question", "Input sensor data for recommendations", "Satellite Data", "FAQ Section")
#     )

#     # Option 1: Ask AI any agriculture-related question
#     if option == "Ask a general agriculture-related question":
#         user_query = st.chat_input("Ask AI anything about agriculture...")
#         if user_query:
#             with st.spinner("Processing your query..."):
#                 # Display user's query
#                 with st.chat_message("user"):
#                     st.markdown(user_query)

#                 # Get assistant's response
#                 with st.chat_message("assistant"):
#                     response = st.session_state.conversational_chain({"question": user_query})
#                     assistant_response = response["answer"]

#                     # Translate response based on selected language
#                     translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
#                     translated_response = translator.translate(assistant_response)

#                     # Display response in selected language
#                     st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")

#     # Option 2: Input sensor data for recommendations
#     elif option == "Input sensor data for recommendations":
#         st.markdown("### Enter soil and environmental parameters:")
#         ph = st.number_input("Enter Soil pH", min_value=0.0, max_value=14.0, step=0.1)
#         moisture = st.number_input("Enter Soil Moisture (%)", min_value=0.0, max_value=100.0, step=0.1)
#         temperature = st.number_input("Enter Temperature (°C)", min_value=-50.0, max_value=60.0, step=0.1)
#         air_quality = st.number_input("Enter Air Quality Index (AQI)", min_value=0, max_value=500, step=1)

#         if st.button("Get Recommendations"):
#             if ph and moisture and temperature and air_quality:
#                 with st.spinner("Analyzing data..."):
#                     # Prepare input query
#                     user_input = f"Recommendations for:\n- pH: {ph}\n- Moisture: {moisture}%\n- Temperature: {temperature}°C\n- Air Quality: {air_quality}"

#                     # Display user's input
#                     with st.chat_message("user"):
#                         st.markdown(user_input)

#                     # Get assistant's response
#                     with st.chat_message("assistant"):
#                         response = st.session_state.conversational_chain({"question": user_input})
#                         assistant_response = response["answer"]

#                         # Translate response based on selected language
#                         translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
#                         translated_response = translator.translate(assistant_response)

#                         # Display response in selected language
#                         st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")
#             else:
#                 st.error("Please fill in all the fields!")

#     # Option 3: Satellite Data
#     elif option == "Satellite Data":
#         st.markdown("### Satellite Data Functionality Coming Soon!")

#     # Option 4: FAQ Section
#     elif option == "FAQ Section":
#         crop = st.radio("Select a crop for FAQs:", ("Cotton", "Tur"))
#         if crop == "Tur":
#             st.markdown("### *Q&A on Arhar Crop*")
#             tur_questions = [
#                 "Q1: What are the suitable climate and soil requirements for Arhar cultivation?",
#                 "Q2: What is the best time for sowing Arhar, and how much seed is needed per hectare?",
#                 "Q3: What are the improved varieties of Arhar and their characteristics?",
#                 "Q4: What fertilizers and irrigation are required for Arhar cultivation?",
#                 "Q5: What are the main pests and diseases affecting Arhar, and how can they be managed?"
#             ]
#             tur_answers = [
#                 "A: Arhar requires a warm and dry climate with a temperature range of 25-30°C. It thrives in well-drained loamy soil with a pH value of 6.0 to 7.5.",
#                 "A: The best time for sowing Arhar is from June to July (monsoon season). The seed requirement is 15-20 kg per hectare. The seeds should be treated with Trichoderma or Carbendazim before sowing.",
#                 "A: Some improved varieties of Arhar include ICPL-87 (early maturing), Sharad (high-yielding), and Pant Arhar-3 (short-duration).",
#                 "A: Fertilizers: Nitrogen: 20 kg/hectare, Phosphorus: 50 kg/hectare. Irrigation: Two to three irrigations during flowering and pod formation stages.",
#                 "A: Pests like pod borers and diseases like wilt (root rot) affect Arhar. Control measures include spraying neem oil and using disease-resistant varieties."
#             ]
#         elif crop == "Cotton":
#             st.markdown("### *Q&A on Cotton Crop*")
#             tur_questions = [
#                 "Q1: What is the suitable climate for cotton cultivation?",
#                 "Q2: How much water does cotton require during its growth?",
#                 "Q3: What are the common pests and diseases in cotton?",
#                 "Q4: Which fertilizers are best for cotton farming?",
#                 "Q5: What is the average yield of cotton per hectare?"
#             ]
#             tur_answers = [
#                 "A: Cotton grows well in warm climates with temperatures between 21-30°C.",
#                 "A: Cotton requires about 700-1300 mm of water depending on the variety and climate.",
#                 "A: Common pests include bollworms; diseases include leaf curl virus.",
#                 "A: Use nitrogen (60 kg/ha), phosphorus (30 kg/ha), and potassium (30 kg/ha).",
#                 "A: Average yield ranges between 500-800 kg/ha depending on the variety and conditions."
#             ]

#         for q, a in zip(tur_questions, tur_answers):
#             translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
#             st.markdown(f"**{translator.translate(q)}**\n\n{translator.translate(a)}")