M17idd commited on
Commit
4bf7119
·
verified ·
1 Parent(s): 2b9cbaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -127,33 +127,42 @@ class HuggingFaceEmbeddings(Embeddings):
127
  @st.cache_resource
128
  def get_pdf_index():
129
  with st.spinner('📄 در حال پردازش فایل PDF...'):
130
- # Load the PDF file with chunks
131
  loader = PyPDFLoader('test1.pdf')
132
  pages = loader.load()
133
 
134
- # Instead of loading all pages, process them in batches
135
- batch_size = 5 # Processing 5 pages at a time
136
  all_texts = []
137
 
 
 
 
 
138
  for i in range(0, len(pages), batch_size):
139
  batch = pages[i:i + batch_size]
140
  batch_text = "\n".join([page.page_content for page in batch])
141
  all_texts.append(batch_text)
142
 
143
- # Combine all texts for further processing
 
 
 
 
 
144
  full_text = "\n".join(all_texts)
145
 
146
- # Split the text into chunks
147
  text_splitter = RecursiveCharacterTextSplitter(
148
- chunk_size=1024, # Chunk size
149
- chunk_overlap=128 # Overlap between chunks
150
  )
151
  texts = text_splitter.split_text(full_text)
152
 
153
- # Create embeddings
154
  embeddings = HuggingFaceEmbeddings(model_name="FacebookAI/xlm-roberta-large")
155
 
156
- # Create FAISS vector store
157
  vector_store = FAISS.from_texts(texts, embeddings)
158
 
159
  return vector_store
 
127
  @st.cache_resource
128
  def get_pdf_index():
129
  with st.spinner('📄 در حال پردازش فایل PDF...'):
130
+ # بارگذاری فایل PDF
131
  loader = PyPDFLoader('test1.pdf')
132
  pages = loader.load()
133
 
134
+ # تقسیم صفحات به دسته‌های ۵ تایی
135
+ batch_size = 5 # پردازش ۵ صفحه در هر بار
136
  all_texts = []
137
 
138
+ progress = st.progress(0) # نوار پیشرفت
139
+ total_batches = len(pages) // batch_size + (1 if len(pages) % batch_size != 0 else 0) # تعداد دسته‌ها
140
+
141
+ # پردازش هر دسته
142
  for i in range(0, len(pages), batch_size):
143
  batch = pages[i:i + batch_size]
144
  batch_text = "\n".join([page.page_content for page in batch])
145
  all_texts.append(batch_text)
146
 
147
+ # به‌روزرسانی نوار پیشرفت
148
+ progress.progress((i // batch_size + 1) / total_batches)
149
+
150
+ time.sleep(0.5) # شبیه‌سازی زمان پردازش
151
+
152
+ # ترکیب تمام متن‌ها برای پردازش بیشتر
153
  full_text = "\n".join(all_texts)
154
 
155
+ # تقسیم متن به بخش‌ها
156
  text_splitter = RecursiveCharacterTextSplitter(
157
+ chunk_size=1024, # اندازه هر بخش
158
+ chunk_overlap=128 # هم‌پوشانی بین بخش‌ها
159
  )
160
  texts = text_splitter.split_text(full_text)
161
 
162
+ # ایجاد انتشارات
163
  embeddings = HuggingFaceEmbeddings(model_name="FacebookAI/xlm-roberta-large")
164
 
165
+ # ایجاد FAISS vector store
166
  vector_store = FAISS.from_texts(texts, embeddings)
167
 
168
  return vector_store