zbparse/flask_app/货物标/提取采购需求main.py

57 lines
2.6 KiB
Python
Raw Normal View History

2024-09-23 15:49:30 +08:00
import concurrent.futures
2024-09-13 15:03:55 +08:00
import json
2024-09-23 15:49:30 +08:00
import time
2024-10-25 17:50:20 +08:00
from flask_app.货物标.技术参数要求提取 import get_technical_requirements
2024-10-22 10:06:22 +08:00
from flask_app.general.通义千问long import upload_file
2024-10-15 21:03:02 +08:00
from flask_app.货物标.商务服务其他要求提取 import get_business_requirements
2024-10-22 10:06:22 +08:00
2024-09-13 15:03:55 +08:00
#获取采购清单
2024-10-31 15:03:32 +08:00
def fetch_procurement_reqs(procurement_path,procurement_docpath):
2024-10-17 15:33:58 +08:00
# 定义默认的 procurement_reqs 字典
DEFAULT_PROCUREMENT_REQS = {
"技术要求": "",
"商务要求": "",
"服务要求": "",
"其他要求": ""
}
# 如果 truncate_file 是空字符串,直接返回包含空字符串的字典
2024-10-31 15:03:32 +08:00
if not procurement_docpath:
2024-10-17 15:33:58 +08:00
return DEFAULT_PROCUREMENT_REQS.copy()
try:
# 上传文件并获取 file_id
2024-10-31 15:03:32 +08:00
file_id = upload_file(procurement_docpath)
2024-10-17 15:33:58 +08:00
# 使用 ThreadPoolExecutor 并行处理 get_technical_requirements 和 get_business_requirements
with concurrent.futures.ThreadPoolExecutor() as executor:
# 提交任务给线程池
future_technical = executor.submit(get_technical_requirements, file_id)
time.sleep(1) # 如果需要延迟,可以保留,否则建议移除以提高效率
2024-10-31 15:03:32 +08:00
future_business = executor.submit(get_business_requirements, procurement_path, file_id)
2024-09-23 15:49:30 +08:00
2024-10-17 15:33:58 +08:00
# 获取并行任务的结果
technical_requirements = future_technical.result()
business_requirements = future_business.result()
2024-09-23 15:49:30 +08:00
2024-10-17 15:33:58 +08:00
# 构建最终的嵌套结构,确保四个键平级
procurement_reqs = {
2024-09-23 15:49:30 +08:00
"技术要求": technical_requirements.get("技术要求", {}),
"商务要求": business_requirements.get("商务要求", {}),
"服务要求": business_requirements.get("服务要求", {}),
2024-10-25 17:50:20 +08:00
"其他要求": business_requirements.get("其他要求", {}),
2024-10-17 15:33:58 +08:00
}
return procurement_reqs
2024-09-23 15:49:30 +08:00
2024-10-17 15:33:58 +08:00
except Exception as e:
print(f"Error in fetch_procurement_reqs: {e}")
# 在出错时返回默认的包含空字符串的字典
return DEFAULT_PROCUREMENT_REQS.copy()
2024-09-13 15:03:55 +08:00
if __name__ == "__main__":
output_folder = "C:\\Users\\Administrator\\Desktop\\货物标\\货物标output"
2024-10-15 20:57:58 +08:00
# file_path="C:\\Users\\Administrator\\Desktop\\货物标\\output1\\2-招标文件2020年广水市中小学教师办公电脑系统及多媒体“班班通”设备采购安装项目_procurement.pdf"
2024-10-31 15:03:32 +08:00
file_path="D:\\flask_project\\flask_app\\static\\output\\output1\\bf225a5e-16d0-45c8-8c19-54a1a94cf3e2\\ztbfile_procurement.docx"
2024-09-23 15:49:30 +08:00
res=fetch_procurement_reqs(file_path)
print(json.dumps(res, ensure_ascii=False, indent=4))