openfree commited on
Commit
206f5ac
ยท
verified ยท
1 Parent(s): 4ac1625

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -203,6 +203,8 @@ class PDFRAGSystem:
203
  """๊ด€๋ จ ์ฒญํฌ ๊ฒ€์ƒ‰"""
204
  all_relevant_chunks = []
205
 
 
 
206
  if self.embedder and self.embeddings_store:
207
  # ์ž„๋ฒ ๋”ฉ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰
208
  query_embedding = self.embedder.encode([query])[0]
@@ -218,58 +220,68 @@ class PDFRAGSystem:
218
  sim = np.dot(query_embedding, emb) / (np.linalg.norm(query_embedding) * np.linalg.norm(emb))
219
  similarities.append(sim)
220
 
221
- # ์ƒ์œ„ ์ฒญํฌ ์„ ํƒ
222
  top_indices = np.argsort(similarities)[-top_k:][::-1]
223
 
224
  for idx in top_indices:
225
- if similarities[idx] > 0.2:
226
  all_relevant_chunks.append({
227
  "content": chunks[idx],
228
  "doc_name": self.documents[doc_id]["metadata"]["file_name"],
229
  "similarity": similarities[idx]
230
  })
 
231
  else:
232
  # ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰
 
233
  query_keywords = set(query.lower().split())
234
 
235
  for doc_id in doc_ids:
236
  if doc_id in self.document_chunks:
237
  chunks = self.document_chunks[doc_id]
238
- for i, chunk in enumerate(chunks[:5]): # ์ฒ˜์Œ 5๊ฐœ๋งŒ
239
  chunk_lower = chunk.lower()
240
  score = sum(1 for keyword in query_keywords if keyword in chunk_lower)
241
  if score > 0:
242
  all_relevant_chunks.append({
243
- "content": chunk[:500],
244
  "doc_name": self.documents[doc_id]["metadata"]["file_name"],
245
  "similarity": score / len(query_keywords) if query_keywords else 0
246
  })
247
 
248
  # ์ •๋ ฌ ๋ฐ ๋ฐ˜ํ™˜
249
  all_relevant_chunks.sort(key=lambda x: x.get('similarity', 0), reverse=True)
250
- return all_relevant_chunks[:top_k]
 
 
251
 
252
  def create_rag_prompt(self, query: str, doc_ids: List[str], top_k: int = 3) -> tuple:
253
  """RAG ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ - ์ฟผ๋ฆฌ์™€ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฐ˜ํ™˜"""
 
 
254
  relevant_chunks = self.search_relevant_chunks(query, doc_ids, top_k)
255
 
256
  if not relevant_chunks:
 
257
  return query, ""
258
 
 
 
259
  # ์ปจํ…์ŠคํŠธ ๊ตฌ์„ฑ
260
  context_parts = []
261
- context_parts.append("๋‹ค์Œ ๋ฌธ์„œ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”:\n")
262
  context_parts.append("=" * 40)
263
 
264
  for i, chunk in enumerate(relevant_chunks, 1):
265
- context_parts.append(f"\n[์ฐธ๊ณ  {i} - {chunk['doc_name']}]")
266
- content = chunk['content'][:300] if len(chunk['content']) > 300 else chunk['content']
267
  context_parts.append(content)
 
268
 
269
  context_parts.append("\n" + "=" * 40)
270
 
271
  context = "\n".join(context_parts)
272
- enhanced_query = f"{context}\n\n์งˆ๋ฌธ: {query}"
273
 
274
  return enhanced_query, context
275
 
@@ -304,7 +316,12 @@ def format_conversation_history(chat_history):
304
  @spaces.GPU()
305
  def generate_response(input_data, chat_history, max_new_tokens, system_prompt, temperature, top_p, top_k, repetition_penalty):
306
  """Generate response with optional RAG enhancement"""
307
- global last_context
 
 
 
 
 
308
 
309
  # Apply RAG if enabled
310
  if rag_enabled and selected_docs:
