Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,243 +1,316 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from langchain.memory import ConversationBufferMemory
|
3 |
-
from llama_index.core.indices.query.schema import QueryBundle
|
4 |
-
from llama_index.core import Document, VectorStoreIndex
|
5 |
-
from llama_index.core.text_splitter import SentenceSplitter
|
6 |
-
from llama_index.core.retrievers import QueryFusionRetriever
|
7 |
-
from llama_index.retrievers.bm25 import BM25Retriever
|
8 |
-
from llama_index.core.postprocessor import SentenceTransformerRerank
|
9 |
-
from llama_index.core.prompts import PromptTemplate
|
10 |
-
from llama_index.core.query_engine import RetrieverQueryEngine
|
11 |
-
from llama_index.embeddings.gemini import GeminiEmbedding
|
12 |
-
from llama_index.llms.gemini import Gemini
|
13 |
-
from llama_index.core import Settings
|
14 |
-
from llama_index.vector_stores.faiss import FaissVectorStore
|
15 |
-
from llama_index.core import (
|
16 |
-
SimpleDirectoryReader,
|
17 |
-
load_index_from_storage,
|
18 |
-
VectorStoreIndex,
|
19 |
-
StorageContext,
|
20 |
-
)
|
21 |
-
from llama_index.core.node_parser import SemanticSplitterNodeParser
|
22 |
-
|
23 |
-
import os
|
24 |
-
import faiss
|
25 |
-
import pickle
|
26 |
-
import spacy
|
27 |
-
|
28 |
-
# Load NLP model
|
29 |
-
# nlp = spacy.load("en_core_web_sm")
|
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 |
-
vector_retriever
|
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 |
-
st.
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
color: #
|
137 |
-
}
|
138 |
-
.
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
color: #
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
margin:
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
margin:
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
background-color: #2a3942;
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
border:
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
background-color: #
|
193 |
-
|
194 |
-
|
195 |
-
display:
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.memory import ConversationBufferMemory
|
3 |
+
from llama_index.core.indices.query.schema import QueryBundle
|
4 |
+
from llama_index.core import Document, VectorStoreIndex
|
5 |
+
from llama_index.core.text_splitter import SentenceSplitter
|
6 |
+
from llama_index.core.retrievers import QueryFusionRetriever
|
7 |
+
from llama_index.retrievers.bm25 import BM25Retriever
|
8 |
+
from llama_index.core.postprocessor import SentenceTransformerRerank
|
9 |
+
from llama_index.core.prompts import PromptTemplate
|
10 |
+
from llama_index.core.query_engine import RetrieverQueryEngine
|
11 |
+
from llama_index.embeddings.gemini import GeminiEmbedding
|
12 |
+
from llama_index.llms.gemini import Gemini
|
13 |
+
from llama_index.core import Settings
|
14 |
+
from llama_index.vector_stores.faiss import FaissVectorStore
|
15 |
+
from llama_index.core import (
|
16 |
+
SimpleDirectoryReader,
|
17 |
+
load_index_from_storage,
|
18 |
+
VectorStoreIndex,
|
19 |
+
StorageContext,
|
20 |
+
)
|
21 |
+
from llama_index.core.node_parser import SemanticSplitterNodeParser
|
22 |
+
|
23 |
+
import os
|
24 |
+
import faiss
|
25 |
+
import pickle
|
26 |
+
import spacy
|
27 |
+
|
28 |
+
# Load NLP model
|
29 |
+
# nlp = spacy.load("en_core_web_sm")
|
30 |
+
|
31 |
+
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
32 |
+
|
33 |
+
# Function to load documents
|
34 |
+
def load_documents(filename="documents.pkl"):
|
35 |
+
with open(filename, "rb") as file:
|
36 |
+
return pickle.load(file)
|
37 |
+
|
38 |
+
# Load stored documents
|
39 |
+
loaded_docs = load_documents()
|
40 |
+
|
41 |
+
# Function to split text into sentences
|
42 |
+
# def spacy_sentence_splitter(text):
|
43 |
+
# doc = nlp(text)
|
44 |
+
# return [sent.text for sent in doc.sents]
|
45 |
+
embed_model = GeminiEmbedding(model_name="models/embedding-001", use_async=False)
|
46 |
+
splitter = SemanticSplitterNodeParser(
|
47 |
+
buffer_size=5, breakpoint_percentile_threshold=95, embed_model=embed_model
|
48 |
+
)
|
49 |
+
# splitter = SentenceSplitter(chunk_size=512, chunk_overlap=50, separator="\n")
|
50 |
+
nodes = splitter.get_nodes_from_documents([doc for doc in loaded_docs])
|
51 |
+
chunked_documents = [Document(text=node.text, metadata=node.metadata) for node in nodes]
|
52 |
+
# Process documents
|
53 |
+
# chunked_documents = [
|
54 |
+
# Document(text=chunk_text, metadata=doc.metadata)
|
55 |
+
# for doc in loaded_docs for chunk_text in spacy_sentence_splitter(doc.text)
|
56 |
+
# ]
|
57 |
+
|
58 |
+
# Configure LLM and embeddings
|
59 |
+
Settings.llm = Gemini(model="models/gemini-2.0-flash", api_key=GOOGLE_API_KEY, temperature=0.5)
|
60 |
+
|
61 |
+
dimension = 768
|
62 |
+
faiss_index = faiss.IndexFlatL2(dimension)
|
63 |
+
vector_store = FaissVectorStore(faiss_index=faiss_index)
|
64 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
65 |
+
|
66 |
+
# Build index
|
67 |
+
index = VectorStoreIndex.from_documents(
|
68 |
+
documents=chunked_documents,
|
69 |
+
storage_context=storage_context,
|
70 |
+
embed_model=embed_model,
|
71 |
+
show_progress=True
|
72 |
+
)
|
73 |
+
index.storage_context.persist()
|
74 |
+
|
75 |
+
# Initialize memory
|
76 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
77 |
+
|
78 |
+
def get_chat_history():
|
79 |
+
return memory.load_memory_variables({})["chat_history"]
|
80 |
+
|
81 |
+
# Define chatbot prompt template
|
82 |
+
prompt_template = PromptTemplate(
|
83 |
+
"""You are a friendly college counselor with expertise in Indian technical institutes.
|
84 |
+
Previous conversation context (if any):\n{chat_history}\n\n
|
85 |
+
Available college information:\n{context_str}\n\n"
|
86 |
+
User query: {query_str}\n\n
|
87 |
+
Instructions:\n
|
88 |
+
1. Provide a brief, direct answer using only the information available above\n
|
89 |
+
2. If specific data is not available, clearly state that\n
|
90 |
+
3. Keep responses under 3 sentences when possible\n
|
91 |
+
4. If comparing colleges, use bullet points for clarity\n
|
92 |
+
5. Use a friendly, conversational tone\n
|
93 |
+
6. Always be interactive and ask follow-up questions\n
|
94 |
+
7. Always try to give answers in points each point should focus on single aspect of the response.\n
|
95 |
+
8. Always try to give conclusion of your answer in the end for the user to take a decision.\n
|
96 |
+
Response:"""
|
97 |
+
)
|
98 |
+
|
99 |
+
# Configure retrieval and query engine
|
100 |
+
vector_retriever = index.as_retriever(similarity_top_k=10)
|
101 |
+
bm25_retriever = BM25Retriever.from_defaults(index=index, similarity_top_k=10)
|
102 |
+
hybrid_retriever = QueryFusionRetriever(
|
103 |
+
[vector_retriever, bm25_retriever],
|
104 |
+
similarity_top_k=10,
|
105 |
+
num_queries=10,
|
106 |
+
mode="reciprocal_rerank",
|
107 |
+
use_async=False
|
108 |
+
)
|
109 |
+
|
110 |
+
reranker = SentenceTransformerRerank(
|
111 |
+
model="cross-encoder/ms-marco-MiniLM-L-2-v2",
|
112 |
+
top_n=10,
|
113 |
+
)
|
114 |
+
|
115 |
+
query_engine = RetrieverQueryEngine.from_args(
|
116 |
+
retriever=hybrid_retriever,
|
117 |
+
node_postprocessors=[reranker],
|
118 |
+
llm=Settings.llm,
|
119 |
+
verbose=True,
|
120 |
+
prompt_template=prompt_template,
|
121 |
+
use_async=False,
|
122 |
+
)
|
123 |
+
|
124 |
+
# Streamlit UI
|
125 |
+
st.title("📚 Precollege Chatbot")
|
126 |
+
st.write("Ask me anything about different colleges and their courses!")
|
127 |
+
|
128 |
+
# Custom CSS for WhatsApp-like interface
|
129 |
+
st.markdown("""
|
130 |
+
<style>
|
131 |
+
body {
|
132 |
+
background-color: #111b21;
|
133 |
+
color: #e9edef;
|
134 |
+
}
|
135 |
+
.stApp {
|
136 |
+
background-color: #111b21;
|
137 |
+
}
|
138 |
+
.chat-container {
|
139 |
+
padding: 10px;
|
140 |
+
color: #111b21;
|
141 |
+
}
|
142 |
+
.user-message {
|
143 |
+
background-color: #005c4b;
|
144 |
+
color: #e9edef;
|
145 |
+
padding: 10px 15px;
|
146 |
+
border-radius: 15px;
|
147 |
+
margin: 5px 0;
|
148 |
+
max-width: 70%;
|
149 |
+
margin-left: auto;
|
150 |
+
margin-right: 10px;
|
151 |
+
}
|
152 |
+
.ai-message {
|
153 |
+
background-color: #1f2c33;
|
154 |
+
color: #e9edef;
|
155 |
+
padding: 10px 15px;
|
156 |
+
border-radius: 15px;
|
157 |
+
margin: 5px 0;
|
158 |
+
max-width: 70%;
|
159 |
+
margin-right: auto;
|
160 |
+
margin-left: 10px;
|
161 |
+
box-shadow: 0 1px 2px rgba(255,255,255,0.1);
|
162 |
+
}
|
163 |
+
.ai-message table {
|
164 |
+
border-collapse: collapse;
|
165 |
+
width: 100%;
|
166 |
+
margin: 10px 0;
|
167 |
+
}
|
168 |
+
.ai-message th, .ai-message td {
|
169 |
+
border: 1px solid #e9edef;
|
170 |
+
padding: 8px;
|
171 |
+
text-align: left;
|
172 |
+
}
|
173 |
+
.ai-message th {
|
174 |
+
background-color: #2a3942;
|
175 |
+
}
|
176 |
+
.message-container {
|
177 |
+
display: flex;
|
178 |
+
margin-bottom: 10px;
|
179 |
+
}
|
180 |
+
.stTextInput input {
|
181 |
+
border-radius: 20px;
|
182 |
+
padding: 10px 20px;
|
183 |
+
border: 1px solid #ccc;
|
184 |
+
background-color: #2a3942;
|
185 |
+
color: #e9edef;
|
186 |
+
}
|
187 |
+
.stButton button {
|
188 |
+
border-radius: 50%; /* Make it circular */
|
189 |
+
width: 40px;
|
190 |
+
height: 40px;
|
191 |
+
padding: 0px;
|
192 |
+
background-color: #005c4b;
|
193 |
+
color: #e9edef;
|
194 |
+
font-size: 20px;
|
195 |
+
display: flex;
|
196 |
+
align-items: center;
|
197 |
+
justify-content: center;
|
198 |
+
border: none;
|
199 |
+
cursor: pointer;
|
200 |
+
}
|
201 |
+
.stButton button:hover {
|
202 |
+
background-color: #00735e;
|
203 |
+
}
|
204 |
+
div[data-testid="stToolbar"] {
|
205 |
+
display: none;
|
206 |
+
}
|
207 |
+
.stMarkdown {
|
208 |
+
color: #e9edef;
|
209 |
+
}
|
210 |
+
header {
|
211 |
+
background-color: #202c33 !important;
|
212 |
+
}
|
213 |
+
.ai-message table.ai-table {
|
214 |
+
border-collapse: collapse;
|
215 |
+
width: 100%;
|
216 |
+
margin: 10px 0;
|
217 |
+
background-color: #2a3942;
|
218 |
+
}
|
219 |
+
|
220 |
+
.ai-message table.ai-table th,
|
221 |
+
.ai-message table.ai-table td {
|
222 |
+
border: 1px solid #e9edef;
|
223 |
+
padding: 8px;
|
224 |
+
text-align: left;
|
225 |
+
color: #e9edef;
|
226 |
+
}
|
227 |
+
|
228 |
+
.ai-message table.ai-table th {
|
229 |
+
background-color: #005c4b;
|
230 |
+
font-weight: bold;
|
231 |
+
}
|
232 |
+
|
233 |
+
.ai-message table.ai-table tr:nth-child(even) {
|
234 |
+
background-color: #1f2c33;
|
235 |
+
}
|
236 |
+
</style>
|
237 |
+
""", unsafe_allow_html=True)
|
238 |
+
|
239 |
+
if "chat_history" not in st.session_state:
|
240 |
+
st.session_state.chat_history = []
|
241 |
+
|
242 |
+
# Create a container for chat messages
|
243 |
+
chat_container = st.container()
|
244 |
+
|
245 |
+
# Create a form for input
|
246 |
+
with st.form(key="message_form", clear_on_submit=True):
|
247 |
+
col1, col2 = st.columns([5,1])
|
248 |
+
with col1:
|
249 |
+
user_input = st.text_input("", placeholder="Type a message...", label_visibility="collapsed")
|
250 |
+
with col2:
|
251 |
+
submit_button = st.form_submit_button("➤")
|
252 |
+
|
253 |
+
if submit_button and user_input.strip():
|
254 |
+
chat_history = get_chat_history()
|
255 |
+
query_bundle = QueryBundle(query_str=f"{chat_history}\n\nUser: {user_input}")
|
256 |
+
response_obj = query_engine.query(query_bundle)
|
257 |
+
response_text = str(response_obj.response) if hasattr(response_obj, "response") else str(response_obj)
|
258 |
+
|
259 |
+
memory.save_context({"query_str": user_input}, {"response": response_text})
|
260 |
+
st.session_state.chat_history.append(("You", user_input))
|
261 |
+
st.session_state.chat_history.append(("AI", response_text))
|
262 |
+
|
263 |
+
# Display chat history with custom styling
|
264 |
+
with chat_container:
|
265 |
+
for role, message in st.session_state.chat_history:
|
266 |
+
message = message.replace("</div>", "").replace("<div>", "") # Sanitize the message
|
267 |
+
if role == "You":
|
268 |
+
st.markdown(
|
269 |
+
f'<div class="message-container"><div class="user-message">{message}</div></div>',
|
270 |
+
unsafe_allow_html=True
|
271 |
+
)
|
272 |
+
else:
|
273 |
+
# Convert markdown tables to HTML tables with proper styling
|
274 |
+
if "|" in message and "-|-" in message: # Detect markdown tables
|
275 |
+
# Split the message into lines
|
276 |
+
lines = message.split("\n")
|
277 |
+
table_html = []
|
278 |
+
in_table = False
|
279 |
+
formatted_lines = []
|
280 |
+
|
281 |
+
for line in lines:
|
282 |
+
if "|" in line:
|
283 |
+
if not in_table:
|
284 |
+
in_table = True
|
285 |
+
table_html.append('<table class="ai-table">')
|
286 |
+
# Add header
|
287 |
+
header = line.strip().strip("|").split("|")
|
288 |
+
table_html.append("<tr>")
|
289 |
+
for h in header:
|
290 |
+
table_html.append(f"<th>{h.strip()}</th>")
|
291 |
+
table_html.append("</tr>")
|
292 |
+
elif "-|-" not in line: # Skip separator line
|
293 |
+
# Add row
|
294 |
+
row = line.strip().strip("|").split("|")
|
295 |
+
table_html.append("<tr>")
|
296 |
+
for cell in row:
|
297 |
+
table_html.append(f"<td>{cell.strip()}</td>")
|
298 |
+
table_html.append("</tr>")
|
299 |
+
else:
|
300 |
+
if in_table:
|
301 |
+
in_table = False
|
302 |
+
table_html.append("</table>")
|
303 |
+
formatted_lines.append("".join(table_html))
|
304 |
+
table_html = []
|
305 |
+
formatted_lines.append(line)
|
306 |
+
|
307 |
+
if in_table:
|
308 |
+
table_html.append("</table>")
|
309 |
+
formatted_lines.append("".join(table_html))
|
310 |
+
|
311 |
+
message = "\n".join(formatted_lines)
|
312 |
+
|
313 |
+
st.markdown(
|
314 |
+
f'<div class="message-container"><div class="ai-message">{message}</div></div>',
|
315 |
+
unsafe_allow_html=True
|
316 |
+
)
|