samim2024 commited on
Commit
6d72d65
·
verified ·
1 Parent(s): 0f5f3e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -12,13 +12,12 @@ from langchain.chains import RetrievalQA
12
  from langchain.prompts import PromptTemplate
13
  import faiss
14
  import uuid
15
- from dotenv import load_dotenv
16
 
17
- load_dotenv()
18
- HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
19
- RAG_ACCESS_KEY = os.getenv("RAG_ACCESS_KEY")
20
 
21
- # Session state init
22
  if "vectorstore" not in st.session_state:
23
  st.session_state.vectorstore = None
24
  if "history" not in st.session_state:
@@ -26,7 +25,7 @@ if "history" not in st.session_state:
26
  if "authenticated" not in st.session_state:
27
  st.session_state.authenticated = False
28
 
29
- # Sidebar with login
30
  with st.sidebar:
31
  try:
32
  st.image("bsnl_logo.png", width=200)
@@ -36,6 +35,7 @@ with st.sidebar:
36
  st.header("RAG Control Panel")
37
  api_key_input = st.text_input("Enter RAG Access Key", type="password")
38
 
 
39
  st.markdown("""
40
  <style>
41
  .auth-button button {
@@ -81,7 +81,7 @@ with st.sidebar:
81
  st.write(f"**A{i+1}:** {a}")
82
  st.markdown("---")
83
 
84
- # Main UI
85
  def main():
86
  st.markdown("""
87
  <style>
@@ -114,10 +114,10 @@ def main():
114
  except Exception as e:
115
  st.error(f"Error generating answer: {str(e)}")
116
 
117
- # Processing PDF
118
  def process_input(input_data):
119
  os.makedirs("vectorstore", exist_ok=True)
120
- os.chmod("vectorstore", 0o700) # safer permissions
121
 
122
  progress_bar = st.progress(0)
123
  status = st.status("Processing PDF file...", expanded=True)
@@ -161,20 +161,20 @@ def process_input(input_data):
161
  progress_bar.progress(1.0)
162
  return vector_store
163
 
164
- # Q&A logic
165
  def answer_question(vectorstore, query):
166
  try:
167
  llm = HuggingFaceHub(
168
  repo_id="mistralai/Mistral-7B-Instruct-v0.1",
169
- model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
170
  huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
171
  )
172
  except Exception as e:
173
- raise RuntimeError("Failed to load LLM. Ensure your Hugging Face API key is valid and the model is public or accessible.") from e
174
 
175
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
176
  prompt_template = PromptTemplate(
177
- template="Use the context below to answer the question.\n\nContext: {context}\n\nQuestion: {question}\n\nAnswer:",
178
  input_variables=["context", "question"]
179
  )
180
 
@@ -189,5 +189,6 @@ def answer_question(vectorstore, query):
189
  result = qa_chain({"query": query})
190
  return result["result"].split("Answer:")[-1].strip()
191
 
 
192
  if __name__ == "__main__":
193
  main()
 
12
  from langchain.prompts import PromptTemplate
13
  import faiss
14
  import uuid
 
15
 
16
+ # Load secrets from Streamlit
17
+ HUGGINGFACEHUB_API_TOKEN = st.secrets["HUGGINGFACEHUB_API_TOKEN"]
18
+ RAG_ACCESS_KEY = st.secrets["RAG_ACCESS_KEY"]
19
 
20
+ # Initialize session state
21
  if "vectorstore" not in st.session_state:
22
  st.session_state.vectorstore = None
23
  if "history" not in st.session_state:
 
25
  if "authenticated" not in st.session_state:
26
  st.session_state.authenticated = False
27
 
28
+ # Sidebar with logo and authentication
29
  with st.sidebar:
30
  try:
31
  st.image("bsnl_logo.png", width=200)
 
35
  st.header("RAG Control Panel")
36
  api_key_input = st.text_input("Enter RAG Access Key", type="password")
37
 
38
+ # Custom styled Authenticate button
39
  st.markdown("""
40
  <style>
41
  .auth-button button {
 
81
  st.write(f"**A{i+1}:** {a}")
82
  st.markdown("---")
83
 
84
+ # Main app interface
85
  def main():
86
  st.markdown("""
87
  <style>
 
114
  except Exception as e:
115
  st.error(f"Error generating answer: {str(e)}")
116
 
117
+ # Process PDF and build vector store
118
  def process_input(input_data):
119
  os.makedirs("vectorstore", exist_ok=True)
120
+ os.chmod("vectorstore", 0o777)
121
 
122
  progress_bar = st.progress(0)
123
  status = st.status("Processing PDF file...", expanded=True)
 
161
  progress_bar.progress(1.0)
162
  return vector_store
163
 
164
+ # Answer the user's query
165
  def answer_question(vectorstore, query):
166
  try:
167
  llm = HuggingFaceHub(
168
  repo_id="mistralai/Mistral-7B-Instruct-v0.1",
169
+ model_kwargs={"temperature": 0.7, "max_length": 512},
170
  huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
171
  )
172
  except Exception as e:
173
+ raise RuntimeError("Failed to load LLM. Check Hugging Face API key and access rights.") from e
174
 
175
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
176
  prompt_template = PromptTemplate(
177
+ template="Use the context to answer the question concisely:\n\nContext: {context}\n\nQuestion: {question}\n\nAnswer:",
178
  input_variables=["context", "question"]
179
  )
180
 
 
189
  result = qa_chain({"query": query})
190
  return result["result"].split("Answer:")[-1].strip()
191
 
192
+ # Run the app
193
  if __name__ == "__main__":
194
  main()