@@ -312,9 +329,11 @@ def generate_response(input_data, chat_history, max_new_tokens, system_prompt, t
312
  enhanced_input, context = rag_system.create_rag_prompt(input_data, doc_ids, top_k_chunks)
313
  last_context = context
314
  actual_input = enhanced_input
 
315
  else:
316
  actual_input = input_data
317
  last_context = ""
 
318
 
319
  # Prepare messages
320
  new_message = {"role": "user", "content": actual_input}
@@ -432,6 +451,9 @@ def update_rag_settings(enable, docs, k):
432
  selected_docs = docs if docs else []
433
  top_k_chunks = k
434
 
 
 
 
435
  status = "โœ… Enabled" if enable and docs else "โญ• Disabled"
436
  status_html = f"<div class='pdf-status pdf-info'>๐Ÿ” RAG: <strong>{status}</strong></div>"
437
 
 
203
  """๊ด€๋ จ ์ฒญํฌ ๊ฒ€์ƒ‰"""
204
  all_relevant_chunks = []
205
 
206
+ print(f"Searching chunks for query: '{query[:50]}...' in {len(doc_ids)} documents")
207
+
208
  if self.embedder and self.embeddings_store:
209
  # ์ž„๋ฒ ๋”ฉ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰
210
  query_embedding = self.embedder.encode([query])[0]
 
220
  sim = np.dot(query_embedding, emb) / (np.linalg.norm(query_embedding) * np.linalg.norm(emb))
221
  similarities.append(sim)
222
 
223
+ # ์ƒ์œ„ ์ฒญํฌ ์„ ํƒ - ์ž„๊ณ„๊ฐ’ ๋‚ฎ์ถค
224
  top_indices = np.argsort(similarities)[-top_k:][::-1]
225
 
226
  for idx in top_indices:
227
+ if similarities[idx] > 0.1: # ์ž„๊ณ„๊ฐ’์„ 0.2์—์„œ 0.1๋กœ ๋‚ฎ์ถค
228
  all_relevant_chunks.append({
229
  "content": chunks[idx],
230
  "doc_name": self.documents[doc_id]["metadata"]["file_name"],
231
  "similarity": similarities[idx]
232
  })
233
+ print(f"Found chunk with similarity: {similarities[idx]:.3f}")
234
  else:
235
  # ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰
236
+ print("Using keyword-based search (embedder not available)")
237
  query_keywords = set(query.lower().split())
238
 
239
  for doc_id in doc_ids:
240
  if doc_id in self.document_chunks:
241
  chunks = self.document_chunks[doc_id]
242
+ for i, chunk in enumerate(chunks): # ๋ชจ๋“  ์ฒญํฌ ๊ฒ€์ƒ‰
243
  chunk_lower = chunk.lower()
244
  score = sum(1 for keyword in query_keywords if keyword in chunk_lower)
245
  if score > 0:
246
  all_relevant_chunks.append({
247
+ "content": chunk[:800], # ๋” ๊ธด ์ฒญํฌ ์‚ฌ์šฉ
248
  "doc_name": self.documents[doc_id]["metadata"]["file_name"],
249
  "similarity": score / len(query_keywords) if query_keywords else 0
250
  })
251
 
252
  # ์ •๋ ฌ ๋ฐ ๋ฐ˜ํ™˜
253
  all_relevant_chunks.sort(key=lambda x: x.get('similarity', 0), reverse=True)
254
+ result = all_relevant_chunks[:top_k]
255
+ print(f"Returning {len(result)} chunks")
256
+ return result
257
 
258
  def create_rag_prompt(self, query: str, doc_ids: List[str], top_k: int = 3) -> tuple:
259
  """RAG ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ - ์ฟผ๋ฆฌ์™€ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฐ˜ํ™˜"""
260
+ print(f"Creating RAG prompt for query: '{query[:50]}...' with docs: {doc_ids}")
261
+
262
  relevant_chunks = self.search_relevant_chunks(query, doc_ids, top_k)
263
 
264
  if not relevant_chunks:
265
+ print("No relevant chunks found")
266
  return query, ""
267
 
268
+ print(f"Found {len(relevant_chunks)} relevant chunks")
269
+
270
  # ์ปจํ…์ŠคํŠธ ๊ตฌ์„ฑ
271
  context_parts = []
272
+ context_parts.append("Based on the following document context, please answer the question below:")
273
  context_parts.append("=" * 40)
274
 
275
  for i, chunk in enumerate(relevant_chunks, 1):
276
+ context_parts.append(f"\n[Document Reference {i} - {chunk['doc_name']}]")
277
+ content = chunk['content'][:500] if len(chunk['content']) > 500 else chunk['content']
278
  context_parts.append(content)
279
+ print(f"Added chunk {i} with similarity: {chunk.get('similarity', 0):.3f}")
280
 
281
  context_parts.append("\n" + "=" * 40)
282
 
283
  context = "\n".join(context_parts)
284
+ enhanced_query = f"{context}\n\nQuestion: {query}\n\nAnswer based on the document context provided above:"
285
 
286
  return enhanced_query, context
287
 
 
316
  @spaces.GPU()
317
  def generate_response(input_data, chat_history, max_new_tokens, system_prompt, temperature, top_p, top_k, repetition_penalty):
318
  """Generate response with optional RAG enhancement"""
319
+ global last_context, rag_enabled, selected_docs, top_k_chunks
320
+
321
+ # Debug logging
322
+ print(f"RAG Enabled: {rag_enabled}")
323
+ print(f"Selected Docs: {selected_docs}")
324
+ print(f"Available Docs: {list(rag_system.documents.keys())}")
325
 
326
  # Apply RAG if enabled
327
  if rag_enabled and selected_docs:
 
329
  enhanced_input, context = rag_system.create_rag_prompt(input_data, doc_ids, top_k_chunks)
330
  last_context = context
331
  actual_input = enhanced_input
332
+ print(f"RAG Applied - Original: {len(input_data)} chars, Enhanced: {len(enhanced_input)} chars")
333
  else:
334
  actual_input = input_data
335
  last_context = ""
336
+ print("RAG Not Applied")
337
 
338
  # Prepare messages
339
  new_message = {"role": "user", "content": actual_input}
 
451
  selected_docs = docs if docs else []
452
  top_k_chunks = k
453
 
454
+ # Debug logging
455
+ print(f"RAG Settings Updated - Enabled: {rag_enabled}, Docs: {selected_docs}, Top-K: {top_k_chunks}")
456
+
457
  status = "โœ… Enabled" if enable and docs else "โญ• Disabled"
458
  status_html = f"<div class='pdf-status pdf-info'>๐Ÿ” RAG: <strong>{status}</strong></div>"
459