Update app.py
Browse files
app.py
CHANGED
@@ -539,39 +539,29 @@ st.markdown("""
|
|
539 |
|
540 |
|
541 |
|
542 |
-
import nltk
|
543 |
-
from nltk.corpus import stopwords
|
544 |
-
from nltk.tokenize import word_tokenize
|
545 |
import string
|
|
|
|
|
|
|
546 |
|
547 |
-
|
548 |
-
# تابع برای استخراج کلمات کلیدی از سوال
|
549 |
def extract_keywords(query):
|
550 |
-
# حذف علامتهای نگارشی از جمله
|
551 |
-
query = query.translate(str.maketrans('', '', string.punctuation))
|
552 |
-
|
553 |
-
# اگر برای اولین بار از nltk استفاده میکنید، باید اینها را دانلود کن
|
554 |
words = word_tokenize(query)
|
555 |
|
556 |
-
|
557 |
-
|
558 |
-
keywords = [word for word in words if word not in stop_words]
|
559 |
|
560 |
return keywords
|
561 |
|
562 |
-
|
|
|
563 |
|
564 |
if query:
|
565 |
threshold = 60
|
566 |
matched_sentences = []
|
567 |
-
import nltk
|
568 |
-
nltk.download('punkt')
|
569 |
-
nltk.download('stopwords')
|
570 |
|
571 |
-
# استخراج کلمات کلیدی از سوال
|
572 |
keywords = extract_keywords(query)
|
573 |
|
574 |
-
# پیدا کردن جملات مشابه
|
575 |
for idx, sentence in enumerate(all_sentences):
|
576 |
similarity = fuzz.partial_ratio(query, sentence)
|
577 |
if similarity >= threshold:
|
@@ -580,13 +570,11 @@ if query:
|
|
580 |
if matched_sentences:
|
581 |
found_sentences = []
|
582 |
|
583 |
-
# برای هر جمله در matched_sentences بررسی میکنیم که آیا کلمات کلیدی در آن وجود دارد یا نه
|
584 |
for sentence in matched_sentences:
|
585 |
if any(keyword in sentence for keyword in keywords):
|
586 |
found_sentences.append(sentence)
|
587 |
|
588 |
if found_sentences:
|
589 |
-
# ایجاد متن نهایی از جملات مرتبط
|
590 |
matched_text = "\n".join(found_sentences)
|
591 |
|
592 |
# ساخت پرامپت اصلی برای تولید پاسخ نهایی حرفهای
|
@@ -602,7 +590,7 @@ if query:
|
|
602 |
پاسخ نهایی حرفهای بازنویسیشده:
|
603 |
"""
|
604 |
|
605 |
-
response = llm([
|
606 |
SystemMessage(content="You are a helpful assistant."),
|
607 |
HumanMessage(content=prompt)
|
608 |
])
|
@@ -617,7 +605,7 @@ if query:
|
|
617 |
{rewritten}
|
618 |
"""
|
619 |
|
620 |
-
review_response = llm([
|
621 |
SystemMessage(content="You are a helpful assistant."),
|
622 |
HumanMessage(content=review_prompt)
|
623 |
])
|
@@ -637,7 +625,7 @@ if query:
|
|
637 |
{rewritten}
|
638 |
پاسخ نهایی:
|
639 |
"""
|
640 |
-
new_response = llm([
|
641 |
SystemMessage(content="You are a helpful assistant."),
|
642 |
HumanMessage(content=final_prompt)
|
643 |
])
|
@@ -650,7 +638,7 @@ if query:
|
|
650 |
سوال:
|
651 |
{query}
|
652 |
"""
|
653 |
-
fallback_response = llm([
|
654 |
SystemMessage(content="You are a helpful assistant."),
|
655 |
HumanMessage(content=fallback_prompt)
|
656 |
])
|
@@ -664,11 +652,10 @@ if query:
|
|
664 |
سوال:
|
665 |
{query}
|
666 |
"""
|
667 |
-
response = llm([
|
668 |
SystemMessage(content="You are a helpful assistant."),
|
669 |
HumanMessage(content=prompt)
|
670 |
])
|
671 |
rewritten = clean_text(response.content.strip())
|
672 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
673 |
think.empty()
|
674 |
-
|
|
|
539 |
|
540 |
|
541 |
|
|
|
|
|
|
|
542 |
import string
|
543 |
+
from fuzzywuzzy import fuzz
|
544 |
+
from hazm import word_tokenize, stopwords
|
545 |
+
import streamlit as st
|
546 |
|
|
|
|
|
547 |
def extract_keywords(query):
|
|
|
|
|
|
|
|
|
548 |
words = word_tokenize(query)
|
549 |
|
550 |
+
stop_words = set(stopwords.list())
|
551 |
+
|
552 |
+
keywords = [word for word in words if word not in stop_words and word.isalpha()]
|
553 |
|
554 |
return keywords
|
555 |
|
556 |
+
def clean_text(text):
|
557 |
+
return text.strip()
|
558 |
|
559 |
if query:
|
560 |
threshold = 60
|
561 |
matched_sentences = []
|
|
|
|
|
|
|
562 |
|
|
|
563 |
keywords = extract_keywords(query)
|
564 |
|
|
|
565 |
for idx, sentence in enumerate(all_sentences):
|
566 |
similarity = fuzz.partial_ratio(query, sentence)
|
567 |
if similarity >= threshold:
|
|
|
570 |
if matched_sentences:
|
571 |
found_sentences = []
|
572 |
|
|
|
573 |
for sentence in matched_sentences:
|
574 |
if any(keyword in sentence for keyword in keywords):
|
575 |
found_sentences.append(sentence)
|
576 |
|
577 |
if found_sentences:
|
|
|
578 |
matched_text = "\n".join(found_sentences)
|
579 |
|
580 |
# ساخت پرامپت اصلی برای تولید پاسخ نهایی حرفهای
|
|
|
590 |
پاسخ نهایی حرفهای بازنویسیشده:
|
591 |
"""
|
592 |
|
593 |
+
response = llm([ # این خط باید تابع یا مدل مورد نظر شما باشد
|
594 |
SystemMessage(content="You are a helpful assistant."),
|
595 |
HumanMessage(content=prompt)
|
596 |
])
|
|
|
605 |
{rewritten}
|
606 |
"""
|
607 |
|
608 |
+
review_response = llm([ # این خط باید تابع یا مدل مورد نظر شما باشد
|
609 |
SystemMessage(content="You are a helpful assistant."),
|
610 |
HumanMessage(content=review_prompt)
|
611 |
])
|
|
|
625 |
{rewritten}
|
626 |
پاسخ نهایی:
|
627 |
"""
|
628 |
+
new_response = llm([ # این خط باید تابع یا مدل مورد نظر شما باشد
|
629 |
SystemMessage(content="You are a helpful assistant."),
|
630 |
HumanMessage(content=final_prompt)
|
631 |
])
|
|
|
638 |
سوال:
|
639 |
{query}
|
640 |
"""
|
641 |
+
fallback_response = llm([ # این خط باید تابع یا مدل مورد نظر شما باشد
|
642 |
SystemMessage(content="You are a helpful assistant."),
|
643 |
HumanMessage(content=fallback_prompt)
|
644 |
])
|
|
|
652 |
سوال:
|
653 |
{query}
|
654 |
"""
|
655 |
+
response = llm([ # این خط باید تابع یا مدل مورد نظر شما باشد
|
656 |
SystemMessage(content="You are a helpful assistant."),
|
657 |
HumanMessage(content=prompt)
|
658 |
])
|
659 |
rewritten = clean_text(response.content.strip())
|
660 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
661 |
think.empty()
|
|