Spaces:
Running
Running
Refactor app.py: Improve layout and organization of Knowledge Base management UI, enhance button functionality, and streamline information retrieval process.
Browse files
app.py
CHANGED
|
@@ -1191,163 +1191,162 @@ with gr.Blocks(css="""
|
|
| 1191 |
|
| 1192 |
clear_btn.click(clear_conversation, None, [chatbot, conversation_id])
|
| 1193 |
|
| 1194 |
-
|
| 1195 |
-
|
| 1196 |
with gr.Tab("Knowledge Base"):
|
| 1197 |
gr.Markdown("### Knowledge Base Management")
|
| 1198 |
|
| 1199 |
-
|
| 1200 |
-
|
| 1201 |
-
|
| 1202 |
-
|
| 1203 |
-
|
| 1204 |
-
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
|
| 1208 |
-
|
| 1209 |
-
|
| 1210 |
-
|
| 1211 |
-
|
| 1212 |
-
|
| 1213 |
-
|
| 1214 |
-
|
| 1215 |
-
# Статус операций с базой знаний
|
| 1216 |
-
kb_status = gr.Textbox(
|
| 1217 |
-
label="Operation Status",
|
| 1218 |
-
interactive=False,
|
| 1219 |
-
placeholder="Ready",
|
| 1220 |
-
value="Ready"
|
| 1221 |
-
)
|
| 1222 |
-
|
| 1223 |
-
# Кнопки для управления базой знаний
|
| 1224 |
-
with gr.Row():
|
| 1225 |
-
update_kb_btn = gr.Button("Update Knowledge Base", variant="primary")
|
| 1226 |
-
rebuild_kb_btn = gr.Button("Rebuild Knowledge Base from Scratch", variant="secondary")
|
| 1227 |
|
| 1228 |
-
|
| 1229 |
-
|
| 1230 |
-
|
| 1231 |
-
|
| 1232 |
-
|
| 1233 |
-
|
| 1234 |
-
|
| 1235 |
-
|
| 1236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1237 |
|
| 1238 |
-
|
| 1239 |
-
|
| 1240 |
-
|
| 1241 |
-
|
| 1242 |
-
# Функция для получения информации о базе знаний
|
| 1243 |
-
def get_kb_info() -> str:
|
| 1244 |
-
"""
|
| 1245 |
-
Get information about the current state of the knowledge base.
|
| 1246 |
|
| 1247 |
-
|
| 1248 |
-
|
| 1249 |
-
|
| 1250 |
-
|
| 1251 |
-
|
| 1252 |
-
|
| 1253 |
-
|
| 1254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1255 |
|
| 1256 |
-
|
|
|
|
|
|
|
| 1257 |
|
| 1258 |
-
|
|
|
|
|
|
|
| 1259 |
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
| 1263 |
-
|
| 1264 |
-
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
|
| 1272 |
-
|
| 1273 |
-
|
| 1274 |
-
|
| 1275 |
-
|
| 1276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1277 |
|
| 1278 |
-
**Documents**:
|
| 1279 |
|
| 1280 |
-
**
|
| 1281 |
|
| 1282 |
-
|
| 1283 |
"""
|
| 1284 |
-
|
| 1285 |
-
# Получаем файл с датой последнего обновления
|
| 1286 |
-
last_updated = "Unknown"
|
| 1287 |
-
try:
|
| 1288 |
-
from src.knowledge_base.dataset import DatasetManager
|
| 1289 |
-
dataset = DatasetManager()
|
| 1290 |
-
last_updated = dataset.get_last_update_date() or "Unknown"
|
| 1291 |
except Exception as e:
|
| 1292 |
-
|
| 1293 |
-
|
| 1294 |
-
|
| 1295 |
-
|
| 1296 |
-
|
| 1297 |
-
|
| 1298 |
-
|
| 1299 |
-
|
| 1300 |
-
|
| 1301 |
-
|
| 1302 |
-
"""
|
| 1303 |
-
|
| 1304 |
-
except Exception as e:
|
| 1305 |
-
return f"""
|
| 1306 |
-
**Status**: Error
|
| 1307 |
-
|
| 1308 |
-
**Details**: {str(e)}
|
| 1309 |
-
|
| 1310 |
-
Please try rebuilding the knowledge base.
|
| 1311 |
-
"""
|
| 1312 |
-
|
| 1313 |
-
kb_info = gr.Markdown(value=get_kb_info())
|
| 1314 |
-
refresh_kb_info_btn = gr.Button("Refresh Information")
|
| 1315 |
|
| 1316 |
-
|
| 1317 |
-
|
| 1318 |
|
| 1319 |
-
|
| 1320 |
-
|
| 1321 |
-
|
| 1322 |
-
|
| 1323 |
-
|
| 1324 |
-
|
| 1325 |
-
|
| 1326 |
-
|
| 1327 |
-
|
| 1328 |
-
|
| 1329 |
-
|
| 1330 |
-
|
| 1331 |
-
|
| 1332 |
-
|
| 1333 |
-
|
| 1334 |
-
|
| 1335 |
-
|
| 1336 |
-
|
| 1337 |
-
|
| 1338 |
-
|
| 1339 |
-
|
| 1340 |
-
|
| 1341 |
-
|
| 1342 |
-
|
| 1343 |
-
|
| 1344 |
-
|
| 1345 |
-
|
| 1346 |
-
|
| 1347 |
-
|
| 1348 |
-
|
| 1349 |
-
|
| 1350 |
-
|
| 1351 |
|
| 1352 |
with gr.Tab("Model Settings"):
|
| 1353 |
gr.Markdown("### Model Configuration")
|
|
|
|
| 1191 |
|
| 1192 |
clear_btn.click(clear_conversation, None, [chatbot, conversation_id])
|
| 1193 |
|
| 1194 |
+
|
|
|
|
| 1195 |
with gr.Tab("Knowledge Base"):
|
| 1196 |
gr.Markdown("### Knowledge Base Management")
|
| 1197 |
|
| 1198 |
+
with gr.Row():
|
| 1199 |
+
with gr.Column(scale=2):
|
| 1200 |
+
# Отображение источников
|
| 1201 |
+
gr.Markdown("#### Information Sources")
|
| 1202 |
+
sources_list = gr.Dataframe(
|
| 1203 |
+
value=pd.DataFrame({
|
| 1204 |
+
"URL": URLS,
|
| 1205 |
+
"Include": [True for _ in URLS],
|
| 1206 |
+
"Status": ["Ready" for _ in URLS]
|
| 1207 |
+
}),
|
| 1208 |
+
interactive=True,
|
| 1209 |
+
wrap=True,
|
| 1210 |
+
row_count=15,
|
| 1211 |
+
show_label=False
|
| 1212 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1213 |
|
| 1214 |
+
# Статус операций с базой знаний
|
| 1215 |
+
kb_status = gr.Textbox(
|
| 1216 |
+
label="Operation Status",
|
| 1217 |
+
interactive=False,
|
| 1218 |
+
placeholder="Ready",
|
| 1219 |
+
value="Ready"
|
| 1220 |
+
)
|
| 1221 |
+
|
| 1222 |
+
# Кнопки для управления базой знаний
|
| 1223 |
+
with gr.Row():
|
| 1224 |
+
update_kb_btn = gr.Button("Update Knowledge Base", variant="primary")
|
| 1225 |
+
rebuild_kb_btn = gr.Button("Rebuild Knowledge Base from Scratch", variant="secondary")
|
| 1226 |
+
|
| 1227 |
+
gr.Markdown("""
|
| 1228 |
+
<small>
|
| 1229 |
+
**Update Knowledge Base**: Adds new information to the existing knowledge base.
|
| 1230 |
+
|
| 1231 |
+
**Rebuild Knowledge Base**: Recreates the entire knowledge base from scratch. Use this if there are inconsistencies.
|
| 1232 |
+
|
| 1233 |
+
All changes are saved to the Hugging Face dataset.
|
| 1234 |
+
</small>
|
| 1235 |
+
""")
|
| 1236 |
|
| 1237 |
+
with gr.Column(scale=1):
|
| 1238 |
+
# Информация о текущей базе знаний
|
| 1239 |
+
gr.Markdown("#### Knowledge Base Information")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1240 |
|
| 1241 |
+
# Функция для получения информации о базе знаний
|
| 1242 |
+
def get_kb_info() -> str:
|
| 1243 |
+
"""
|
| 1244 |
+
Get information about the current state of the knowledge base.
|
| 1245 |
+
|
| 1246 |
+
Returns:
|
| 1247 |
+
str: Formatted markdown string containing knowledge base statistics
|
| 1248 |
+
"""
|
| 1249 |
+
try:
|
| 1250 |
+
vector_store = load_vector_store()
|
| 1251 |
+
if vector_store is None or isinstance(vector_store, str):
|
| 1252 |
+
return """
|
| 1253 |
+
**Status**: Not found or error
|
| 1254 |
+
|
| 1255 |
+
**Documents**: 0
|
| 1256 |
+
|
| 1257 |
+
**Last updated**: Never
|
| 1258 |
+
|
| 1259 |
+
Please create a knowledge base using the buttons on the left.
|
| 1260 |
+
"""
|
| 1261 |
|
| 1262 |
+
# Get information about vector store
|
| 1263 |
+
doc_count = len(vector_store.docstore._dict)
|
| 1264 |
+
sources = set()
|
| 1265 |
|
| 1266 |
+
for doc_id, doc in vector_store.docstore._dict.items():
|
| 1267 |
+
if hasattr(doc, 'metadata') and 'source' in doc.metadata:
|
| 1268 |
+
sources.add(doc.metadata['source'])
|
| 1269 |
|
| 1270 |
+
source_count = len(sources)
|
| 1271 |
+
|
| 1272 |
+
# Если хранилище существует, но источников нет
|
| 1273 |
+
if source_count == 0:
|
| 1274 |
+
return """
|
| 1275 |
+
**Status**: Created but empty
|
| 1276 |
+
|
| 1277 |
+
**Documents**: 0
|
| 1278 |
+
|
| 1279 |
+
**Last updated**: Unknown
|
| 1280 |
+
|
| 1281 |
+
Please rebuild the knowledge base using the button on the left.
|
| 1282 |
+
"""
|
| 1283 |
+
|
| 1284 |
+
# Получаем файл с датой последнего обновления
|
| 1285 |
+
last_updated = "Unknown"
|
| 1286 |
+
try:
|
| 1287 |
+
from src.knowledge_base.dataset import DatasetManager
|
| 1288 |
+
dataset = DatasetManager()
|
| 1289 |
+
last_updated = dataset.get_last_update_date() or "Unknown"
|
| 1290 |
+
except Exception as e:
|
| 1291 |
+
logger.error(f"Error getting last update date: {str(e)}")
|
| 1292 |
+
|
| 1293 |
+
return f"""
|
| 1294 |
+
**Status**: Active
|
| 1295 |
|
| 1296 |
+
**Documents**: {doc_count}
|
| 1297 |
|
| 1298 |
+
**Sources**: {source_count}
|
| 1299 |
|
| 1300 |
+
**Last updated**: {last_updated}
|
| 1301 |
"""
|
| 1302 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1303 |
except Exception as e:
|
| 1304 |
+
return f"""
|
| 1305 |
+
**Status**: Error
|
| 1306 |
+
|
| 1307 |
+
**Details**: {str(e)}
|
| 1308 |
+
|
| 1309 |
+
Please try rebuilding the knowledge base.
|
| 1310 |
+
"""
|
| 1311 |
+
|
| 1312 |
+
kb_info = gr.Markdown(value=get_kb_info())
|
| 1313 |
+
refresh_kb_info_btn = gr.Button("Refresh Information")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1314 |
|
| 1315 |
+
# 3. Добавим обработчики событий для кнопок в конце файла
|
| 1316 |
+
# Добавьте эти обработчики перед строкой "if __name__ == "__main__":"
|
| 1317 |
|
| 1318 |
+
# Обработчики для Knowledge Base
|
| 1319 |
+
update_kb_btn.click(
|
| 1320 |
+
fn=update_kb_with_selected,
|
| 1321 |
+
inputs=[sources_list],
|
| 1322 |
+
outputs=[kb_status]
|
| 1323 |
+
)
|
| 1324 |
+
|
| 1325 |
+
rebuild_kb_btn.click(
|
| 1326 |
+
fn=rebuild_kb_with_selected,
|
| 1327 |
+
inputs=[sources_list],
|
| 1328 |
+
outputs=[kb_status]
|
| 1329 |
+
)
|
| 1330 |
+
|
| 1331 |
+
# Обновление информации о базе знаний
|
| 1332 |
+
refresh_kb_info_btn.click(
|
| 1333 |
+
fn=get_kb_info,
|
| 1334 |
+
inputs=[],
|
| 1335 |
+
outputs=[kb_info]
|
| 1336 |
+
)
|
| 1337 |
+
|
| 1338 |
+
# Автоматическое обновление информации после операций с базой знаний
|
| 1339 |
+
update_kb_btn.click(
|
| 1340 |
+
fn=get_kb_info,
|
| 1341 |
+
inputs=[],
|
| 1342 |
+
outputs=[kb_info]
|
| 1343 |
+
)
|
| 1344 |
+
|
| 1345 |
+
rebuild_kb_btn.click(
|
| 1346 |
+
fn=get_kb_info,
|
| 1347 |
+
inputs=[],
|
| 1348 |
+
outputs=[kb_info]
|
| 1349 |
+
)
|
| 1350 |
|
| 1351 |
with gr.Tab("Model Settings"):
|
| 1352 |
gr.Markdown("### Model Configuration")
|