Update src/streamlit_app.py
Browse files- src/streamlit_app.py +17 -12
src/streamlit_app.py
CHANGED
|
@@ -6,6 +6,13 @@ import re
|
|
| 6 |
import streamlit as st
|
| 7 |
import torch
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ==========================================================
|
| 11 |
# ✅ PAGE CONFIGS
|
|
@@ -221,11 +228,12 @@ else:
|
|
| 221 |
text, toc, toc_source = extract_text_from_pdf(temp_path)
|
| 222 |
chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
|
| 223 |
|
| 224 |
-
#
|
| 225 |
-
doc_sample = " ".join(chunks[:3])[:
|
| 226 |
-
doc_lang =
|
| 227 |
st.session_state["doc_lang"] = doc_lang
|
| 228 |
-
|
|
|
|
| 229 |
|
| 230 |
with st.spinner("⚙️ Building search index..."):
|
| 231 |
embeddings = cache_embeddings(os.path.basename(temp_path), chunks, embed_chunks)
|
|
@@ -236,12 +244,10 @@ else:
|
|
| 236 |
query_suggestions = generate_dynamic_suggestions_from_toc(toc, chunks, doc_name)
|
| 237 |
st.session_state["query_suggestions_fixed"] = query_suggestions
|
| 238 |
st.session_state["last_doc"] = doc_name
|
| 239 |
-
|
| 240 |
-
# 🧹 Reset query when new document is loaded
|
| 241 |
st.session_state["user_query_input"] = ""
|
| 242 |
st.session_state["selected_suggestion"] = None
|
| 243 |
st.session_state["show_more"] = False
|
| 244 |
-
st.
|
| 245 |
else:
|
| 246 |
query_suggestions = st.session_state["query_suggestions_fixed"]
|
| 247 |
|
|
@@ -260,7 +266,7 @@ else:
|
|
| 260 |
toggle_text = "Show less ▲" if st.session_state["show_more"] else "Show more ▼"
|
| 261 |
if st.button(toggle_text, help="Show or hide more suggestions"):
|
| 262 |
st.session_state["show_more"] = not st.session_state["show_more"]
|
| 263 |
-
st.
|
| 264 |
|
| 265 |
user_query = st.text_input("Type your question or click one above:", key="user_query_input")
|
| 266 |
|
|
@@ -271,15 +277,14 @@ else:
|
|
| 271 |
reasoning_mode = mode == "Extended (Document + general)"
|
| 272 |
with st.spinner("💭 Generating your answer..."):
|
| 273 |
retrieved = retrieve_chunks(user_query, index, chunks, top_k=top_k, embeddings=embeddings)
|
| 274 |
-
doc_lang = st.session_state.get("doc_lang", "en")
|
|
|
|
| 275 |
answer = generate_answer(user_query, retrieved, reasoning_mode=reasoning_mode, doc_lang=doc_lang)
|
| 276 |
|
| 277 |
st.markdown("### 🤖 Assistant’s Answer")
|
| 278 |
|
| 279 |
-
# ✅ Apply bullet formatting only for strict factual mode (not reasoning)
|
| 280 |
if not reasoning_mode and not answer.startswith("⚠️"):
|
| 281 |
-
|
| 282 |
-
answer = re.sub(r"\*\*(.*?)\*\*", r"\1", answer) # strip **bold**
|
| 283 |
answer = re.sub(r"(^|\n)-\s*", r"\1<br>• ", answer)
|
| 284 |
st.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
|
| 285 |
|
|
|
|
| 6 |
import streamlit as st
|
| 7 |
import torch
|
| 8 |
|
| 9 |
+
# ==========================================================
|
| 10 |
+
# 🌐 Language Detection Imports
|
| 11 |
+
# ==========================================================
|
| 12 |
+
from fasttext_langdetect import detect_langs
|
| 13 |
+
from langdetect import detect
|
| 14 |
+
|
| 15 |
+
|
| 16 |
|
| 17 |
# ==========================================================
|
| 18 |
# ✅ PAGE CONFIGS
|
|
|
|
| 228 |
text, toc, toc_source = extract_text_from_pdf(temp_path)
|
| 229 |
chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
|
| 230 |
|
| 231 |
+
# 🌐 Detect document language (robust multilingual)
|
| 232 |
+
doc_sample = " ".join(chunks[:3])[:3000]
|
| 233 |
+
doc_lang = detect_document_language(doc_sample)
|
| 234 |
st.session_state["doc_lang"] = doc_lang
|
| 235 |
+
lang_label = "Hindi" if doc_lang.startswith("hi") else "English"
|
| 236 |
+
st.caption(f"🈹 Detected document language: {lang_label}")
|
| 237 |
|
| 238 |
with st.spinner("⚙️ Building search index..."):
|
| 239 |
embeddings = cache_embeddings(os.path.basename(temp_path), chunks, embed_chunks)
|
|
|
|
| 244 |
query_suggestions = generate_dynamic_suggestions_from_toc(toc, chunks, doc_name)
|
| 245 |
st.session_state["query_suggestions_fixed"] = query_suggestions
|
| 246 |
st.session_state["last_doc"] = doc_name
|
|
|
|
|
|
|
| 247 |
st.session_state["user_query_input"] = ""
|
| 248 |
st.session_state["selected_suggestion"] = None
|
| 249 |
st.session_state["show_more"] = False
|
| 250 |
+
st.rerun()
|
| 251 |
else:
|
| 252 |
query_suggestions = st.session_state["query_suggestions_fixed"]
|
| 253 |
|
|
|
|
| 266 |
toggle_text = "Show less ▲" if st.session_state["show_more"] else "Show more ▼"
|
| 267 |
if st.button(toggle_text, help="Show or hide more suggestions"):
|
| 268 |
st.session_state["show_more"] = not st.session_state["show_more"]
|
| 269 |
+
st.rerun()
|
| 270 |
|
| 271 |
user_query = st.text_input("Type your question or click one above:", key="user_query_input")
|
| 272 |
|
|
|
|
| 277 |
reasoning_mode = mode == "Extended (Document + general)"
|
| 278 |
with st.spinner("💭 Generating your answer..."):
|
| 279 |
retrieved = retrieve_chunks(user_query, index, chunks, top_k=top_k, embeddings=embeddings)
|
| 280 |
+
doc_lang = st.session_state.get("doc_lang", "en")
|
| 281 |
+
print("🧠 Document language used for GPT prompt:", doc_lang)
|
| 282 |
answer = generate_answer(user_query, retrieved, reasoning_mode=reasoning_mode, doc_lang=doc_lang)
|
| 283 |
|
| 284 |
st.markdown("### 🤖 Assistant’s Answer")
|
| 285 |
|
|
|
|
| 286 |
if not reasoning_mode and not answer.startswith("⚠️"):
|
| 287 |
+
answer = re.sub(r"\*\*(.*?)\*\*", r"\1", answer)
|
|
|
|
| 288 |
answer = re.sub(r"(^|\n)-\s*", r"\1<br>• ", answer)
|
| 289 |
st.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
|
| 290 |
|