Spaces:
Sleeping
Sleeping
Commit
·
52f7579
1
Parent(s):
beb5d17
fixed chunking showring problem
Browse files
app.py
CHANGED
|
@@ -109,42 +109,22 @@ def switch_model(model_name, vector_index):
|
|
| 109 |
return None, f"❌ {error_msg}"
|
| 110 |
|
| 111 |
def main_answer_question(question):
|
| 112 |
-
from index_retriever import rerank_nodes
|
| 113 |
global query_engine, reranker, current_model, chunks_df
|
| 114 |
if not question.strip():
|
| 115 |
-
return ("<div style='color: black;'>Пожалуйста, введите вопрос</div>",
|
| 116 |
-
"<div style='color: black;'>Источники появятся после обработки запроса</div>"
|
| 117 |
-
|
|
|
|
| 118 |
try:
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
source_nodes = response.source_nodes if hasattr(response, 'source_nodes') else []
|
| 124 |
-
|
| 125 |
-
# Rerank the nodes
|
| 126 |
-
reranked_nodes = rerank_nodes(question, source_nodes, reranker, top_k=10)
|
| 127 |
-
|
| 128 |
-
# Create chunk info for display
|
| 129 |
-
chunk_info = []
|
| 130 |
-
for node in reranked_nodes:
|
| 131 |
-
chunk_info.append({
|
| 132 |
-
'document_id': node.metadata.get('document_id', 'unknown'),
|
| 133 |
-
'section_id': node.metadata.get('section_id', 'unknown'),
|
| 134 |
-
'chunk_size': len(node.text),
|
| 135 |
-
'chunk_text': node.text
|
| 136 |
-
})
|
| 137 |
-
|
| 138 |
-
# Format response
|
| 139 |
-
answer_html = f"<div style='color: black; padding: 15px; background-color: #f8f9fa; border-radius: 8px;'>{str(response)}</div>"
|
| 140 |
-
sources_html = create_chunks_display_html(chunk_info)
|
| 141 |
-
|
| 142 |
-
return answer_html, sources_html
|
| 143 |
-
|
| 144 |
except Exception as e:
|
| 145 |
log_message(f"Ошибка при ответе на вопрос: {str(e)}")
|
| 146 |
-
return (f"<div style='color: red;'>Ошибка: {str(e)}</div>",
|
| 147 |
-
"<div style='color: black;'>Источники недоступны из-за ошибки</div>"
|
|
|
|
| 148 |
|
| 149 |
|
| 150 |
|
|
@@ -208,6 +188,12 @@ def create_demo_interface(answer_question_func, switch_model_func, current_model
|
|
| 208 |
label="",
|
| 209 |
value="<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; text-align: center;'>Здесь появятся релевантные чанки...</div>",
|
| 210 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
switch_btn.click(
|
| 213 |
fn=switch_model_func,
|
|
@@ -218,13 +204,13 @@ def create_demo_interface(answer_question_func, switch_model_func, current_model
|
|
| 218 |
ask_btn.click(
|
| 219 |
fn=answer_question_func,
|
| 220 |
inputs=[question_input],
|
| 221 |
-
outputs=[answer_output, sources_output]
|
| 222 |
)
|
| 223 |
|
| 224 |
question_input.submit(
|
| 225 |
fn=answer_question_func,
|
| 226 |
inputs=[question_input],
|
| 227 |
-
outputs=[answer_output, sources_output]
|
| 228 |
)
|
| 229 |
return demo
|
| 230 |
|
|
@@ -237,7 +223,10 @@ current_model = DEFAULT_MODEL
|
|
| 237 |
|
| 238 |
def main_answer_question(question):
|
| 239 |
global query_engine, reranker, current_model, chunks_df
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
def main_switch_model(model_name):
|
| 243 |
global query_engine, vector_index, current_model
|
|
|
|
| 109 |
return None, f"❌ {error_msg}"
|
| 110 |
|
| 111 |
def main_answer_question(question):
|
|
|
|
| 112 |
global query_engine, reranker, current_model, chunks_df
|
| 113 |
if not question.strip():
|
| 114 |
+
return ("<div style='color: black;'>Пожалуйста, введите вопрос</div>",
|
| 115 |
+
"<div style='color: black;'>Источники появятся после обработки запроса</div>",
|
| 116 |
+
"<div style='color: black;'>Чанки появятся после обработки запроса</div>")
|
| 117 |
+
|
| 118 |
try:
|
| 119 |
+
# Call the answer_question function which returns 3 values
|
| 120 |
+
answer_html, sources_html, chunks_html = answer_question(question, query_engine, reranker, current_model, chunks_df)
|
| 121 |
+
return answer_html, sources_html, chunks_html
|
| 122 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
log_message(f"Ошибка при ответе на вопрос: {str(e)}")
|
| 125 |
+
return (f"<div style='color: red;'>Ошибка: {str(e)}</div>",
|
| 126 |
+
"<div style='color: black;'>Источники недоступны из-за ошибки</div>",
|
| 127 |
+
"<div style='color: black;'>Чанки недоступны из-за ошибки</div>")
|
| 128 |
|
| 129 |
|
| 130 |
|
|
|
|
| 188 |
label="",
|
| 189 |
value="<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; text-align: center;'>Здесь появятся релевантные чанки...</div>",
|
| 190 |
)
|
| 191 |
+
|
| 192 |
+
with gr.Column(scale=1):
|
| 193 |
+
chunks_output = gr.HTML(
|
| 194 |
+
label="Релевантные чанки",
|
| 195 |
+
value="<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; text-align: center;'>Здесь появятся релевантные чанки...</div>",
|
| 196 |
+
)
|
| 197 |
|
| 198 |
switch_btn.click(
|
| 199 |
fn=switch_model_func,
|
|
|
|
| 204 |
ask_btn.click(
|
| 205 |
fn=answer_question_func,
|
| 206 |
inputs=[question_input],
|
| 207 |
+
outputs=[answer_output, sources_output, chunks_output]
|
| 208 |
)
|
| 209 |
|
| 210 |
question_input.submit(
|
| 211 |
fn=answer_question_func,
|
| 212 |
inputs=[question_input],
|
| 213 |
+
outputs=[answer_output, sources_output, chunks_output]
|
| 214 |
)
|
| 215 |
return demo
|
| 216 |
|
|
|
|
| 223 |
|
| 224 |
def main_answer_question(question):
|
| 225 |
global query_engine, reranker, current_model, chunks_df
|
| 226 |
+
answer_html, sources_html, chunks_html = answer_question(
|
| 227 |
+
question, query_engine, reranker, current_model, chunks_df
|
| 228 |
+
)
|
| 229 |
+
return answer_html, sources_html, chunks_html
|
| 230 |
|
| 231 |
def main_switch_model(model_name):
|
| 232 |
global query_engine, vector_index, current_model
|
utils.py
CHANGED
|
@@ -186,7 +186,20 @@ def answer_question(question, query_engine, reranker, current_model, chunks_df=N
|
|
| 186 |
</div>
|
| 187 |
</div>"""
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
except Exception as e:
|
| 192 |
log_message(f"Ошибка обработки вопроса: {str(e)}")
|
|
|
|
| 186 |
</div>
|
| 187 |
</div>"""
|
| 188 |
|
| 189 |
+
# Релевантные чанки (text snippets)
|
| 190 |
+
chunk_info = []
|
| 191 |
+
for node in reranked_nodes:
|
| 192 |
+
chunk_info.append({
|
| 193 |
+
'document_id': node.metadata.get('document_id', 'unknown'),
|
| 194 |
+
'section_id': node.metadata.get('section_id', 'unknown'),
|
| 195 |
+
'chunk_size': len(node.text),
|
| 196 |
+
'chunk_text': node.text
|
| 197 |
+
})
|
| 198 |
+
from app import create_chunks_display_html
|
| 199 |
+
chunks_html = create_chunks_display_html(chunk_info)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
return answer_with_time, sources_html, chunks_html
|
| 203 |
|
| 204 |
except Exception as e:
|
| 205 |
log_message(f"Ошибка обработки вопроса: {str(e)}")
|