MrSimple07 commited on
Commit
beb5d17
·
1 Parent(s): df58192

new code for showing chunks

Browse files
Files changed (1) hide show
  1. app.py +49 -17
app.py CHANGED
@@ -13,21 +13,21 @@ from config import (
13
 
14
  def create_chunks_display_html(chunk_info):
15
  if not chunk_info:
16
- return "<div style='padding: 20px; text-align: center;'>Нет данных о чанках</div>"
17
 
18
- html = "<div style='max-height: 500px; overflow-y: auto; padding: 10px;'>"
19
- html += f"<h4>Всего чанков: {len(chunk_info)}</h4>"
20
 
21
  for i, chunk in enumerate(chunk_info):
22
  bg_color = "#f8f9fa" if i % 2 == 0 else "#e9ecef"
23
  html += f"""
24
- <div style='background-color: {bg_color}; padding: 10px; margin: 5px 0; border-radius: 5px; border-left: 4px solid #007bff;'>
25
  <strong>Документ:</strong> {chunk['document_id']}<br>
26
  <strong>Раздел:</strong> {chunk['section_id']}<br>
27
- <strong>Чанк:</strong> {chunk['chunk_id']} | <strong>Размер:</strong> {chunk['chunk_size']} символов<br>
28
  <strong>Содержание:</strong><br>
29
- <div style='background-color: white; padding: 8px; margin-top: 5px; border-radius: 3px; font-family: monospace; font-size: 12px;'>
30
- {chunk['chunk_preview']}
31
  </div>
32
  </div>
33
  """
@@ -108,6 +108,46 @@ def switch_model(model_name, vector_index):
108
  log_message(error_msg)
109
  return None, f"❌ {error_msg}"
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  def create_demo_interface(answer_question_func, switch_model_func, current_model, chunk_info=None):
112
  with gr.Blocks(title="AIEXP - AI Expert для нормативной документации", theme=gr.themes.Soft()) as demo:
113
 
@@ -166,7 +206,7 @@ def create_demo_interface(answer_question_func, switch_model_func, current_model
166
  with gr.Column(scale=1):
167
  sources_output = gr.HTML(
168
  label="",
169
- value="<div style='background-color: #2d3748; color: white; padding: 20px; border-radius: 10px; text-align: center;'>Здесь появятся источники...</div>",
170
  )
171
 
172
  switch_btn.click(
@@ -186,17 +226,9 @@ def create_demo_interface(answer_question_func, switch_model_func, current_model
186
  inputs=[question_input],
187
  outputs=[answer_output, sources_output]
188
  )
189
-
190
- with gr.Tab("Просмотр чанков"):
191
- gr.Markdown("### Содержание обработанных чанков документов")
192
-
193
- chunks_display = gr.HTML(
194
- value=create_chunks_display_html(chunk_info),
195
- label="Информация о чанках"
196
- )
197
-
198
  return demo
199
 
 
200
  query_engine = None
201
  chunks_df = None
202
  reranker = None
 
13
 
14
  def create_chunks_display_html(chunk_info):
15
  if not chunk_info:
16
+ return "<div style='padding: 20px; text-align: center; color: black;'>Нет данных о чанках</div>"
17
 
18
+ html = "<div style='max-height: 500px; overflow-y: auto; padding: 10px; color: black;'>"
19
+ html += f"<h4 style='color: black;'>Найдено релевантных чанков: {len(chunk_info)}</h4>"
20
 
21
  for i, chunk in enumerate(chunk_info):
22
  bg_color = "#f8f9fa" if i % 2 == 0 else "#e9ecef"
23
  html += f"""
24
+ <div style='background-color: {bg_color}; padding: 10px; margin: 5px 0; border-radius: 5px; border-left: 4px solid #007bff; color: black;'>
25
  <strong>Документ:</strong> {chunk['document_id']}<br>
26
  <strong>Раздел:</strong> {chunk['section_id']}<br>
27
+ <strong>Ранг:</strong> {i+1} | <strong>Размер:</strong> {chunk['chunk_size']} символов<br>
28
  <strong>Содержание:</strong><br>
29
+ <div style='background-color: white; padding: 8px; margin-top: 5px; border-radius: 3px; font-family: monospace; font-size: 12px; color: black; max-height: 200px; overflow-y: auto;'>
30
+ {chunk['chunk_text']}
31
  </div>
32
  </div>
33
  """
 
108
  log_message(error_msg)
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
+ # Get response and retrieve nodes for reranking display
120
+ response = query_engine.query(question)
121
+
122
+ # Get source nodes from response
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
+
151
  def create_demo_interface(answer_question_func, switch_model_func, current_model, chunk_info=None):
152
  with gr.Blocks(title="AIEXP - AI Expert для нормативной документации", theme=gr.themes.Soft()) as demo:
153
 
 
206
  with gr.Column(scale=1):
207
  sources_output = gr.HTML(
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(
 
226
  inputs=[question_input],
227
  outputs=[answer_output, sources_output]
228
  )
 
 
 
 
 
 
 
 
 
229
  return demo
230
 
231
+
232
  query_engine = None
233
  chunks_df = None
234
  reranker = None