Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,82 @@ from langchain.vectorstores import Chroma
|
|
6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
|
8 |
from langchain_groq import ChatGroq
|
9 |
-
from langchain.chains import RetrievalQA
|
10 |
from langchain.document_loaders import PyPDFLoader
|
11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Set persistent storage path
|
15 |
PERSISTENT_DIR = "vector_db"
|
@@ -58,53 +130,6 @@ api_key1 = os.getenv("api_key")
|
|
58 |
start_time = time.time()
|
59 |
st.set_page_config(page_title="Dr. Radha: The Agro-Homeopath", page_icon="π", layout="wide")
|
60 |
|
61 |
-
# CSS for dark green background and white text
|
62 |
-
st.markdown("""
|
63 |
-
<style>
|
64 |
-
/* Set only background color for app */
|
65 |
-
.stApp {
|
66 |
-
background-color: #1B4D3E !important;
|
67 |
-
}
|
68 |
-
|
69 |
-
/* Style all text elements in white */
|
70 |
-
.stMarkdown, .stText, .stTitle, .stHeader, .stSubheader,
|
71 |
-
.stTextInput label, .stSelectbox label, .st-emotion-cache-10trblm,
|
72 |
-
.st-emotion-cache-1a7jz76, .st-emotion-cache-1629p8f,
|
73 |
-
[data-testid="stTitle"], [data-testid="stSubheader"] {
|
74 |
-
color: white !important;
|
75 |
-
}
|
76 |
-
|
77 |
-
/* Additional specific selectors for title and subheader */
|
78 |
-
h1, h2, h3 {
|
79 |
-
color: white !important;
|
80 |
-
}
|
81 |
-
|
82 |
-
/* Keep button text black */
|
83 |
-
.stButton > button {
|
84 |
-
color: black !important;
|
85 |
-
background-color: yellow !important;
|
86 |
-
}
|
87 |
-
</style>
|
88 |
-
""", unsafe_allow_html=True)
|
89 |
-
|
90 |
-
# Add custom CSS for centering
|
91 |
-
st.markdown("""
|
92 |
-
<style>
|
93 |
-
/* Center align title and header */
|
94 |
-
.css-10trblm, .css-1a7jz76 {
|
95 |
-
text-align: center !important;
|
96 |
-
color: white !important;
|
97 |
-
}
|
98 |
-
|
99 |
-
/* Center align markdown text */
|
100 |
-
.center-text {
|
101 |
-
text-align: center !important;
|
102 |
-
margin: 0 auto;
|
103 |
-
display: block;
|
104 |
-
}
|
105 |
-
</style>
|
106 |
-
""", unsafe_allow_html=True)
|
107 |
-
|
108 |
# Title and subheader
|
109 |
st.title("πΏ Dr. Radha: AI-Powered Organic Farming Consultant")
|
110 |
st.subheader("Specializing in Agro-Homeopathy | Free Consultation")
|
@@ -235,53 +260,51 @@ Answer:"""
|
|
235 |
fallback_prompt = PromptTemplate(template=fallback_template, input_variables=["question"])
|
236 |
fallback_chain = LLMChain(llm=llm, prompt=fallback_prompt)
|
237 |
|
|
|
238 |
chat_container = st.container()
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
with st.form(key='query_form', clear_on_submit=True):
|
241 |
-
query = st.text_input(
|
242 |
-
|
243 |
-
""
|
244 |
-
|
245 |
-
.stTextInput label {
|
246 |
-
color: white !important;
|
247 |
-
}
|
248 |
-
.stButton > button {
|
249 |
-
color: black !important;
|
250 |
-
background-color: yellow !important;
|
251 |
-
font-weight: bold;
|
252 |
-
}
|
253 |
-
</style>
|
254 |
-
""",
|
255 |
-
unsafe_allow_html=True
|
256 |
)
|
257 |
-
submit_button = st.form_submit_button(label='
|
258 |
-
end_time = time.time()
|
259 |
-
#print(f"Setting up retrieval chain took {end_time - start_time:.4f} seconds")
|
260 |
-
start_time = time.time()
|
261 |
|
262 |
if submit_button and query:
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
|
|
|
|
|
|
282 |
|
283 |
-
|
|
|
284 |
|
|
|
285 |
st.session_state["query"] = ""
|
286 |
-
|
287 |
-
|
|
|
|
6 |
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
|
8 |
from langchain_groq import ChatGroq
|
9 |
+
from langchain.chains import RetrievalQA, LLMChain
|
10 |
from langchain.document_loaders import PyPDFLoader
|
11 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
12 |
+
|
13 |
+
# Set page configuration with wide layout
|
14 |
+
st.set_page_config(
|
15 |
+
page_title="Dr. Radha: The Agro-Homeopath",
|
16 |
+
page_icon="πΏ",
|
17 |
+
layout="wide",
|
18 |
+
initial_sidebar_state="collapsed"
|
19 |
+
)
|
20 |
+
|
21 |
+
# Enhanced CSS styling
|
22 |
+
st.markdown("""
|
23 |
+
<style>
|
24 |
+
/* Main container styling */
|
25 |
+
.stApp {
|
26 |
+
background-color: #1B4D3E;
|
27 |
+
}
|
28 |
+
|
29 |
+
/* Chat message styling */
|
30 |
+
.stChatMessage {
|
31 |
+
background-color: rgba(255, 255, 255, 0.1);
|
32 |
+
border-radius: 15px;
|
33 |
+
padding: 15px;
|
34 |
+
margin: 10px 0;
|
35 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
36 |
+
}
|
37 |
+
|
38 |
+
/* Input field styling */
|
39 |
+
.stChatInput {
|
40 |
+
border-radius: 20px !important;
|
41 |
+
border: 2px solid rgba(255, 255, 255, 0.2) !important;
|
42 |
+
background-color: rgba(255, 255, 255, 0.05) !important;
|
43 |
+
}
|
44 |
+
|
45 |
+
/* Text colors */
|
46 |
+
.stMarkdown, .stText, .stTitle, .stHeader, .stSubheader {
|
47 |
+
color: white !important;
|
48 |
+
}
|
49 |
+
|
50 |
+
/* Button styling */
|
51 |
+
.stButton > button {
|
52 |
+
background-color: #FFD700 !important;
|
53 |
+
color: #1B4D3E !important;
|
54 |
+
font-weight: bold;
|
55 |
+
border-radius: 25px;
|
56 |
+
padding: 10px 25px;
|
57 |
+
border: none;
|
58 |
+
transition: all 0.3s ease;
|
59 |
+
}
|
60 |
+
|
61 |
+
.stButton > button:hover {
|
62 |
+
transform: scale(1.05);
|
63 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
64 |
+
}
|
65 |
+
|
66 |
+
/* Center alignment */
|
67 |
+
.css-10trblm, .css-1a7jz76 {
|
68 |
+
text-align: center !important;
|
69 |
+
}
|
70 |
+
</style>
|
71 |
+
""", unsafe_allow_html=True)
|
72 |
+
|
73 |
+
# Initialize session state for chat history
|
74 |
+
if "messages" not in st.session_state:
|
75 |
+
st.session_state.messages = []
|
76 |
+
st.session_state.messages.append({
|
77 |
+
"role": "assistant",
|
78 |
+
"content": "π Hello! I'm Dr. Radha, your AI-powered Organic Farming Consultant. How can I assist you today?"
|
79 |
+
})
|
80 |
+
|
81 |
+
# Your existing initialization code here
|
82 |
+
PERSISTENT_DIR = "vector_db"
|
83 |
+
# [Keep all your existing functions and variable definitions]
|
84 |
+
|
85 |
|
86 |
# Set persistent storage path
|
87 |
PERSISTENT_DIR = "vector_db"
|
|
|
130 |
start_time = time.time()
|
131 |
st.set_page_config(page_title="Dr. Radha: The Agro-Homeopath", page_icon="π", layout="wide")
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
# Title and subheader
|
134 |
st.title("πΏ Dr. Radha: AI-Powered Organic Farming Consultant")
|
135 |
st.subheader("Specializing in Agro-Homeopathy | Free Consultation")
|
|
|
260 |
fallback_prompt = PromptTemplate(template=fallback_template, input_variables=["question"])
|
261 |
fallback_chain = LLMChain(llm=llm, prompt=fallback_prompt)
|
262 |
|
263 |
+
# Replace your existing chat container and form section with this:
|
264 |
chat_container = st.container()
|
265 |
+
with chat_container:
|
266 |
+
# Display chat history
|
267 |
+
for message in st.session_state.messages:
|
268 |
+
with st.chat_message(message["role"], avatar="π€" if message["role"] == "user" else "πΏ"):
|
269 |
+
st.markdown(message["content"])
|
270 |
|
271 |
with st.form(key='query_form', clear_on_submit=True):
|
272 |
+
query = st.text_input(
|
273 |
+
"Ask your question:",
|
274 |
+
placeholder="Describe your plant issue here...",
|
275 |
+
label_visibility="collapsed"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
)
|
277 |
+
submit_button = st.form_submit_button(label='Send Message π€')
|
|
|
|
|
|
|
278 |
|
279 |
if submit_button and query:
|
280 |
+
# Add user message to history
|
281 |
+
st.session_state.messages.append({"role": "user", "content": query})
|
282 |
+
|
283 |
+
# Show user message
|
284 |
+
with st.chat_message("user", avatar="π€"):
|
285 |
+
st.markdown(query)
|
286 |
+
|
287 |
+
# Show typing indicator while generating response
|
288 |
+
with st.chat_message("assistant", avatar="πΏ"):
|
289 |
+
with st.status("Analyzing your query...", expanded=True):
|
290 |
+
st.write("π Retrieving relevant information...")
|
291 |
+
st.write("π Generating personalized response...")
|
292 |
+
|
293 |
+
# Generate response
|
294 |
+
result = qa({"query": query})
|
295 |
+
if result['result'].strip() == "":
|
296 |
+
response = fallback_chain.run(query)
|
297 |
+
else:
|
298 |
+
response = result['result']
|
299 |
+
|
300 |
+
# Display final response
|
301 |
+
st.markdown(response)
|
302 |
|
303 |
+
# Add assistant response to history
|
304 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
305 |
|
306 |
+
# Clear the form
|
307 |
st.session_state["query"] = ""
|
308 |
+
|
309 |
+
# Rerun to update chat history
|
310 |
+
st.rerun()
|