M17idd commited on
Commit
9f8a6b6
·
verified ·
1 Parent(s): 34701b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -60
app.py CHANGED
@@ -509,7 +509,6 @@ st.markdown("""
509
  """, unsafe_allow_html=True)
510
 
511
 
512
-
513
  import os
514
  import re
515
  import docx
@@ -546,108 +545,81 @@ def load_and_process_documents(path):
546
 
547
  doc_texts = load_and_process_documents(folder_path)
548
 
549
- with open("stopwords.txt", "r", encoding="utf-8") as f:
550
- stop_words = set(line.strip() for line in f if line.strip())
 
 
 
551
 
 
552
  def remove_stop_words(text, stop_words):
553
  words = text.split()
554
  return " ".join([word for word in words if word not in stop_words])
555
 
 
556
  def extract_keywords_from_text(text, query_words):
557
  matched_lines = []
558
  lines = text.split("\n")
559
 
 
560
  for line in lines:
561
  if any(query_word in line for query_word in query_words):
562
  matched_lines.append(line)
563
  return matched_lines
564
 
565
- from collections import Counter
566
- import heapq
567
-
568
- def summarize_text_by_frequency(text, num_sentences=1):
569
- sentences = text.split('\n')
570
- word_freq = Counter()
571
-
572
- for sentence in sentences:
573
- for word in sentence.split():
574
- if word not in stop_words:
575
- word_freq[word] += 1
576
-
577
- sentence_scores = {}
578
- for sentence in sentences:
579
- for word in sentence.split():
580
- if word in word_freq:
581
- sentence_scores[sentence] = sentence_scores.get(sentence, 0) + word_freq[word]
582
-
583
- summarized_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
584
- return "\n".join(summarized_sentences)
585
-
586
 
587
-
588
-
589
-
590
- def find_closest_lines(query, doc_texts, stop_words, top_n=15):
591
  cleaned_query = remove_stop_words(query, stop_words)
592
  query_words = cleaned_query.split()
593
 
594
  all_matched_lines = []
595
 
 
596
  for filename, text in doc_texts.items():
597
  matched_lines = extract_keywords_from_text(text, query_words)
598
  for line in matched_lines:
599
  similarity = fuzz.partial_ratio(query, line) # محاسبه شباهت خط با سوال
600
  all_matched_lines.append((line, similarity))
601
 
 
602
  all_matched_lines.sort(key=lambda x: x[1], reverse=True)
603
 
 
604
  closest_lines = [line for line, _ in all_matched_lines[:top_n]]
605
 
606
- return closest_lines
 
 
607
 
608
- def remove_stop_phrases(text, stop_words):
609
- for phrase in stop_words:
610
- text = text.replace(phrase, "")
611
- return text
612
 
 
613
  if query:
614
- closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=15)
615
-
616
- # حذف استپ‌وردها از خطوط و سپس پاکسازی نهایی متن
617
- cleaned_closest_lines = [
618
- remove_stop_phrases(line, stop_words)
619
- for line in closest_lines
620
- ]
621
-
622
- summarized_text = summarize_text_by_frequency("\n".join(cleaned_closest_lines), num_sentences=1)
623
-
624
- summarized_cleaned = remove_stop_phrases(summarized_text, stop_words)
625
- st.markdown(summarized_text)
626
-
627
 
628
- if summarized_text:
629
  prompt = f"""
630
- لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفه‌ای، دقیق و روان تولید کن.
631
- فقط از متن خطوط مرتبط استفاده کن و خلاصه بنویس. اطلاعات اضافی ننویس و فقط به سوال پاسخ بده.
632
- در صورتی که اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن که اطلاعات کافی برای پاسخ‌دهی موجود نیست.
633
  سوال:
634
  {query}
635
  خطوط مرتبط:
636
- {summarized_text}
637
  پاسخ نهایی:
638
  """
639
-
640
- # ارسال پیام به مدل به صورت صحیح
641
  response = llm([
642
- SystemMessage(content="تو رزم یار ارتش هستی و از کتاب و دیتای موجود به سوالات پاسخ میدی."),
643
  HumanMessage(content=prompt)
644
  ])
