Update app.py
Browse files
app.py
CHANGED
|
@@ -78,21 +78,25 @@ def translate_text(text, dest_language="hi"):
|
|
| 78 |
|
| 79 |
@st.cache_resource
|
| 80 |
def get_vectorstore():
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
def set_custom_prompt(custom_prompt_template):
|
| 98 |
return PromptTemplate(template=custom_prompt_template, input_variables=["context", "question"])
|
|
@@ -144,23 +148,35 @@ def handle_user_input(prompt, qa_chain):
|
|
| 144 |
st.error(f"Error: {str(e)}")
|
| 145 |
|
| 146 |
def handle_translation():
|
| 147 |
-
if "last_response"
|
| 148 |
try:
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
| 162 |
except Exception as e:
|
| 163 |
st.error(f"Translation error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
def main():
|
| 166 |
st.markdown("""
|
|
@@ -297,8 +313,13 @@ def main():
|
|
| 297 |
handle_user_input(prompt, qa_chain)
|
| 298 |
|
| 299 |
if st.session_state.get("last_response"):
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
except Exception as e:
|
| 304 |
st.error(f"Initialization error: {str(e)}")
|
|
|
|
| 78 |
|
| 79 |
@st.cache_resource
|
| 80 |
def get_vectorstore():
|
| 81 |
+
try:
|
| 82 |
+
embedding_model = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 83 |
+
os.makedirs("vectorstore/db_faiss", exist_ok=True)
|
| 84 |
+
|
| 85 |
+
faiss_files = ["index.faiss", "index.pkl"]
|
| 86 |
+
for filename in faiss_files:
|
| 87 |
+
if not os.path.exists(f"vectorstore/db_faiss/{filename}"):
|
| 88 |
+
hf_hub_download(
|
| 89 |
+
repo_id=VECTORSTORE_REPO_ID,
|
| 90 |
+
filename=filename,
|
| 91 |
+
local_dir="vectorstore/db_faiss",
|
| 92 |
+
token=HF_TOKEN,
|
| 93 |
+
repo_type="dataset"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return FAISS.load_local("vectorstore/db_faiss", embedding_model, allow_dangerous_deserialization=True)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
st.error(f"Vectorstore initialization failed: {str(e)}")
|
| 99 |
+
st.stop()
|
| 100 |
|
| 101 |
def set_custom_prompt(custom_prompt_template):
|
| 102 |
return PromptTemplate(template=custom_prompt_template, input_variables=["context", "question"])
|
|
|
|
| 148 |
st.error(f"Error: {str(e)}")
|
| 149 |
|
| 150 |
def handle_translation():
|
| 151 |
+
if st.session_state.get("last_response") and not st.session_state.get("translation_done", False):
|
| 152 |
try:
|
| 153 |
+
translated_text = translate_text(st.session_state.last_response, "hi")
|
| 154 |
+
|
| 155 |
+
# Update all assistant messages (in case of multiple responses)
|
| 156 |
+
for idx, msg in enumerate(st.session_state.messages):
|
| 157 |
+
if msg["role"] == "assistant":
|
| 158 |
+
# Preserve original English text in metadata
|
| 159 |
+
st.session_state.messages[idx] = {
|
| 160 |
+
"role": "assistant",
|
| 161 |
+
"content": f'<div class="hindi-text">{translated_text}</div>',
|
| 162 |
+
"original": st.session_state.messages[idx]["content"]
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
st.session_state.translation_done = True
|
| 166 |
+
st.experimental_rerun() # Force full refresh
|
| 167 |
+
|
| 168 |
except Exception as e:
|
| 169 |
st.error(f"Translation error: {str(e)}")
|
| 170 |
+
st.session_state.translation_done = False
|
| 171 |
+
|
| 172 |
+
def render_chat_messages():
|
| 173 |
+
for message in st.session_state.messages:
|
| 174 |
+
with st.chat_message(message["role"], avatar="🐿" if message["role"] == "user" else "🪈"):
|
| 175 |
+
content = message.get("original", message["content"]) # Show original if available
|
| 176 |
+
if "hindi-text" in message["content"]:
|
| 177 |
+
st.markdown(message["content"], unsafe_allow_html=True)
|
| 178 |
+
else:
|
| 179 |
+
st.markdown(content)
|
| 180 |
|
| 181 |
def main():
|
| 182 |
st.markdown("""
|
|
|
|
| 313 |
handle_user_input(prompt, qa_chain)
|
| 314 |
|
| 315 |
if st.session_state.get("last_response"):
|
| 316 |
+
col1, col2 = st.columns([1, 3])
|
| 317 |
+
with col1:
|
| 318 |
+
if st.button("🌐 Translate to Hindi", key="translate_btn"):
|
| 319 |
+
handle_translation()
|
| 320 |
+
with col2:
|
| 321 |
+
if st.session_state.get("translation_done"):
|
| 322 |
+
st.success("Translation to Hindi completed!")
|
| 323 |
|
| 324 |
except Exception as e:
|
| 325 |
st.error(f"Initialization error: {str(e)}")
|