Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
-
import sqlite3
|
4 |
from datetime import datetime
|
5 |
import streamlit as st
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
@@ -16,32 +15,6 @@ config_data = json.load(open(f"{working_dir}/config.json"))
|
|
16 |
GROQ_API_KEY = config_data["GROQ_API_KEY"]
|
17 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
18 |
|
19 |
-
# Database setup
|
20 |
-
def setup_db():
|
21 |
-
conn = sqlite3.connect("chat_history.db", check_same_thread=False)
|
22 |
-
cursor = conn.cursor()
|
23 |
-
cursor.execute("""
|
24 |
-
CREATE TABLE IF NOT EXISTS chat_histories (
|
25 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
26 |
-
username TEXT,
|
27 |
-
timestamp TEXT,
|
28 |
-
day TEXT,
|
29 |
-
user_input TEXT,
|
30 |
-
assistant_response TEXT
|
31 |
-
)
|
32 |
-
""")
|
33 |
-
conn.commit()
|
34 |
-
return conn
|
35 |
-
|
36 |
-
# Save chat history to SQLite
|
37 |
-
def save_chat_history(conn, username, timestamp, day, user_input, assistant_response):
|
38 |
-
cursor = conn.cursor()
|
39 |
-
cursor.execute("""
|
40 |
-
INSERT INTO chat_histories (username, timestamp, day, user_input, assistant_response)
|
41 |
-
VALUES (?, ?, ?, ?, ?)
|
42 |
-
""", (username, timestamp, day, user_input, assistant_response))
|
43 |
-
conn.commit()
|
44 |
-
|
45 |
# Vectorstore setup
|
46 |
def setup_vectorstore():
|
47 |
embeddings = HuggingFaceEmbeddings()
|
@@ -68,22 +41,17 @@ def chat_chain(vectorstore):
|
|
68 |
)
|
69 |
return chain
|
70 |
|
71 |
-
|
72 |
# Updated Streamlit setup with language selection dropdown
|
73 |
st.set_page_config(page_title="Soil.Ai", page_icon="🌱", layout="centered")
|
74 |
st.title("🌱 Soil.Ai - Smart Farming Recommendations")
|
75 |
st.subheader("AI-driven solutions for modern farming!")
|
76 |
|
77 |
-
# Initialize
|
78 |
-
if "conn" not in st.session_state:
|
79 |
-
st.session_state.conn = setup_db()
|
80 |
-
|
81 |
if "username" not in st.session_state:
|
82 |
username = st.text_input("Enter your name to proceed:")
|
83 |
if username:
|
84 |
with st.spinner("Loading AI interface..."):
|
85 |
st.session_state.username = username
|
86 |
-
st.session_state.chat_history = []
|
87 |
st.session_state.vectorstore = setup_vectorstore()
|
88 |
st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
|
89 |
st.session_state.selected_language = "English" # Default language
|
@@ -91,6 +59,12 @@ if "username" not in st.session_state:
|
|
91 |
else:
|
92 |
username = st.session_state.username
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
# Main interface
|
95 |
if "conversational_chain" not in st.session_state:
|
96 |
st.session_state.vectorstore = setup_vectorstore()
|
@@ -102,14 +76,14 @@ if "username" in st.session_state:
|
|
102 |
# Dropdown for selecting output language
|
103 |
st.session_state.selected_language = st.selectbox(
|
104 |
"Select output language:",
|
105 |
-
|
106 |
-
index=
|
107 |
)
|
108 |
|
109 |
-
# Option selection
|
110 |
option = st.radio(
|
111 |
"Choose an action:",
|
112 |
-
("Ask a general agriculture-related question", "Input sensor data for recommendations")
|
113 |
)
|
114 |
|
115 |
# Option 1: Ask AI any agriculture-related question
|
@@ -127,27 +101,12 @@ if "username" in st.session_state:
|
|
127 |
assistant_response = response["answer"]
|
128 |
|
129 |
# Translate response based on selected language
|
130 |
-
|
131 |
-
|
132 |
-
translated_response = translator.translate(assistant_response)
|
133 |
-
elif st.session_state.selected_language == "Hindi":
|
134 |
-
translator = GoogleTranslator(source="en", target="hi")
|
135 |
-
translated_response = translator.translate(assistant_response)
|
136 |
-
else: # Default: English
|
137 |
-
translated_response = assistant_response
|
138 |
|
139 |
# Display response in selected language
|
140 |
st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")
|
141 |
|
142 |
-
# Save chat history
|
143 |
-
st.session_state.chat_history.append({"role": "user", "content": user_query})
|
144 |
-
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
145 |
-
|
146 |
-
# Save to database
|
147 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
148 |
-
day = datetime.now().strftime("%A")
|
149 |
-
save_chat_history(st.session_state.conn, username, timestamp, day, user_query, assistant_response)
|
150 |
-
|
151 |
# Option 2: Input sensor data for recommendations
|
152 |
elif option == "Input sensor data for recommendations":
|
153 |
st.markdown("### Enter soil and environmental parameters:")
|
@@ -172,25 +131,54 @@ if "username" in st.session_state:
|
|
172 |
assistant_response = response["answer"]
|
173 |
|
174 |
# Translate response based on selected language
|
175 |
-
|
176 |
-
|
177 |
-
translated_response = translator.translate(assistant_response)
|
178 |
-
elif st.session_state.selected_language == "Hindi":
|
179 |
-
translator = GoogleTranslator(source="en", target="hi")
|
180 |
-
translated_response = translator.translate(assistant_response)
|
181 |
-
else: # Default: English
|
182 |
-
translated_response = assistant_response
|
183 |
|
184 |
# Display response in selected language
|
185 |
st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")
|
186 |
-
|
187 |
-
# Save chat history
|
188 |
-
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
189 |
-
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
190 |
-
|
191 |
-
# Save to database
|
192 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
193 |
-
day = datetime.now().strftime("%A")
|
194 |
-
save_chat_history(st.session_state.conn, username, timestamp, day, user_input, assistant_response)
|
195 |
else:
|
196 |
-
st.error("Please fill in all the fields!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
|
|
3 |
from datetime import datetime
|
4 |
import streamlit as st
|
5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
15 |
GROQ_API_KEY = config_data["GROQ_API_KEY"]
|
16 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Vectorstore setup
|
19 |
def setup_vectorstore():
|
20 |
embeddings = HuggingFaceEmbeddings()
|
|
|
41 |
)
|
42 |
return chain
|
43 |
|
|
|
44 |
# Updated Streamlit setup with language selection dropdown
|
45 |
st.set_page_config(page_title="Soil.Ai", page_icon="🌱", layout="centered")
|
46 |
st.title("🌱 Soil.Ai - Smart Farming Recommendations")
|
47 |
st.subheader("AI-driven solutions for modern farming!")
|
48 |
|
49 |
+
# Initialize session state
|
|
|
|
|
|
|
50 |
if "username" not in st.session_state:
|
51 |
username = st.text_input("Enter your name to proceed:")
|
52 |
if username:
|
53 |
with st.spinner("Loading AI interface..."):
|
54 |
st.session_state.username = username
|
|
|
55 |
st.session_state.vectorstore = setup_vectorstore()
|
56 |
st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
|
57 |
st.session_state.selected_language = "English" # Default language
|
|
|
59 |
else:
|
60 |
username = st.session_state.username
|
61 |
|
62 |
+
# Language options
|
63 |
+
languages = [
|
64 |
+
"English", "Marathi", "Hindi", "Bengali", "Gujarati", "Kannada", "Malayalam",
|
65 |
+
"Odia", "Punjabi", "Tamil", "Telugu", "Urdu", "Spanish", "French", "German"
|
66 |
+
]
|
67 |
+
|
68 |
# Main interface
|
69 |
if "conversational_chain" not in st.session_state:
|
70 |
st.session_state.vectorstore = setup_vectorstore()
|
|
|
76 |
# Dropdown for selecting output language
|
77 |
st.session_state.selected_language = st.selectbox(
|
78 |
"Select output language:",
|
79 |
+
languages,
|
80 |
+
index=languages.index(st.session_state.get("selected_language", "English"))
|
81 |
)
|
82 |
|
83 |
+
# Option selection
|
84 |
option = st.radio(
|
85 |
"Choose an action:",
|
86 |
+
("Ask a general agriculture-related question", "Input sensor data for recommendations", "Satellite Data", "FAQ Section")
|
87 |
)
|
88 |
|
89 |
# Option 1: Ask AI any agriculture-related question
|
|
|
101 |
assistant_response = response["answer"]
|
102 |
|
103 |
# Translate response based on selected language
|
104 |
+
translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
|
105 |
+
translated_response = translator.translate(assistant_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
# Display response in selected language
|
108 |
st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
# Option 2: Input sensor data for recommendations
|
111 |
elif option == "Input sensor data for recommendations":
|
112 |
st.markdown("### Enter soil and environmental parameters:")
|
|
|
131 |
assistant_response = response["answer"]
|
132 |
|
133 |
# Translate response based on selected language
|
134 |
+
translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
|
135 |
+
translated_response = translator.translate(assistant_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
# Display response in selected language
|
138 |
st.markdown(f"**{st.session_state.selected_language}:** {translated_response}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
else:
|
140 |
+
st.error("Please fill in all the fields!")
|
141 |
+
|
142 |
+
# Option 3: Satellite Data
|
143 |
+
elif option == "Satellite Data":
|
144 |
+
st.markdown("### Satellite Data Functionality Coming Soon!")
|
145 |
+
|
146 |
+
# Option 4: FAQ Section
|
147 |
+
elif option == "FAQ Section":
|
148 |
+
crop = st.radio("Select a crop for FAQs:", ("Cotton", "Tur"))
|
149 |
+
if crop == "Tur":
|
150 |
+
st.markdown("### *Q&A on Arhar Crop*")
|
151 |
+
tur_questions = [
|
152 |
+
"Q1: What are the suitable climate and soil requirements for Arhar cultivation?",
|
153 |
+
"Q2: What is the best time for sowing Arhar, and how much seed is needed per hectare?",
|
154 |
+
"Q3: What are the improved varieties of Arhar and their characteristics?",
|
155 |
+
"Q4: What fertilizers and irrigation are required for Arhar cultivation?",
|
156 |
+
"Q5: What are the main pests and diseases affecting Arhar, and how can they be managed?"
|
157 |
+
]
|
158 |
+
tur_answers = [
|
159 |
+
"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.",
|
160 |
+
"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.",
|
161 |
+
"A: Some improved varieties of Arhar include ICPL-87 (early maturing), Sharad (high-yielding), and Pant Arhar-3 (short-duration).",
|
162 |
+
"A: Fertilizers: Nitrogen: 20 kg/hectare, Phosphorus: 50 kg/hectare. Irrigation: Two to three irrigations during flowering and pod formation stages.",
|
163 |
+
"A: Pests like pod borers and diseases like wilt (root rot) affect Arhar. Control measures include spraying neem oil and using disease-resistant varieties."
|
164 |
+
]
|
165 |
+
elif crop == "Cotton":
|
166 |
+
st.markdown("### *Q&A on Cotton Crop*")
|
167 |
+
tur_questions = [
|
168 |
+
"Q1: What is the suitable climate for cotton cultivation?",
|
169 |
+
"Q2: How much water does cotton require during its growth?",
|
170 |
+
"Q3: What are the common pests and diseases in cotton?",
|
171 |
+
"Q4: Which fertilizers are best for cotton farming?",
|
172 |
+
"Q5: What is the average yield of cotton per hectare?"
|
173 |
+
]
|
174 |
+
tur_answers = [
|
175 |
+
"A: Cotton grows well in warm climates with temperatures between 21-30°C.",
|
176 |
+
"A: Cotton requires about 700-1300 mm of water depending on the variety and climate.",
|
177 |
+
"A: Common pests include bollworms; diseases include leaf curl virus.",
|
178 |
+
"A: Use nitrogen (60 kg/ha), phosphorus (30 kg/ha), and potassium (30 kg/ha).",
|
179 |
+
"A: Average yield ranges between 500-800 kg/ha depending on the variety and conditions."
|
180 |
+
]
|
181 |
+
|
182 |
+
for q, a in zip(tur_questions, tur_answers):
|
183 |
+
translator = GoogleTranslator(source="en", target=st.session_state.selected_language.lower())
|
184 |
+
st.markdown(f"**{translator.translate(q)}**\n\n{translator.translate(a)}")
|