Pablo276 commited on
Commit
2de98a7
·
verified ·
1 Parent(s): 32deb2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -1
app.py CHANGED
@@ -10,6 +10,7 @@ from langchain_community.document_loaders import PyPDFLoader
10
  from langchain_text_splitters import RecursiveCharacterTextSplitter
11
  import io
12
  import pandas as pd
 
13
 
14
  # Load environment variables from a .env file
15
  load_dotenv()
@@ -56,6 +57,7 @@ def delete_file_from_vectorstore(filename):
56
  return f"Error while deleting the file: {str(e)}", get_files_df()
57
 
58
 
 
59
  def embedder(uploaded_file_path):
60
  """Handles embedding of the uploaded PDF file."""
61
  if uploaded_file_path is None:
@@ -96,7 +98,15 @@ def embedder(uploaded_file_path):
96
  )
97
  documents = text_splitter.split_documents(raw_documents)
98
 
99
- uuids = [f"{original_filename.replace('.pdf', '')}_{i+1}" for i in range(len(documents))]
 
 
 
 
 
 
 
 
100
 
101
  batch_size = 100
102
  for i in range(0, len(documents), batch_size):
@@ -110,6 +120,7 @@ def embedder(uploaded_file_path):
110
  return f"Unable to create embeddings: {str(e)}", get_files_df()
111
 
112
 
 
113
  # --- Gradio Interface Functions ---
114
  def get_files_df():
115
  files = get_stored_files()
 
10
  from langchain_text_splitters import RecursiveCharacterTextSplitter
11
  import io
12
  import pandas as pd
13
+ import re # Make sure to add this import at the top of your file
14
 
15
  # Load environment variables from a .env file
16
  load_dotenv()
 
57
  return f"Error while deleting the file: {str(e)}", get_files_df()
58
 
59
 
60
+
61
  def embedder(uploaded_file_path):
62
  """Handles embedding of the uploaded PDF file."""
63
  if uploaded_file_path is None:
 
98
  )
99
  documents = text_splitter.split_documents(raw_documents)
100
 
101
+ # --- START: CORRECTED CODE ---
102
+ # 1. Sanitize the base filename to conform to Pinecone's rules.
103
+ sanitized_filename = original_filename.replace('.pdf', '').lower()
104
+ # 2. Replace all invalid characters (anything not a-z, 0-9, or -) with a hyphen.
105
+ sanitized_filename = re.sub(r'[^a-z0-9-]', '-', sanitized_filename)
106
+
107
+ # 3. Use the sanitized filename to generate the vector IDs.
108
+ uuids = [f"{sanitized_filename}-{i}" for i in range(len(documents))]
109
+ # --- END: CORRECTED CODE ---
110
 
111
  batch_size = 100
112
  for i in range(0, len(documents), batch_size):
 
120
  return f"Unable to create embeddings: {str(e)}", get_files_df()
121
 
122
 
123
+
124
  # --- Gradio Interface Functions ---
125
  def get_files_df():
126
  files = get_stored_files()