lordzukoiroh commited on
Commit
9e95b50
·
verified ·
1 Parent(s): d24b565

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -456,13 +456,13 @@ MONTAG_PERSONA = (
456
  "**Eğer soru kitaptan veya doğrudan Montag'ın dünyasından bağımsızsa bile, cevabını Montag'ın bakış açısıyla, kitaplara ve bilgiye olan özlemiyle ilişkilendirerek ver.**"
457
  )
458
 
459
- # === BAĞLAM ALMA FONKSİYONU ===
460
  def retrieve_context(question: str, chatbot_history: List[List[str]], k: int = 2) -> Tuple[List[str], str]:
461
  """FAISS indeksini kullanarak sorguya, geçmiş sohbete ve kitap odaklı anahtar kelimelere en uygun paragrafları getirir."""
462
  if index is None or embedder is None or not paragraphs:
463
  print("WARNING: FAISS index, embedder or paragraphs not initialized for context retrieval.")
464
  return [], "Bağlam bulunamadı."
465
 
 
466
  history_queries = []
467
  # Son 5 konuşma çiftini geçmişe dahil et (sadece kullanıcı mesajları)
468
  for user_msg, _ in chatbot_history[-5:]:
@@ -471,13 +471,29 @@ def retrieve_context(question: str, chatbot_history: List[List[str]], k: int = 2
471
  if cleaned_user_msg and not (("Montag düşünüyor..." in cleaned_user_msg) or ("saniyede üretildi" in cleaned_user_msg)):
472
  history_queries.append(cleaned_user_msg)
473
 
474
- # Montag'ın temel kimliğini ve görevini yansıtan doğrudan anahtar kelimeler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475
  montag_identity_keywords = [
476
  "guy montag", "montag", "itfaiyeci", "kitap yakmak", "yakıcı", "yangın",
477
  "kül", "alev", "benzin", "kask", "savaş", "kaos", "direniş"
478
  ]
479
 
480
- # Romanın genel temaları ve önemli karakterleri
481
  general_book_keywords = [
482
  "kitap", "okuma", "bilgi", "düşünce", "hakikat", "yasak", "sansür",
483
  "toplum", "televizyon", "mildred", "clarisse", "faber", "beatty",
@@ -485,10 +501,8 @@ def retrieve_context(question: str, chatbot_history: List[List[str]], k: int = 2
485
  "kaçış", "nehir"
486
  ]
487
 
488
- # Kullanıcı sorgusu, geçmiş sorguları ve tüm anahtar kelimeleri birleştirerek tek bir arama metni oluştur
489
  combined_query_text = f"{question} {' '.join(history_queries)} {' '.join(montag_identity_keywords)} {' '.join(general_book_keywords)}"
490
-
491
- # Fazla boşlukları temizleyelim
492
  combined_query_text = ' '.join(combined_query_text.split())
493
 
494
  try:
@@ -498,10 +512,11 @@ def retrieve_context(question: str, chatbot_history: List[List[str]], k: int = 2
498
  retrieved_texts = [p for i in I[0] if i < len(paragraphs) for p in [paragraphs[i]] if p not in previous_paragraphs]
499
  unique_retrieved_texts = list(dict.fromkeys(retrieved_texts))
500
  context_text = "\n".join(unique_retrieved_texts)
501
- return unique_retrieved_texts, context_text # Hem liste hem de birleştirilmiş metni döndür
502
  except Exception as e:
503
  print(f"Bağlam alınırken hata: {e}")
504
- return [], "Bağlam alınırken bir sorun oluştu."
 
505
 
506
  # === ALTERNATİF CEVAPLAR ===
507
  alternative_responses = [
 
456
  "**Eğer soru kitaptan veya doğrudan Montag'ın dünyasından bağımsızsa bile, cevabını Montag'ın bakış açısıyla, kitaplara ve bilgiye olan özlemiyle ilişkilendirerek ver.**"
457
  )
458
 
 
459
  def retrieve_context(question: str, chatbot_history: List[List[str]], k: int = 2) -> Tuple[List[str], str]:
460
  """FAISS indeksini kullanarak sorguya, geçmiş sohbete ve kitap odaklı anahtar kelimelere en uygun paragrafları getirir."""
461
  if index is None or embedder is None or not paragraphs:
462
  print("WARNING: FAISS index, embedder or paragraphs not initialized for context retrieval.")
463
  return [], "Bağlam bulunamadı."
464
 
465
+ # --- history_queries'i burada tanımlıyoruz (SADECE BİR KERE) ---
466
  history_queries = []
467
  # Son 5 konuşma çiftini geçmişe dahil et (sadece kullanıcı mesajları)
468
  for user_msg, _ in chatbot_history[-5:]:
 
471
  if cleaned_user_msg and not (("Montag düşünüyor..." in cleaned_user_msg) or ("saniyede üretildi" in cleaned_user_msg)):
472
  history_queries.append(cleaned_user_msg)
473
 
474
+ # --- previous_paragraphs'ı burada tanımlıyoruz ---
475
+ # chatbot_history'deki tüm geçmiş mesajları (hem kullanıcı hem asistan) 'previous_paragraphs' olarak alabiliriz.
476
+ # Bu, zaten konuşulmuş konuların bağlam olarak tekrar getirilmesini engeller.
477
+ previous_paragraphs = []
478
+ for user_msg, assistant_msg in chatbot_history:
479
+ if user_msg:
480
+ cleaned_user_msg = user_msg.replace('📚', '').replace('🧠', '').replace('🔥', '').strip()
481
+ if cleaned_user_msg and not (("Montag düşünüyor..." in cleaned_user_msg) or ("saniyede üretildi" in cleaned_user_msg)):
482
+ previous_paragraphs.append(cleaned_user_msg)
483
+ if assistant_msg:
484
+ cleaned_assistant_msg = assistant_msg.replace('📚', '').replace('🧠', '').replace('🔥', '').strip()
485
+ if cleaned_assistant_msg and not (("Montag düşünüyor..." in cleaned_assistant_msg) or ("saniyede üretildi" in cleaned_assistant_msg)):
486
+ previous_paragraphs.append(cleaned_assistant_msg)
487
+ # Tekrar eden öğeleri kaldırıp listeyi benzersiz hale getirelim (opsiyonel ama iyi bir pratik)
488
+ previous_paragraphs = list(dict.fromkeys(previous_paragraphs))
489
+ # --- previous_paragraphs tanımı sonu ---
490
+
491
+
492
  montag_identity_keywords = [
493
  "guy montag", "montag", "itfaiyeci", "kitap yakmak", "yakıcı", "yangın",
494
  "kül", "alev", "benzin", "kask", "savaş", "kaos", "direniş"
495
  ]
496
 
 
497
  general_book_keywords = [
498
  "kitap", "okuma", "bilgi", "düşünce", "hakikat", "yasak", "sansür",
499
  "toplum", "televizyon", "mildred", "clarisse", "faber", "beatty",
 
501
  "kaçış", "nehir"
502
  ]
503
 
 
504
  combined_query_text = f"{question} {' '.join(history_queries)} {' '.join(montag_identity_keywords)} {' '.join(general_book_keywords)}"
505
+
 
506
  combined_query_text = ' '.join(combined_query_text.split())
507
 
508
  try:
 
512
  retrieved_texts = [p for i in I[0] if i < len(paragraphs) for p in [paragraphs[i]] if p not in previous_paragraphs]
513
  unique_retrieved_texts = list(dict.fromkeys(retrieved_texts))
514
  context_text = "\n".join(unique_retrieved_texts)
515
+ return unique_retrieved_texts, context_text
516
  except Exception as e:
517
  print(f"Bağlam alınırken hata: {e}")
518
+ return [], "Bağlam bulunamadı."
519
+
520
 
521
  # === ALTERNATİF CEVAPLAR ===
522
  alternative_responses = [