Update app.py
Browse files
app.py
CHANGED
@@ -508,7 +508,6 @@ st.markdown("""
|
|
508 |
</style>
|
509 |
""", unsafe_allow_html=True)
|
510 |
|
511 |
-
|
512 |
import os
|
513 |
import re
|
514 |
import docx
|
@@ -545,6 +544,7 @@ def load_and_process_documents(path):
|
|
545 |
|
546 |
doc_texts = load_and_process_documents(folder_path)
|
547 |
|
|
|
548 |
stop_words = [
|
549 |
"است", "و", "با", "که", "در", "از", "برای", "به", "بر", "تا", "این", "آن", "یک", "کدام", "کجا", "هم", "همه",
|
550 |
"یا", "از", "بر", "همچنین", "می", "باید", "شود", "شد", "گفت", "گویا", "داشت", "داشتن", "کنند", "کنیم",
|
@@ -572,7 +572,7 @@ 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=
|
576 |
# حذف کلمات اضافی از سوال
|
577 |
cleaned_query = remove_stop_words(query, stop_words)
|
578 |
query_words = cleaned_query.split()
|
@@ -589,27 +589,35 @@ def find_closest_lines(query, doc_texts, stop_words, top_n=20, exclude_line=None
|
|
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=
|
|
|
|
|
|
|
605 |
|
606 |
-
if
|
607 |
prompt = f"""
|
608 |
لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
609 |
سوال:
|
610 |
{query}
|
611 |
خطوط مرتبط:
|
612 |
-
{
|
613 |
پاسخ نهایی:
|
614 |
"""
|
615 |
|
@@ -622,4 +630,4 @@ if query:
|
|
622 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
623 |
|
624 |
else:
|
625 |
-
st.warning("هیچ خط مرتبطی پیدا نشد.")
|
|
|
508 |
</style>
|
509 |
""", unsafe_allow_html=True)
|
510 |
|
|
|
511 |
import os
|
512 |
import re
|
513 |
import docx
|
|
|
544 |
|
545 |
doc_texts = load_and_process_documents(folder_path)
|
546 |
|
547 |
+
# لیست کلمات توقف
|
548 |
stop_words = [
|
549 |
"است", "و", "با", "که", "در", "از", "برای", "به", "بر", "تا", "این", "آن", "یک", "کدام", "کجا", "هم", "همه",
|
550 |
"یا", "از", "بر", "همچنین", "می", "باید", "شود", "شد", "گفت", "گویا", "داشت", "داشتن", "کنند", "کنیم",
|
|
|
572 |
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
573 |
|
574 |
# تابعی برای پیدا کردن نزدیکترین خطوط به سوال
|
575 |
+
def find_closest_lines(query, doc_texts, stop_words, top_n=10):
|
576 |
# حذف کلمات اضافی از سوال
|
577 |
cleaned_query = remove_stop_words(query, stop_words)
|
578 |
query_words = cleaned_query.split()
|
|
|
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 |
return closest_lines
|
596 |
|
597 |
+
# تابعی برای حذف کلمات توقف از یک لیست از خطوط
|
598 |
+
def remove_stop_words_from_lines(lines, stop_words):
|
599 |
+
cleaned_lines = []
|
600 |
+
for line in lines:
|
601 |
+
words = line.split()
|
602 |
+
cleaned_words = [word for word in words if word not in stop_words]
|
603 |
+
cleaned_lines.append(" ".join(cleaned_words))
|
604 |
+
return cleaned_lines
|
605 |
+
|
606 |
# حالا این رو در کد اصلی استفاده میکنیم:
|
607 |
if query:
|
608 |
+
# پیدا کردن ۱۰ خط نزدیکتر به سوال
|
609 |
+
closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=3)
|
610 |
+
|
611 |
+
# حذف کلمات توقف از خطوط نزدیک
|
612 |
+
cleaned_closest_lines = remove_stop_words_from_lines(closest_lines, stop_words)
|
613 |
|
614 |
+
if cleaned_closest_lines:
|
615 |
prompt = f"""
|
616 |
لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
617 |
سوال:
|
618 |
{query}
|
619 |
خطوط مرتبط:
|
620 |
+
{cleaned_closest_lines}
|
621 |
پاسخ نهایی:
|
622 |
"""
|
623 |
|
|
|
630 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
631 |
|
632 |
else:
|
633 |
+
st.warning("هیچ خط مرتبطی پیدا نشد.")
|