Abrar20 commited on
Commit
9ce8b35
Β·
verified Β·
1 Parent(s): ad69146

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -36,13 +36,18 @@ if not GOOGLE_API_KEY:
36
  # Configure the API key
37
  genai.configure(api_key=GOOGLE_API_KEY)
38
 
39
- def get_pdf_text(pdf_docs):
40
- """Extract text from uploaded PDF documents."""
41
  text = ""
42
- for pdf in pdf_docs:
43
- pdf_reader = PdfReader(pdf)
44
- for page in pdf_reader.pages:
45
- text += page.extract_text()
 
 
 
 
 
46
  return text
47
 
48
  def get_text_chunks(text):
@@ -119,7 +124,10 @@ def main():
119
  st.set_page_config(page_title="Robi AI Customer Service", page_icon="πŸ“±")
120
 
121
  # Display Logo
122
- st.image("robi_logo.png", width=200) # Ensure you have a 'robi_logo.png' in your project directory
 
 
 
123
 
124
  # Title and Description
125
  st.title("πŸ“Ά Robi AI Customer Service & Recommendation System")
@@ -197,16 +205,20 @@ def main():
197
  st.header("πŸ“„ Documents:")
198
  # Check if the FAISS index exists
199
  if not os.path.exists("faiss_index/index.faiss"):
200
- with st.spinner("Processing documents..."):
201
- # Process the provided PDFs
202
- uploaded_files = st.file_uploader("Upload Robi Information PDF", type=["pdf"], accept_multiple_files=True)
203
- if uploaded_files:
204
- raw_text = get_pdf_text(uploaded_files)
205
- text_chunks = get_text_chunks(raw_text)
206
- get_vector_store(text_chunks)
207
- st.success("Documents processed and FAISS index created.")
 
 
 
 
208
  else:
209
  st.info("FAISS index loaded from 'faiss_index'.")
210
 
211
  if __name__ == "__main__":
212
- main()
 
36
  # Configure the API key
37
  genai.configure(api_key=GOOGLE_API_KEY)
38
 
39
+ def get_pdf_text(pdf_path):
40
+ """Extract text from a PDF document."""
41
  text = ""
42
+ try:
43
+ with open(pdf_path, "rb") as pdf_file:
44
+ pdf_reader = PdfReader(pdf_file)
45
+ for page in pdf_reader.pages:
46
+ extracted_text = page.extract_text()
47
+ if extracted_text:
48
+ text += extracted_text
49
+ except Exception as e:
50
+ st.error(f"Error reading PDF file: {e}")
51
  return text
52
 
53
  def get_text_chunks(text):
 
124
  st.set_page_config(page_title="Robi AI Customer Service", page_icon="πŸ“±")
125
 
126
  # Display Logo
127
+ if os.path.exists("robi_logo.png"):
128
+ st.image("robi_logo.png", width=200) # Ensure you have a 'robi_logo.png' in your project directory
129
+ else:
130
+ st.warning("Logo image 'robi_logo.png' not found. Please add it to the project directory.")
131
 
132
  # Title and Description
133
  st.title("πŸ“Ά Robi AI Customer Service & Recommendation System")
 
205
  st.header("πŸ“„ Documents:")
206
  # Check if the FAISS index exists
207
  if not os.path.exists("faiss_index/index.faiss"):
208
+ pdf_path = "customerservicebot.pdf" # Default PDF file
209
+ if os.path.exists(pdf_path):
210
+ with st.spinner(f"Processing '{pdf_path}'..."):
211
+ raw_text = get_pdf_text(pdf_path)
212
+ if raw_text:
213
+ text_chunks = get_text_chunks(raw_text)
214
+ get_vector_store(text_chunks)
215
+ st.success(f"'{pdf_path}' processed and FAISS index created.")
216
+ else:
217
+ st.error(f"No text extracted from '{pdf_path}'. Please check the PDF content.")
218
+ else:
219
+ st.error(f"Default PDF '{pdf_path}' not found. Please add it to the project directory.")
220
  else:
221
  st.info("FAISS index loaded from 'faiss_index'.")
222
 
223
  if __name__ == "__main__":
224
+ main()