Update app.py
Browse files
app.py
CHANGED
|
@@ -540,19 +540,49 @@ st.markdown("""
|
|
| 540 |
|
| 541 |
|
| 542 |
|
|
|
|
|
|
|
| 543 |
from sentence_transformers import SentenceTransformer, util
|
| 544 |
|
| 545 |
-
# بارگذاری مدل
|
| 546 |
model = SentenceTransformer("nomic-ai/nomic-embed-text-v2-moe", trust_remote_code=True)
|
| 547 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
if query:
|
| 549 |
found = False
|
| 550 |
threshold = 0.55
|
| 551 |
|
|
|
|
| 552 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 553 |
|
| 554 |
-
|
| 555 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
similarities = util.pytorch_cos_sim(query_embedding, sentence_embeddings)[0]
|
| 557 |
|
| 558 |
for idx, similarity in enumerate(similarities):
|
|
@@ -566,10 +596,7 @@ if query:
|
|
| 566 |
{sentence}
|
| 567 |
پاسخ نهایی حرفهای بازنویسیشده:
|
| 568 |
"""
|
| 569 |
-
response = llm([
|
| 570 |
-
SystemMessage(content="You are a helpful assistant."),
|
| 571 |
-
HumanMessage(content=prompt)
|
| 572 |
-
])
|
| 573 |
rewritten = response.content.strip()
|
| 574 |
|
| 575 |
# بررسی مرتبط بودن پاسخ با سؤال
|
|
@@ -580,10 +607,7 @@ if query:
|
|
| 580 |
پاسخ:
|
| 581 |
{rewritten}
|
| 582 |
"""
|
| 583 |
-
review_response = llm([
|
| 584 |
-
SystemMessage(content="You are a helpful assistant."),
|
| 585 |
-
HumanMessage(content=review_prompt)
|
| 586 |
-
])
|
| 587 |
review_result = review_response.content.strip()
|
| 588 |
if "تأیید شد" in review_result:
|
| 589 |
rewritten = clean_text(rewritten)
|
|
@@ -595,10 +619,7 @@ if query:
|
|
| 595 |
سوال:
|
| 596 |
{query}
|
| 597 |
"""
|
| 598 |
-
new_response = llm([
|
| 599 |
-
SystemMessage(content="You are a helpful assistant."),
|
| 600 |
-
HumanMessage(content=final_prompt)
|
| 601 |
-
])
|
| 602 |
final_answer = new_response.content.strip()
|
| 603 |
final_answer = clean_text(final_answer)
|
| 604 |
st.markdown(f'<div class="chat-message">{final_answer}</div>', unsafe_allow_html=True)
|
|
@@ -609,11 +630,8 @@ if query:
|
|
| 609 |
|
| 610 |
if not found:
|
| 611 |
prompt = f"لطفاً بر اساس سوال زیر یک متن مرتبط و معنادار تولید کن و جملات ساختگی استفاده نکن و از جملات موجود در اسناد و جملات معتبر استفاده کن که در ویکی پدیا موجود باشد و از زبانی جز فارسی استفاده نکن:\n\nسوال: {query}"
|
| 612 |
-
response = llm([
|
| 613 |
-
SystemMessage(content="You are a helpful assistant."),
|
| 614 |
-
HumanMessage(content=prompt)
|
| 615 |
-
])
|
| 616 |
rewritten = response.content.strip()
|
| 617 |
rewritten = clean_text(rewritten)
|
| 618 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
| 619 |
-
think.empty()
|
|
|
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
+
import os
|
| 544 |
+
import json
|
| 545 |
from sentence_transformers import SentenceTransformer, util
|
| 546 |
|
| 547 |
+
# بارگذاری مدل
|
| 548 |
model = SentenceTransformer("nomic-ai/nomic-embed-text-v2-moe", trust_remote_code=True)
|
| 549 |
|
| 550 |
+
# مسیر فایل کش برای ذخیره امبدینگهای کتاب
|
| 551 |
+
embeddings_cache_path = "book_embeddings_cache.json"
|
| 552 |
+
|
| 553 |
+
def load_embeddings_from_cache():
|
| 554 |
+
"""بارگذاری امبدینگها از کش (فایل JSON)"""
|
| 555 |
+
if os.path.exists(embeddings_cache_path):
|
| 556 |
+
with open(embeddings_cache_path, "r") as file:
|
| 557 |
+
return json.load(file)
|
| 558 |
+
return {}
|
| 559 |
+
|
| 560 |
+
def save_embeddings_to_cache(embeddings):
|
| 561 |
+
"""ذخیره امبدینگها به کش (فایل JSON)"""
|
| 562 |
+
with open(embeddings_cache_path, "w") as file:
|
| 563 |
+
json.dump(embeddings, file)
|
| 564 |
+
|
| 565 |
+
# بارگذاری امبدینگهای قبلی از کش (اگر موجود باشد)
|
| 566 |
+
book_embeddings = load_embeddings_from_cache()
|
| 567 |
+
|
| 568 |
if query:
|
| 569 |
found = False
|
| 570 |
threshold = 0.55
|
| 571 |
|
| 572 |
+
# بررسی اینکه آیا امبدینگهای سوال قبلاً محاسبه شده یا خیر
|
| 573 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 574 |
|
| 575 |
+
# اگر امبدینگهای کتاب در کش نباشند، آنها را محاسبه و ذخیره میکنیم
|
| 576 |
+
if not book_embeddings:
|
| 577 |
+
print("Emeddings for the book are not cached. Embedding the book now...")
|
| 578 |
+
# فرض کنید 'all_sentences' لیستی از جملات کتاب است
|
| 579 |
+
all_sentences_embeddings = model.encode(all_sentences, convert_to_tensor=True)
|
| 580 |
+
# ذخیره امبدینگهای کتاب در کش
|
| 581 |
+
book_embeddings = {idx: embedding.tolist() for idx, embedding in enumerate(all_sentences_embeddings)}
|
| 582 |
+
save_embeddings_to_cache(book_embeddings)
|
| 583 |
+
|
| 584 |
+
# محاسبه مشابهتها با استفاده از امبدینگهای کتاب
|
| 585 |
+
sentence_embeddings = [embedding for embedding in book_embeddings.values()]
|
| 586 |
similarities = util.pytorch_cos_sim(query_embedding, sentence_embeddings)[0]
|
| 587 |
|
| 588 |
for idx, similarity in enumerate(similarities):
|
|
|
|
| 596 |
{sentence}
|
| 597 |
پاسخ نهایی حرفهای بازنویسیشده:
|
| 598 |
"""
|
| 599 |
+
response = llm([SystemMessage(content="You are a helpful assistant."), HumanMessage(content=prompt)])
|
|
|
|
|
|
|
|
|
|
| 600 |
rewritten = response.content.strip()
|
| 601 |
|
| 602 |
# بررسی مرتبط بودن پاسخ با سؤال
|
|
|
|
| 607 |
پاسخ:
|
| 608 |
{rewritten}
|
| 609 |
"""
|
| 610 |
+
review_response = llm([SystemMessage(content="You are a helpful assistant."), HumanMessage(content=review_prompt)])
|
|
|
|
|
|
|
|
|
|
| 611 |
review_result = review_response.content.strip()
|
| 612 |
if "تأیید شد" in review_result:
|
| 613 |
rewritten = clean_text(rewritten)
|
|
|
|
| 619 |
سوال:
|
| 620 |
{query}
|
| 621 |
"""
|
| 622 |
+
new_response = llm([SystemMessage(content="You are a helpful assistant."), HumanMessage(content=final_prompt)])
|
|
|
|
|
|
|
|
|
|
| 623 |
final_answer = new_response.content.strip()
|
| 624 |
final_answer = clean_text(final_answer)
|
| 625 |
st.markdown(f'<div class="chat-message">{final_answer}</div>', unsafe_allow_html=True)
|
|
|
|
| 630 |
|
| 631 |
if not found:
|
| 632 |
prompt = f"لطفاً بر اساس سوال زیر یک متن مرتبط و معنادار تولید کن و جملات ساختگی استفاده نکن و از جملات موجود در اسناد و جملات معتبر استفاده کن که در ویکی پدیا موجود باشد و از زبانی جز فارسی استفاده نکن:\n\nسوال: {query}"
|
| 633 |
+
response = llm([SystemMessage(content="You are a helpful assistant."), HumanMessage(content=prompt)])
|
|
|
|
|
|
|
|
|
|
| 634 |
rewritten = response.content.strip()
|
| 635 |
rewritten = clean_text(rewritten)
|
| 636 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
| 637 |
+
think.empty()
|