Update app.py
Browse files
app.py
CHANGED
@@ -547,9 +547,17 @@ def find_closest_filenames(query, filenames, top_n=3):
|
|
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]
|
@@ -557,8 +565,16 @@ def find_best_answer(query, top_files, doc_texts):
|
|
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:
|
|
|
547 |
scores.sort(key=lambda x: x[1], reverse=True)
|
548 |
return [score[0] for score in scores[:top_n]]
|
549 |
|
550 |
+
from transformers import pipeline
|
551 |
+
|
552 |
+
def summarize_text(text):
|
553 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
554 |
+
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
|
555 |
+
return summary[0]['summary_text']
|
556 |
+
|
557 |
def find_best_answer(query, top_files, doc_texts):
|
558 |
best_match = None
|
559 |
best_score = 0
|
560 |
+
best_text = ""
|
561 |
|
562 |
for filename in top_files:
|
563 |
text = doc_texts[filename]
|
|
|
565 |
if similarity > best_score:
|
566 |
best_score = similarity
|
567 |
best_match = filename
|
568 |
+
best_text = text
|
569 |
+
|
570 |
+
# خلاصه کردن متن برای خروجی بهتر
|
571 |
+
if best_text:
|
572 |
+
summary = summarize_text(best_text)
|
573 |
+
else:
|
574 |
+
summary = "متنی برای خلاصهسازی پیدا نشد."
|
575 |
+
|
576 |
+
return summary
|
577 |
|
|
|
578 |
|
579 |
# حالا این رو در کد اصلی استفاده میکنیم:
|
580 |
if query:
|