Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 5 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
import torch
|
| 7 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
|
|
| 8 |
|
| 9 |
def is_header(txt):
|
| 10 |
if not txt or len(txt) < 35:
|
|
@@ -75,6 +76,19 @@ def rut5_answer(question, context):
|
|
| 75 |
)
|
| 76 |
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def ask_chatbot(question):
|
| 79 |
question = question.strip()
|
| 80 |
if not question:
|
|
@@ -87,13 +101,12 @@ def ask_chatbot(question):
|
|
| 87 |
n_blocks = min(3, len(blocks))
|
| 88 |
if n_blocks == 0:
|
| 89 |
return "Ошибка: база знаний отсутствует или пуста."
|
| 90 |
-
# Корректная обработка индексов!
|
| 91 |
sorted_idxs = sims.argsort()[-n_blocks:][::-1]
|
| 92 |
-
sorted_idxs = [int(i) for i in sorted_idxs] # ЯВНО! Только числа!
|
| 93 |
context_blocks = []
|
| 94 |
-
for
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
context = " ".join(context_blocks)
|
| 98 |
# Ответ только из абзацев, не заголовков!
|
| 99 |
# Ищем наиболее релевантный "нормальный" блок
|
|
|
|
| 5 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
import torch
|
| 7 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 8 |
+
import numpy as np
|
| 9 |
|
| 10 |
def is_header(txt):
|
| 11 |
if not txt or len(txt) < 35:
|
|
|
|
| 76 |
)
|
| 77 |
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 78 |
|
| 79 |
+
def flatten_index(idx):
|
| 80 |
+
# idx может быть np.generic или np.ndarray
|
| 81 |
+
if isinstance(idx, (np.generic, np.integer)):
|
| 82 |
+
return int(idx)
|
| 83 |
+
if isinstance(idx, (list, tuple)) and idx and isinstance(idx, (int, np.generic, np.integer)):
|
| 84 |
+
return int(idx[0])
|
| 85 |
+
if hasattr(idx, "tolist"):
|
| 86 |
+
item = idx.tolist()
|
| 87 |
+
if isinstance(item, list):
|
| 88 |
+
return int(item)
|
| 89 |
+
return int(item)
|
| 90 |
+
return int(idx)
|
| 91 |
+
|
| 92 |
def ask_chatbot(question):
|
| 93 |
question = question.strip()
|
| 94 |
if not question:
|
|
|
|
| 101 |
n_blocks = min(3, len(blocks))
|
| 102 |
if n_blocks == 0:
|
| 103 |
return "Ошибка: база знаний отсутствует или пуста."
|
|
|
|
| 104 |
sorted_idxs = sims.argsort()[-n_blocks:][::-1]
|
|
|
|
| 105 |
context_blocks = []
|
| 106 |
+
for idx in sorted_idxs:
|
| 107 |
+
idx_int = flatten_index(idx)
|
| 108 |
+
if 0 <= idx_int < len(blocks):
|
| 109 |
+
context_blocks.append(blocks[idx_int])
|
| 110 |
context = " ".join(context_blocks)
|
| 111 |
# Ответ только из абзацев, не заголовков!
|
| 112 |
# Ищем наиболее релевантный "нормальный" блок
|