Wplotnikow commited on
Commit
5964260
·
verified ·
1 Parent(s): c4d8795

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -77,17 +77,20 @@ def rut5_answer(question, context):
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()
@@ -97,7 +100,7 @@ def ask_chatbot(question):
97
  return "Ошибка: база знаний пуста. Проверьте .docx и перезапустите Space."
98
 
99
  user_vec = vectorizer.transform([question.lower()])
100
- sims = cosine_similarity(user_vec, matrix)
101
  n_blocks = min(3, len(blocks))
102
  if n_blocks == 0:
103
  return "Ошибка: база знаний отсутствует или пуста."
@@ -105,11 +108,10 @@ def ask_chatbot(question):
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
- # Ищем наиболее релевантный "нормальный" блок
113
  best_normal_block = ""
114
  max_sim = -1
115
  for nb in normal_blocks:
 
77
  return tokenizer.decode(output_ids[0], skip_special_tokens=True)
78
 
79
  def flatten_index(idx):
80
+ # Универсальный способ из всего достать int
81
+ if isinstance(idx, (int, float, np.integer, np.floating)):
82
  return int(idx)
83
+ if isinstance(idx, (list, tuple, np.ndarray)):
84
+ if len(idx) == 0:
85
+ return 0
86
+ return flatten_index(idx)
87
  if hasattr(idx, "tolist"):
88
  item = idx.tolist()
89
+ return flatten_index(item)
90
+ try:
91
+ return int(idx)
92
+ except Exception:
93
+ return 0
94
 
95
  def ask_chatbot(question):
96
  question = question.strip()
 
100
  return "Ошибка: база знаний пуста. Проверьте .docx и перезапустите Space."
101
 
102
  user_vec = vectorizer.transform([question.lower()])
103
+ sims = cosine_similarity(user_vec, matrix)[0]
104
  n_blocks = min(3, len(blocks))
105
  if n_blocks == 0:
106
  return "Ошибка: база знаний отсутствует или пуста."
 
108
  context_blocks = []
109
  for idx in sorted_idxs:
110
  idx_int = flatten_index(idx)
111
+ if isinstance(idx_int, int) and 0 <= idx_int < len(blocks):
112
  context_blocks.append(blocks[idx_int])
113
  context = " ".join(context_blocks)
114
  # Ответ только из абзацев, не заголовков!
 
115
  best_normal_block = ""
116
  max_sim = -1
117
  for nb in normal_blocks: