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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -35
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import streamlit as st
2
  from hazm import Normalizer, SentenceTokenizer
3
  import os
@@ -517,10 +518,7 @@ import concurrent.futures
517
  from hazm import Normalizer
518
  from rapidfuzz import fuzz
519
  from langchain.schema import SystemMessage, HumanMessage
520
- from collections import Counter
521
- import heapq
522
 
523
- # مسیر پوشه اسناد
524
  folder_path = '46'
525
  normalizer = Normalizer()
526
 
@@ -546,34 +544,27 @@ def load_and_process_documents(path):
546
 
547
  return doc_texts
548
 
549
- # پردازش فایل‌ها
550
  doc_texts = load_and_process_documents(folder_path)
551
 
552
- # خواندن استاپ وردها
553
  with open("stopwords.txt", "r", encoding="utf-8") as f:
554
  stop_words = set(line.strip() for line in f if line.strip())
555
 
556
- # حذف استاپ‌وردها از متن
557
  def remove_stop_words(text, stop_words):
558
  words = text.split()
559
  return " ".join([word for word in words if word not in stop_words])
560
 
561
- # حذف عبارات ایست
562
- def remove_stop_phrases(text, stop_words):
563
- for phrase in stop_words:
564
- text = text.replace(phrase, "")
565
- return text
566
-
567
- # استخراج خطوط حاوی کلمات کوئری
568
  def extract_keywords_from_text(text, query_words):
569
  matched_lines = []
570
  lines = text.split("\n")
 
571
  for line in lines:
572
  if any(query_word in line for query_word in query_words):
573
  matched_lines.append(line)
574
  return matched_lines
575
 
576
- # خلاصه‌سازی بر اساس فراوانی واژگان
 
 
577
  def summarize_text_by_frequency(text, num_sentences=1):
578
  sentences = text.split('\n')
579
  word_freq = Counter()
@@ -592,43 +583,48 @@ def summarize_text_by_frequency(text, num_sentences=1):
592
  summarized_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
593
  return "\n".join(summarized_sentences)
594
 
595
- # پیدا کردن خطوط مشابه
 
 
 
596
  def find_closest_lines(query, doc_texts, stop_words, top_n=15):
597
  cleaned_query = remove_stop_words(query, stop_words)
598
  query_words = cleaned_query.split()
599
 
600
  all_matched_lines = []
 
601
  for filename, text in doc_texts.items():
602
  matched_lines = extract_keywords_from_text(text, query_words)
603
  for line in matched_lines:
604
- similarity = fuzz.partial_ratio(query, line)
605
  all_matched_lines.append((line, similarity))
606
-
607
  all_matched_lines.sort(key=lambda x: x[1], reverse=True)
 
608
  closest_lines = [line for line, _ in all_matched_lines[:top_n]]
609
 
610
  return closest_lines
611
 
612
- # رابط کاربری Streamlit
613
- st.title("پاسخ‌دهی به سوالات بر اساس اسناد بارگذاری‌شده")
614
-
615
- query = st.text_input("سوال خود را وارد کنید:")
616
 
617
  if query:
618
  closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=15)
619
-
620
- # حذف استاپ‌وردها از خطوط
621
  cleaned_closest_lines = [
622
- remove_stop_phrases(line, stop_words)
623
- for line in closest_lines
624
  ]
625
-
626
- # خلاصه‌سازی
627
  summarized_text = summarize_text_by_frequency("\n".join(cleaned_closest_lines), num_sentences=1)
628
-
629
- # نمایش خلاصه
630
  st.markdown(summarized_text)
631
 
 
632
  if summarized_text:
633
  prompt = f"""
634
  لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفه‌ای، دقیق و روان تولید کن.
@@ -640,16 +636,18 @@ if query:
640
  {summarized_text}
641
  پاسخ نهایی:
642
  """
643
-
 
644
  response = llm([
645
- SystemMessage(content="تو رزم یار ارتش هستی و از کتاب و دیتای موجود به سوالات پاسخ میدی."),
646
  HumanMessage(content=prompt)
647
  ])
648
-
 
649
  rewritten = response.content.strip()
650
-
651
  # نمایش نتیجه
652
  st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
653
-
654
  else:
655
- st.warning("هیچ خط مرتبطی پیدا نشد.")
 
1
+
2
  import streamlit as st
3
  from hazm import Normalizer, SentenceTokenizer
4
  import os
 
518
  from hazm import Normalizer
519
  from rapidfuzz import fuzz
520
  from langchain.schema import SystemMessage, HumanMessage
 
 
521
 
 
522
  folder_path = '46'
523
  normalizer = Normalizer()
524
 
 
544
 
545
  return doc_texts
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()
 
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
  لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفه‌ای، دقیق و روان تولید کن.
 
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("هیچ خط مرتبطی پیدا نشد.")