Update app.py
Browse files
app.py
CHANGED
@@ -539,55 +539,121 @@ 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 |
def find_closest_filenames(query, filenames, top_n=3):
|
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]]
|
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 top_files:
|
559 |
text = doc_texts[filename]
|
560 |
-
similarity = fuzz.partial_ratio(query, text)
|
561 |
if similarity > best_score:
|
562 |
best_score = similarity
|
563 |
best_match = filename
|
564 |
|
565 |
return best_match, doc_texts.get(best_match, "")
|
566 |
|
567 |
-
|
568 |
# حالا این رو در کد اصلی استفاده میکنیم:
|
569 |
if query:
|
|
|
570 |
top_files = find_closest_filenames(query, list(doc_texts.keys()), top_n=3)
|
571 |
best_file, matched_text = find_best_answer(query, top_files, doc_texts)
|
572 |
|
573 |
-
if best_file:
|
574 |
-
|
575 |
-
لطفاً با توجه به
|
576 |
سوال:
|
577 |
{query}
|
578 |
محتوای سند:
|
579 |
{matched_text}
|
580 |
پاسخ نهایی:
|
581 |
"""
|
582 |
-
|
583 |
-
response = llm([
|
584 |
SystemMessage(content="You are a helpful assistant."),
|
585 |
-
HumanMessage(content=
|
586 |
])
|
587 |
-
rewritten = clean_text(
|
588 |
-
|
589 |
-
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
590 |
|
591 |
-
|
592 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
593 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
539 |
|
540 |
doc_texts = load_and_process_documents(folder_path)
|
541 |
|
|
|
542 |
def clean_text(text):
|
543 |
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
544 |
|
|
|
545 |
def find_closest_filenames(query, filenames, top_n=3):
|
|
|
546 |
scores = [(f, fuzz.partial_ratio(query, f)) for f in filenames]
|
547 |
scores.sort(key=lambda x: x[1], reverse=True)
|
548 |
+
return [score[0] for score in scores[:top_n]]
|
549 |
|
550 |
def find_best_answer(query, top_files, doc_texts):
|
551 |
best_match = None
|
552 |
best_score = 0
|
553 |
|
|
|
554 |
for filename in top_files:
|
555 |
text = doc_texts[filename]
|
556 |
+
similarity = fuzz.partial_ratio(query, text)
|
557 |
if similarity > best_score:
|
558 |
best_score = similarity
|
559 |
best_match = filename
|
560 |
|
561 |
return best_match, doc_texts.get(best_match, "")
|
562 |
|
|
|
563 |
# حالا این رو در کد اصلی استفاده میکنیم:
|
564 |
if query:
|
565 |
+
# مرحله 1: درخواست از مدل برای بررسی وجود پاسخ در نام فایلها
|
566 |
top_files = find_closest_filenames(query, list(doc_texts.keys()), top_n=3)
|
567 |
best_file, matched_text = find_best_answer(query, top_files, doc_texts)
|
568 |
|
569 |
+
if best_file: # اگر پاسخ از اسم فایلها پیدا شد
|
570 |
+
prompt_for_model_1 = f"""
|
571 |
+
لطفاً با توجه به سوال زیر و محتوای سند موجود، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن سند استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
572 |
سوال:
|
573 |
{query}
|
574 |
محتوای سند:
|
575 |
{matched_text}
|
576 |
پاسخ نهایی:
|
577 |
"""
|
578 |
+
response_model_1 = llm([
|
|
|
579 |
SystemMessage(content="You are a helpful assistant."),
|
580 |
+
HumanMessage(content=prompt_for_model_1)
|
581 |
])
|
582 |
+
rewritten = clean_text(response_model_1.content.strip())
|
|
|
|
|
583 |
|
584 |
+
# مرحله 2: بررسی پاسخ مدل اول
|
585 |
+
prompt_for_model_2 = f"""
|
586 |
+
سوال:
|
587 |
+
{query}
|
588 |
+
آیا مدل اول به درستی جواب داده است؟
|
589 |
+
پاسخ مدل اول:
|
590 |
+
{rewritten}
|
591 |
+
لطفاً تایید کن که آیا این پاسخ مناسب است یا خیر. اگر مناسب نیست، لطفاً اطلاعات بیشتری از متن موجود در سند بده.
|
592 |
+
"""
|
593 |
+
response_model_2 = llm([
|
594 |
+
SystemMessage(content="You are a helpful assistant."),
|
595 |
+
HumanMessage(content=prompt_for_model_2)
|
596 |
+
])
|
597 |
+
answer_model_2 = clean_text(response_model_2.content.strip())
|
598 |
|
599 |
+
if "تایید" in answer_model_2: # اگر تایید شد که پاسخ درست است
|
600 |
+
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
601 |
+
else:
|
602 |
+
# مرحله 3: جستجو در متن کل سندها
|
603 |
+
best_file_from_text = None
|
604 |
+
best_answer_from_text = ""
|
605 |
+
top_files_for_text = find_closest_filenames(query, list(doc_texts.keys()), top_n=3)
|
606 |
+
|
607 |
+
for filename in top_files_for_text:
|
608 |
+
text = doc_texts[filename]
|
609 |
+
similarity = fuzz.partial_ratio(query, text)
|
610 |
+
if similarity > 50: # حداقل شباهت برای انتخاب پاسخ
|
611 |
+
best_file_from_text = filename
|
612 |
+
best_answer_from_text = text
|
613 |
+
break # اگر جواب خوبی پیدا شد، جستجو را متوقف میکنیم
|
614 |
+
|
615 |
+
if best_file_from_text: # اگر جوابی از متن پیدا شد
|
616 |
+
prompt_for_model_3 = f"""
|
617 |
+
لطفاً با توجه به سوال زیر و محتوای سند موجود، یک پاسخ نهایی حرفهای، دقیق و روان تولید کن. فقط از متن سند استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
|
618 |
+
سوال:
|
619 |
+
{query}
|
620 |
+
محتوای سند:
|
621 |
+
{best_answer_from_text}
|
622 |
+
پاسخ نهایی:
|
623 |
+
"""
|
624 |
+
response_model_3 = llm([
|
625 |
+
SystemMessage(content="You are a helpful assistant."),
|
626 |
+
HumanMessage(content=prompt_for_model_3)
|
627 |
+
])
|
628 |
+
rewritten_model_3 = clean_text(response_model_3.content.strip())
|
629 |
+
|
630 |
+
# بررسی تایید مدل 2
|
631 |
+
prompt_for_model_4 = f"""
|
632 |
+
سوال:
|
633 |
+
{query}
|
634 |
+
آیا مدل دوم به درستی جواب داده است؟
|
635 |
+
پاسخ مدل دوم:
|
636 |
+
{rewritten_model_3}
|
637 |
+
لطفاً تایید کن که آیا این پاسخ مناسب است یا خیر.
|
638 |
+
"""
|
639 |
+
response_model_4 = llm([
|
640 |
+
SystemMessage(content="You are a helpful assistant."),
|
641 |
+
HumanMessage(content=prompt_for_model_4)
|
642 |
+
])
|
643 |
+
answer_model_4 = clean_text(response_model_4.content.strip())
|
644 |
+
|
645 |
+
if "تایید" in answer_model_4: # اگر تایید شد که پاسخ درست است
|
646 |
+
st.markdown(f'<div class="chat-message">{rewritten_model_3}</div>', unsafe_allow_html=True)
|
647 |
+
else:
|
648 |
+
# مرحله آخر: اگر تایید نشده بود، از دانش خود مدل استفاده کن
|
649 |
+
prompt_for_model_5 = f"""
|
650 |
+
سوال:
|
651 |
+
{query}
|
652 |
+
لطفاً جواب دقیقی از دانش خودت بده.
|
653 |
+
"""
|
654 |
+
response_model_5 = llm([
|
655 |
+
SystemMessage(content="You are a knowledgeable assistant."),
|
656 |
+
HumanMessage(content=prompt_for_model_5)
|
657 |
+
])
|
658 |
+
rewritten_model_5 = clean_text(response_model_5.content.strip())
|
659 |
+
st.markdown(f'<div class="chat-message">{rewritten_model_5}</div>', unsafe_allow_html=True)
|