Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from langchain.embeddings import HuggingFaceEmbeddings
|
|
| 4 |
from langchain.chains import RetrievalQA
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_core.prompts import PromptTemplate
|
| 7 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
| 8 |
import time
|
| 9 |
import translators as ts
|
| 10 |
from huggingface_hub import hf_hub_download
|
|
@@ -13,9 +13,9 @@ from huggingface_hub import hf_hub_download
|
|
| 13 |
st.set_page_config(layout="wide")
|
| 14 |
|
| 15 |
# Configuration
|
| 16 |
-
HF_TOKEN = os.getenv("HF_TOKEN") #
|
| 17 |
-
VECTORSTORE_REPO_ID = "vashu2425/bhagavad-geeta-faiss-vectordb"
|
| 18 |
-
MODEL_REPO_ID = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 19 |
|
| 20 |
# Constants
|
| 21 |
CUSTOM_PROMPT_TEMPLATE = """
|
|
@@ -30,6 +30,25 @@ Start The Answer Directly. No Small Talk, Please. The Answer Should Contain All
|
|
| 30 |
Consider Yourself As God Krishna And Answer The Question
|
| 31 |
"""
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def translate_text(text, dest_language="hi"):
|
| 34 |
try:
|
| 35 |
return ts.google(text, to_language=dest_language)
|
|
@@ -41,13 +60,10 @@ def translate_text(text, dest_language="hi"):
|
|
| 41 |
def get_vectorstore():
|
| 42 |
embedding_model = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 43 |
|
| 44 |
-
# Create directory structure
|
| 45 |
os.makedirs("vectorstore/db_faiss", exist_ok=True)
|
| 46 |
|
| 47 |
-
# List of required FAISS files
|
| 48 |
faiss_files = ["index.faiss", "index.pkl"]
|
| 49 |
|
| 50 |
-
# Download files from Hugging Face Hub
|
| 51 |
for filename in faiss_files:
|
| 52 |
if not os.path.exists(f"vectorstore/db_faiss/{filename}"):
|
| 53 |
try:
|
|
@@ -71,11 +87,85 @@ def load_llm(huggingface_repo_id, hf_token):
|
|
| 71 |
return HuggingFaceEndpoint(
|
| 72 |
repo_id=huggingface_repo_id,
|
| 73 |
temperature=0.5,
|
| 74 |
-
huggingfacehub_api_token=hf_token,
|
| 75 |
-
model_kwargs={"max_length": 512}
|
| 76 |
)
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def main():
|
| 81 |
st.markdown("""
|
|
|
|
| 4 |
from langchain.chains import RetrievalQA
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_core.prompts import PromptTemplate
|
| 7 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
| 8 |
import time
|
| 9 |
import translators as ts
|
| 10 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 13 |
st.set_page_config(layout="wide")
|
| 14 |
|
| 15 |
# Configuration
|
| 16 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # First try to get from Spaces secrets
|
| 17 |
+
VECTORSTORE_REPO_ID = "vashu2425/bhagavad-geeta-faiss-vectordb"
|
| 18 |
+
MODEL_REPO_ID = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 19 |
|
| 20 |
# Constants
|
| 21 |
CUSTOM_PROMPT_TEMPLATE = """
|
|
|
|
| 30 |
Consider Yourself As God Krishna And Answer The Question
|
| 31 |
"""
|
| 32 |
|
| 33 |
+
# Add manual token input if not found in environment
|
| 34 |
+
if not HF_TOKEN:
|
| 35 |
+
HF_TOKEN = st.sidebar.text_input("Enter Hugging Face Token (hf_...)", type="password")
|
| 36 |
+
if not HF_TOKEN:
|
| 37 |
+
st.warning("Please enter your Hugging Face token to continue")
|
| 38 |
+
st.stop()
|
| 39 |
+
|
| 40 |
+
def initialize_session_states():
|
| 41 |
+
session_defaults = {
|
| 42 |
+
"messages": [],
|
| 43 |
+
"selected_question": None,
|
| 44 |
+
"show_predefined": True,
|
| 45 |
+
"last_response": None,
|
| 46 |
+
"translation_done": False
|
| 47 |
+
}
|
| 48 |
+
for key, val in session_defaults.items():
|
| 49 |
+
if key not in st.session_state:
|
| 50 |
+
st.session_state[key] = val
|
| 51 |
+
|
| 52 |
def translate_text(text, dest_language="hi"):
|
| 53 |
try:
|
| 54 |
return ts.google(text, to_language=dest_language)
|
|
|
|
| 60 |
def get_vectorstore():
|
| 61 |
embedding_model = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 62 |
|
|
|
|
| 63 |
os.makedirs("vectorstore/db_faiss", exist_ok=True)
|
| 64 |
|
|
|
|
| 65 |
faiss_files = ["index.faiss", "index.pkl"]
|
| 66 |
|
|
|
|
| 67 |
for filename in faiss_files:
|
| 68 |
if not os.path.exists(f"vectorstore/db_faiss/{filename}"):
|
| 69 |
try:
|
|
|
|
| 87 |
return HuggingFaceEndpoint(
|
| 88 |
repo_id=huggingface_repo_id,
|
| 89 |
temperature=0.5,
|
| 90 |
+
huggingfacehub_api_token=hf_token,
|
| 91 |
+
model_kwargs={"max_length": 512}
|
| 92 |
)
|
| 93 |
|
| 94 |
+
def format_source_docs(source_documents):
|
| 95 |
+
formatted_docs = []
|
| 96 |
+
for idx, doc in enumerate(source_documents, start=1):
|
| 97 |
+
content = doc.page_content.replace('\t', ' ').replace('\n', ' ').strip()
|
| 98 |
+
formatted_doc = f"**Source {idx}** (Page {doc.metadata['page']}):\n\n{content[:500]}..."
|
| 99 |
+
formatted_docs.append(formatted_doc)
|
| 100 |
+
return "\n\n".join(formatted_docs)
|
| 101 |
+
|
| 102 |
+
def render_chat_messages():
|
| 103 |
+
for message in st.session_state.messages:
|
| 104 |
+
with st.chat_message(message["role"], avatar="🐿" if message["role"] == "user" else "🪈"):
|
| 105 |
+
content = message["content"]
|
| 106 |
+
if "hindi-text" in content:
|
| 107 |
+
st.markdown(content, unsafe_allow_html=True)
|
| 108 |
+
else:
|
| 109 |
+
st.markdown(content)
|
| 110 |
+
|
| 111 |
+
def render_predefined_questions():
|
| 112 |
+
predefined_questions = [
|
| 113 |
+
"Meaning of Dharma?",
|
| 114 |
+
"What is the purpose of life?",
|
| 115 |
+
"How to find inner peace?",
|
| 116 |
+
"How can I be a better person?",
|
| 117 |
+
"What is the meaning of life?",
|
| 118 |
+
"How can I be a better friend?"
|
| 119 |
+
]
|
| 120 |
+
st.markdown("### Or, try one of these:")
|
| 121 |
+
buttons = st.columns(len(predefined_questions))
|
| 122 |
+
for idx, question in enumerate(predefined_questions):
|
| 123 |
+
if buttons[idx].button(question, key=f"predefined_{idx}"):
|
| 124 |
+
st.session_state.selected_question = question
|
| 125 |
+
st.session_state.show_predefined = False
|
| 126 |
+
|
| 127 |
+
def handle_user_input(prompt, qa_chain):
|
| 128 |
+
if prompt:
|
| 129 |
+
with st.chat_message("user", avatar="🐿"):
|
| 130 |
+
st.markdown(prompt)
|
| 131 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 132 |
+
|
| 133 |
+
try:
|
| 134 |
+
response = qa_chain.invoke({"query": prompt})
|
| 135 |
+
result = response["result"]
|
| 136 |
+
source_documents = response["source_documents"]
|
| 137 |
+
|
| 138 |
+
response_placeholder = st.empty()
|
| 139 |
+
accumulated_text = ""
|
| 140 |
+
for char in result:
|
| 141 |
+
accumulated_text += char
|
| 142 |
+
response_placeholder.markdown(f'<div class="english-text">{accumulated_text}</div>', unsafe_allow_html=True)
|
| 143 |
+
time.sleep(0.01)
|
| 144 |
+
|
| 145 |
+
if "don't have information" not in result.lower():
|
| 146 |
+
with st.expander("Source Documents"):
|
| 147 |
+
st.markdown(format_source_docs(source_documents))
|
| 148 |
+
|
| 149 |
+
st.session_state.messages.append({"role": "assistant", "content": accumulated_text})
|
| 150 |
+
st.session_state.last_response = accumulated_text
|
| 151 |
+
st.session_state.show_predefined = False
|
| 152 |
+
st.session_state.translation_done = False
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
st.error(f"Error: {str(e)}")
|
| 156 |
+
|
| 157 |
+
def handle_translation():
|
| 158 |
+
if st.session_state.last_response and not st.session_state.translation_done:
|
| 159 |
+
try:
|
| 160 |
+
translated_text = translate_text(st.session_state.last_response, "hi")
|
| 161 |
+
for msg in reversed(st.session_state.messages):
|
| 162 |
+
if msg["role"] == "assistant":
|
| 163 |
+
msg["content"] = f'<div class="hindi-text">{translated_text}</div>'
|
| 164 |
+
break
|
| 165 |
+
st.session_state.translation_done = True
|
| 166 |
+
st.rerun()
|
| 167 |
+
except Exception as e:
|
| 168 |
+
st.error(f"Translation error: {str(e)}")
|
| 169 |
|
| 170 |
def main():
|
| 171 |
st.markdown("""
|