Update app.py
Browse files
app.py
CHANGED
@@ -539,44 +539,59 @@ def load_and_process_documents(path):
|
|
539 |
|
540 |
doc_texts = load_and_process_documents(folder_path)
|
541 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
542 |
|
|
|
543 |
def clean_text(text):
|
544 |
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
545 |
|
|
|
|
|
|
|
|
|
546 |
|
547 |
-
|
548 |
-
|
549 |
-
scores = [(f, fuzz.partial_ratio(query, f)) for f in filenames]
|
550 |
-
scores.sort(key=lambda x: x[1], reverse=True)
|
551 |
-
return [score[0] for score in scores[:top_n]] # برگشت دادن N فایل با بیشترین شباهت
|
552 |
-
|
553 |
-
def find_best_answer(query, top_files, doc_texts):
|
554 |
-
best_match = None
|
555 |
-
best_score = 0
|
556 |
-
|
557 |
# بررسی محتوای فایلها
|
558 |
-
for filename in
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
|
|
|
|
|
|
|
|
|
|
564 |
|
565 |
-
|
|
|
|
|
566 |
|
|
|
567 |
|
568 |
# حالا این رو در کد اصلی استفاده میکنیم:
|
569 |
if query:
|
570 |
-
|
571 |
-
|
572 |
|
573 |
-
if
|
574 |
prompt = f"""
|
575 |
-
لطفاً با توجه به سؤال زیر و محتوای
|
576 |
سوال:
|
577 |
{query}
|
578 |
-
|
579 |
-
{
|
580 |
پاسخ نهایی:
|
581 |
"""
|
582 |
|
@@ -589,4 +604,4 @@ if query:
|
|
589 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
590 |
|
591 |
else:
|
592 |
-
st.warning("هیچ
|
|
|
539 |
|
540 |
doc_texts = load_and_process_documents(folder_path)
|
541 |
|
542 |
+
# تابعی برای استخراج کلمات از متن
|
543 |
+
def extract_keywords_from_text(text, query_words):
|
544 |
+
matched_lines = []
|
545 |
+
lines = text.split("\n")
|
546 |
+
|
547 |
+
# جستجو برای هر کلمه در هر خط
|
548 |
+
for line in lines:
|
549 |
+
if any(query_word in line for query_word in query_words):
|
550 |
+
matched_lines.append(line)
|
551 |
+
return matched_lines
|
552 |
|
553 |
+
# تابعی برای پاکسازی متن
|
554 |
def clean_text(text):
|
555 |
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
556 |
|
557 |
+
# تابعی برای پیدا کردن نزدیکترین خطوط به سوال
|
558 |
+
def find_closest_lines(query, doc_texts, top_n=20, exclude_line=None):
|
559 |
+
# تقسیم سوال به کلمات
|
560 |
+
query_words = query.split()
|
561 |
|
562 |
+
all_matched_lines = []
|
563 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
# بررسی محتوای فایلها
|
565 |
+
for filename, text in doc_texts.items():
|
566 |
+
matched_lines = extract_keywords_from_text(text, query_words)
|
567 |
+
for line in matched_lines:
|
568 |
+
similarity = fuzz.partial_ratio(query, line) # محاسبه شباهت خط با سوال
|
569 |
+
all_matched_lines.append((line, similarity))
|
570 |
+
|
571 |
+
# مرتب سازی بر اساس شباهت
|
572 |
+
all_matched_lines.sort(key=lambda x: x[1], reverse=True)
|
573 |
+
|
574 |
+
# انتخاب ۲۰ خط نزدیکتر
|
575 |
+
closest_lines = [line for line, _ in all_matched_lines[:top_n]]
|
576 |
|
577 |
+
# حذف خط خاص از لیست در صورت وجود
|
578 |
+
if exclude_line and exclude_line in closest_lines:
|
579 |
+
closest_lines.remove(exclude_line)
|
580 |
|
581 |
+
return closest_lines
|
582 |
|
583 |
# حالا این رو در کد اصلی استفاده میکنیم:
|
584 |
if query:
|
585 |
+
# پیدا کردن ۲۰ خط نزدیکتر به سوال (و حذف یک خط خاص)
|
586 |
+
closest_lines = find_closest_lines(query, doc_texts, top_n=20, exclude_line=None)
|
587 |
|
588 |
+
if closest_lines:
|
589 |
prompt = f"""
|
590 |
+
لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
591 |
سوال:
|
592 |
{query}
|
593 |
+
خطوط مرتبط:
|
594 |
+
{closest_lines}
|
595 |
پاسخ نهایی:
|
596 |
"""
|
597 |
|
|
|
604 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
605 |
|
606 |
else:
|
607 |
+
st.warning("هیچ خط مرتبطی پیدا نشد.")
|