Update app.py
Browse files
app.py
CHANGED
@@ -559,6 +559,32 @@ def extract_keywords_from_text(text, query_words):
|
|
559 |
def clean_text(text):
|
560 |
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
561 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
562 |
def find_closest_lines(query, doc_texts, stop_words, top_n=10):
|
563 |
cleaned_query = remove_stop_words(query, stop_words)
|
564 |
query_words = cleaned_query.split()
|
@@ -593,16 +619,18 @@ if query:
|
|
593 |
clean_text(" ".join([word for word in line.split() if word not in stop_words]))
|
594 |
for line in closest_lines
|
595 |
]
|
|
|
|
|
596 |
|
597 |
-
if
|
598 |
-
cleaned_text = "\n".join(cleaned_closest_lines[:1])
|
599 |
|
600 |
prompt = f"""
|
601 |
لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
602 |
سوال:
|
603 |
{query}
|
604 |
خطوط مرتبط:
|
605 |
-
{
|
606 |
پاسخ نهایی:
|
607 |
"""
|
608 |
|
|
|
559 |
def clean_text(text):
|
560 |
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
561 |
|
562 |
+
|
563 |
+
from collections import Counter
|
564 |
+
import heapq
|
565 |
+
|
566 |
+
def summarize_text_by_frequency(text, num_sentences=3):
|
567 |
+
sentences = text.split('\n')
|
568 |
+
word_freq = Counter()
|
569 |
+
|
570 |
+
for sentence in sentences:
|
571 |
+
for word in sentence.split():
|
572 |
+
if word not in stop_words:
|
573 |
+
word_freq[word] += 1
|
574 |
+
|
575 |
+
sentence_scores = {}
|
576 |
+
for sentence in sentences:
|
577 |
+
for word in sentence.split():
|
578 |
+
if word in word_freq:
|
579 |
+
sentence_scores[sentence] = sentence_scores.get(sentence, 0) + word_freq[word]
|
580 |
+
|
581 |
+
summarized_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
|
582 |
+
return "\n".join(summarized_sentences)
|
583 |
+
|
584 |
+
|
585 |
+
|
586 |
+
|
587 |
+
|
588 |
def find_closest_lines(query, doc_texts, stop_words, top_n=10):
|
589 |
cleaned_query = remove_stop_words(query, stop_words)
|
590 |
query_words = cleaned_query.split()
|
|
|
619 |
clean_text(" ".join([word for word in line.split() if word not in stop_words]))
|
620 |
for line in closest_lines
|
621 |
]
|
622 |
+
summarized_text = summarize_text_by_frequency("\n".join(cleaned_closest_lines), num_sentences=1)
|
623 |
+
|
624 |
|
625 |
+
if summarized_text:
|
626 |
+
# cleaned_text = "\n".join(cleaned_closest_lines[:1])
|
627 |
|
628 |
prompt = f"""
|
629 |
لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
630 |
سوال:
|
631 |
{query}
|
632 |
خطوط مرتبط:
|
633 |
+
{summarized_text}
|
634 |
پاسخ نهایی:
|
635 |
"""
|
636 |
|