samim2024 commited on
Commit
4a31251
·
verified ·
1 Parent(s): 6d72d65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import streamlit as st
3
  import os
4
  from io import BytesIO
@@ -12,10 +11,18 @@ from langchain.chains import RetrievalQA
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:
@@ -25,17 +32,17 @@ 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)
32
- except FileNotFoundError:
33
  st.warning("BSNL logo not found.")
34
 
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 {
@@ -46,6 +53,7 @@ with st.sidebar:
46
  padding: 10px 20px;
47
  border: none;
48
  transition: all 0.3s ease;
 
49
  }
50
  .auth-button button:hover {
51
  background-color: #0056b3 !important;
@@ -57,7 +65,7 @@ with st.sidebar:
57
  with st.container():
58
  st.markdown('<div class="auth-button">', unsafe_allow_html=True)
59
  if st.button("Authenticate"):
60
- if api_key_input == RAG_ACCESS_KEY:
61
  st.session_state.authenticated = True
62
  st.success("Authentication successful!")
63
  else:
@@ -81,7 +89,7 @@ with st.sidebar:
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,32 +122,32 @@ def main():
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)
124
 
125
- status.update(label="Reading PDF file...")
126
  progress_bar.progress(0.2)
127
  pdf_reader = PdfReader(BytesIO(input_data.read()))
128
  documents = "".join([page.extract_text() or "" for page in pdf_reader.pages])
129
 
130
- status.update(label="Splitting text...")
131
  progress_bar.progress(0.4)
132
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
133
  texts = text_splitter.split_text(documents)
134
 
135
- status.update(label="Creating embeddings...")
136
  progress_bar.progress(0.6)
137
  hf_embeddings = HuggingFaceEmbeddings(
138
  model_name="sentence-transformers/all-mpnet-base-v2",
139
  model_kwargs={'device': 'cpu'}
140
  )
141
 
142
- status.update(label="Building vector store...")
143
  progress_bar.progress(0.8)
144
  dimension = len(hf_embeddings.embed_query("test"))
145
  index = faiss.IndexFlatL2(dimension)
@@ -153,24 +161,24 @@ def process_input(input_data):
153
  uuids = [str(uuid.uuid4()) for _ in texts]
154
  vector_store.add_texts(texts, ids=uuids)
155
 
156
- status.update(label="Saving vector store...")
157
  progress_bar.progress(0.9)
158
  vector_store.save_local("vectorstore/faiss_index")
159
 
160
- status.update(label="Done!", state="complete")
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(
@@ -189,6 +197,5 @@ def answer_question(vectorstore, query):
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()
 
 
1
  import streamlit as st
2
  import os
3
  from io import BytesIO
 
11
  from langchain.prompts import PromptTemplate
12
  import faiss
13
  import uuid
14
+ from dotenv import load_dotenv
15
 
16
+ # Load local .env (only useful locally)
17
+ load_dotenv()
18
+
19
+ # Load keys
20
+ RAG_ACCESS_KEY = os.getenv("RAG_ACCESS_KEY")
21
+ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN", "").strip()
22
+
23
+ if not HUGGINGFACEHUB_API_TOKEN:
24
+ st.warning("Hugging Face API token not found in environment variables! "
25
+ "Please set it in your Hugging Face Secrets or your .env file.")
26
 
27
  # Initialize session state
28
  if "vectorstore" not in st.session_state:
 
32
  if "authenticated" not in st.session_state:
33
  st.session_state.authenticated = False
34
 
35
+ # Sidebar with BSNL logo and authentication
36
  with st.sidebar:
37
  try:
38
  st.image("bsnl_logo.png", width=200)
39
+ except Exception:
40
  st.warning("BSNL logo not found.")
41
 
42
  st.header("RAG Control Panel")
43
  api_key_input = st.text_input("Enter RAG Access Key", type="password")
44
 
45
+ # Blue authenticate button style
46
  st.markdown("""
47
  <style>
48
  .auth-button button {
 
53
  padding: 10px 20px;
54
  border: none;
55
  transition: all 0.3s ease;
56
+ width: 100%;
57
  }
58
  .auth-button button:hover {
59
  background-color: #0056b3 !important;
 
65
  with st.container():
66
  st.markdown('<div class="auth-button">', unsafe_allow_html=True)
67
  if st.button("Authenticate"):
68
+ if api_key_input == RAG_ACCESS_KEY and RAG_ACCESS_KEY is not None:
69
  st.session_state.authenticated = True
70
  st.success("Authentication successful!")
71
  else:
 
89
  st.write(f"**A{i+1}:** {a}")
90
  st.markdown("---")
91
 
92
+ # Main app UI
93
  def main():
94
  st.markdown("""
95
  <style>
 
122
  except Exception as e:
123
  st.error(f"Error generating answer: {str(e)}")
124
 
125
+ # PDF processing logic
126
  def process_input(input_data):
127
  os.makedirs("vectorstore", exist_ok=True)
128
  os.chmod("vectorstore", 0o777)
129
 
130
  progress_bar = st.progress(0)
131
+ status = st.empty()
132
 
133
+ status.text("Reading PDF file...")
134
  progress_bar.progress(0.2)
135
  pdf_reader = PdfReader(BytesIO(input_data.read()))
136
  documents = "".join([page.extract_text() or "" for page in pdf_reader.pages])
137
 
138
+ status.text("Splitting text...")
139
  progress_bar.progress(0.4)
140
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
141
  texts = text_splitter.split_text(documents)
142
 
143
+ status.text("Creating embeddings...")
144
  progress_bar.progress(0.6)
145
  hf_embeddings = HuggingFaceEmbeddings(
146
  model_name="sentence-transformers/all-mpnet-base-v2",
147
  model_kwargs={'device': 'cpu'}
148
  )
149
 
150
+ status.text("Building vector store...")
151
  progress_bar.progress(0.8)
152
  dimension = len(hf_embeddings.embed_query("test"))
153
  index = faiss.IndexFlatL2(dimension)
 
161
  uuids = [str(uuid.uuid4()) for _ in texts]
162
  vector_store.add_texts(texts, ids=uuids)
163
 
164
+ status.text("Saving vector store...")
165
  progress_bar.progress(0.9)
166
  vector_store.save_local("vectorstore/faiss_index")
167
 
168
+ status.text("Done!")
169
  progress_bar.progress(1.0)
170
  return vector_store
171
 
172
+ # Question-answering logic
173
  def answer_question(vectorstore, query):
174
+ if not HUGGINGFACEHUB_API_TOKEN:
175
+ raise RuntimeError("Missing Hugging Face API token. Please set it in your secrets.")
176
+
177
+ llm = HuggingFaceHub(
178
+ repo_id="mistralai/Mistral-7B-Instruct-v0.1",
179
+ model_kwargs={"temperature": 0.7, "max_length": 512},
180
+ huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN
181
+ )
182
 
183
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
184
  prompt_template = PromptTemplate(
 
197
  result = qa_chain({"query": query})
198
  return result["result"].split("Answer:")[-1].strip()
199
 
 
200
  if __name__ == "__main__":
201
  main()