645
-
646
- # فرض بر این است که خروجی مدل به صورت دیکشنری است
647
- rewritten = response.content.strip()
648
-
649
- # نمایش نتیجه
650
  st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
651
-
652
  else:
653
  st.warning("هیچ خط مرتبطی پیدا نشد.")
 
509
  """, unsafe_allow_html=True)
510
 
511
 
 
512
  import os
513
  import re
514
  import docx
 
545
 
546
  doc_texts = load_and_process_documents(folder_path)
547
 
548
+ stop_words = [
549
+ "است", "و", "با", "که", "در", "از", "برای", "به", "بر", "تا", "این", "آن", "یک", "کدام", "کجا", "هم", "همه",
550
+ "یا", "از", "بر", "همچنین", "می", "باید", "شود", "شد", "گفت", "گویا", "داشت", "داشتن", "کنند", "کنیم",
551
+ "کرد", "کردن", "نیز", "یا", "اگر", "ای", "اینکه", "نه", "باشید", "باشم", "باشی", "در حالی که", "مگر", "چرا"
552
+ ]
553
 
554
+ # تابعی برای پاکسازی کلمات اضافی از سوال
555
  def remove_stop_words(text, stop_words):
556
  words = text.split()
557
  return " ".join([word for word in words if word not in stop_words])
558
 
559
+ # تابعی برای استخراج کلمات از متن
560
  def extract_keywords_from_text(text, query_words):
561
  matched_lines = []
562
  lines = text.split("\n")
563
 
564
+ # جستجو برای هر کلمه در هر خط
565
  for line in lines:
566
  if any(query_word in line for query_word in query_words):
567
  matched_lines.append(line)
568
  return matched_lines
569
 
570
+ # تابعی برای پاکسازی متن
571
+ def clean_text(text):
572
+ return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
 
574
+ # تابعی برای پیدا کردن نزدیک‌ترین خطوط به سوال
575
+ def find_closest_lines(query, doc_texts, stop_words, top_n=20, exclude_line=None):
576
+ # حذف کلمات اضافی از سوال
 
577
  cleaned_query = remove_stop_words(query, stop_words)
578
  query_words = cleaned_query.split()
579
 
580
  all_matched_lines = []
581
 
582
+ # بررسی محتوای فایل‌ها
583
  for filename, text in doc_texts.items():
584
  matched_lines = extract_keywords_from_text(text, query_words)
585
  for line in matched_lines:
586
  similarity = fuzz.partial_ratio(query, line) # محاسبه شباهت خط با سوال
587
  all_matched_lines.append((line, similarity))
588
 
589
+ # مرتب سازی بر اساس شباهت
590
  all_matched_lines.sort(key=lambda x: x[1], reverse=True)
591
 
592
+ # انتخاب ۲۰ خط نزدیک‌تر
593
  closest_lines = [line for line, _ in all_matched_lines[:top_n]]
594
 
595
+ # حذف خط خاص از لیست در صورت وجود
596
+ if exclude_line and exclude_line in closest_lines:
597
+ closest_lines.remove(exclude_line)
598
 
599
+ return closest_lines
 
 
 
600
 
601
+ # حالا این رو در کد اصلی استفاده می‌کنیم:
602
  if query:
603
+ # پیدا کردن ۲۰ خط نزدیک‌تر به سوال (و حذف یک خط خاص)
604
+ closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=20, exclude_line=None)
 
 
 
 
 
 
 
 
 
 
 
605
 
606
+ if closest_lines:
607
  prompt = f"""
608
+ لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفه‌ای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
 
 
609
  سوال:
610
  {query}
611
  خطوط مرتبط:
612
+ {closest_lines}
613
  پاسخ نهایی:
614
  """
615
+
 
616
  response = llm([
617
+ SystemMessage(content="You are a helpful assistant."),
618
  HumanMessage(content=prompt)
619
  ])
620
+ rewritten = clean_text(response.content.strip())
621
+
 
 
 
622
  st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
623
+
624
  else:
625
  st.warning("هیچ خط مرتبطی پیدا نشد.")