Spaces:
Sleeping
Sleeping
Update helpers.py
Browse files- helpers.py +109 -5
helpers.py
CHANGED
@@ -19,6 +19,10 @@ import os
|
|
19 |
from langchain_docling import DoclingLoader#, ExportType
|
20 |
from langchain_docling.loader import ExportType
|
21 |
import logging
|
|
|
|
|
|
|
|
|
22 |
# logging.getLogger("langchain").setLevel(logging.ERROR)
|
23 |
logging.getLogger().setLevel(logging.ERROR)
|
24 |
|
@@ -297,9 +301,109 @@ def list_docx_files(folder_path):
|
|
297 |
return [str(file) for file in Path(folder_path).rglob("*.docx")]
|
298 |
|
299 |
def prompt_order(queries):
|
300 |
-
text = '
|
301 |
-
i = 0
|
302 |
for q in queries:
|
303 |
-
|
304 |
-
|
305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
from langchain_docling import DoclingLoader#, ExportType
|
20 |
from langchain_docling.loader import ExportType
|
21 |
import logging
|
22 |
+
from langchain.schema import Document
|
23 |
+
import re
|
24 |
+
import ast
|
25 |
+
|
26 |
# logging.getLogger("langchain").setLevel(logging.ERROR)
|
27 |
logging.getLogger().setLevel(logging.ERROR)
|
28 |
|
|
|
301 |
return [str(file) for file in Path(folder_path).rglob("*.docx")]
|
302 |
|
303 |
def prompt_order(queries):
|
304 |
+
text = 'Câu hỏi: '
|
|
|
305 |
for q in queries:
|
306 |
+
text += f'{str(q)}. '
|
307 |
+
return text
|
308 |
+
|
309 |
+
def update_documents_metadata(documents, new_metadata):
|
310 |
+
updated_documents = []
|
311 |
+
for doc in documents:
|
312 |
+
# Preserve the original 'source'
|
313 |
+
original_source = doc.metadata.get("source")
|
314 |
+
|
315 |
+
# Update metadata with new key-value pairs
|
316 |
+
doc.metadata.update(new_metadata)
|
317 |
+
|
318 |
+
# Ensure the 'source' remains unchanged
|
319 |
+
if original_source:
|
320 |
+
doc.metadata["source"] = original_source
|
321 |
+
|
322 |
+
updated_documents.append(doc)
|
323 |
+
return updated_documents
|
324 |
+
|
325 |
+
def extract_metadata(response):
|
326 |
+
if not isinstance(response, str):
|
327 |
+
response = str(response) # Chuyển sang string nếu cần
|
328 |
+
|
329 |
+
# Tìm tất cả các dictionary trong chuỗi đầu vào
|
330 |
+
matches = re.findall(r'\{.*?\}', response, re.DOTALL)
|
331 |
+
if not matches:
|
332 |
+
return None # Trả về None nếu không tìm thấy dict nào
|
333 |
+
|
334 |
+
smallest_dict = None
|
335 |
+
min_length = float("inf")
|
336 |
+
|
337 |
+
for match in matches:
|
338 |
+
try:
|
339 |
+
parsed_dict = ast.literal_eval(match) # Chuyển đổi string thành dictionary
|
340 |
+
if isinstance(parsed_dict, dict):
|
341 |
+
dict_length = len(str(parsed_dict)) # Độ dài chuỗi của dict
|
342 |
+
if dict_length < min_length:
|
343 |
+
smallest_dict = parsed_dict
|
344 |
+
min_length = dict_length
|
345 |
+
except Exception:
|
346 |
+
continue # Bỏ qua nếu không phải dictionary hợp lệ
|
347 |
+
|
348 |
+
return smallest_dict
|
349 |
+
|
350 |
+
def update_metadata(metadata, metadata_child):
|
351 |
+
for key, new_value in metadata_child.items():
|
352 |
+
if key in metadata:
|
353 |
+
# Nếu giá trị hiện tại không phải list, chuyển đổi thành list
|
354 |
+
if not isinstance(metadata[key], list):
|
355 |
+
metadata[key] = [metadata[key]]
|
356 |
+
# Nếu giá trị mới cũng là list thì thêm tất cả, ngược lại thêm từng phần tử
|
357 |
+
if isinstance(new_value, list):
|
358 |
+
metadata[key].extend(new_value)
|
359 |
+
else:
|
360 |
+
metadata[key].append(new_value)
|
361 |
+
else:
|
362 |
+
# Nếu key chưa có, tạo mới với giá trị được chuyển sang dạng list (nếu cần)
|
363 |
+
metadata[key] = new_value if isinstance(new_value, list) else [new_value]
|
364 |
+
return metadata
|
365 |
+
|
366 |
+
def define_metadata(input_text):
|
367 |
+
condition1 = 'Chương trình'
|
368 |
+
condition2 = 'Đề án'
|
369 |
+
condition3 = 'Đề cương'
|
370 |
+
condition4 = ['Trí tuệ nhân tạo',
|
371 |
+
'Toán kinh tế',
|
372 |
+
'Thống kê kinh tế',
|
373 |
+
'Phân tích dữ liệu trong Kinh tế',
|
374 |
+
'Kỹ thuật phần mềm',
|
375 |
+
'Khoa học máy tính',
|
376 |
+
'Khoa học dữ liệu',
|
377 |
+
'Hệ thống thông tin quản lý',
|
378 |
+
'Hệ thống thông tin',
|
379 |
+
'Định phí bảo hiểm và Quản trị rủi ro',
|
380 |
+
'Công nghệ thông tin',
|
381 |
+
'An toàn thông tin']
|
382 |
+
#cond1 cond2 la str, con3 la list ten cac nganh
|
383 |
+
result = {}
|
384 |
+
if condition3 in input_text:
|
385 |
+
result['Tai lieu ve'] = 'Đề cương'
|
386 |
+
elif condition1 in input_text:
|
387 |
+
result['Tai lieu ve'] = 'Chương trình đào tạo'
|
388 |
+
elif condition2 in input_text:
|
389 |
+
result['Tai lieu ve'] = 'Đề án'
|
390 |
+
for cond in condition4:
|
391 |
+
if cond in input_text:
|
392 |
+
if cond in ['An toàn thông tin', 'Công nghệ thông tin', 'Khoa học máy tính', 'Kỹ thuật phần mềm']:
|
393 |
+
result['Khoa'] = 'Công nghệ thông tin (FIT)'
|
394 |
+
elif cond in ['Toán kinh tế', 'Phân tích dữ liệu trong Kinh tế', 'Định phí bảo hiểm và Quản trị rủi ro']:
|
395 |
+
result['Khoa'] = 'Toán Kinh tế (MFE)'
|
396 |
+
if cond == 'Toán kinh tế':
|
397 |
+
cond == 'Toán kinh tế (TOKT)'
|
398 |
+
elif cond == 'Phân tích dữ liệu trong Kinh tế':
|
399 |
+
cond == 'Phân tích dữ liệu trong Kinh tế (DSEB)'
|
400 |
+
elif cond == 'Định phí bảo hiểm và Quản trị rủi ro':
|
401 |
+
cond == 'Định phí bảo hiểm và Quản trị rủi ro (Actuary)'
|
402 |
+
elif cond in ['Khoa h���c dữ liệu', 'Trí tuệ nhân tạo']:
|
403 |
+
result['Khoa'] = 'Khoa học dữ liệu và Trí tuệ nhân tạo (FDA)'
|
404 |
+
elif cond == 'Thống kê kinh tế':
|
405 |
+
result['Khoa'] = 'Thống kê'
|
406 |
+
elif cond in ['Hệ thống thông tin', 'Hệ thống thông tin quản lý']:
|
407 |
+
result['Khoa'] = 'Hệ thống thông tin quản lý (MIS)'
|
408 |
+
result['Nganh'] = cond
|
409 |
+
return result
|