diff --git a/flask_app/general/cwtest3.py b/flask_app/general/cwtest3.py new file mode 100644 index 0000000..28a9b9e --- /dev/null +++ b/flask_app/general/cwtest3.py @@ -0,0 +1,125 @@ +import PyPDF2 +import requests + + +from flask_app.general.clean_pdf import extract_common_header, clean_page_content + + +def extract_text_by_page(file_path): + common_header = extract_common_header(file_path) + # print(f"公共抬头:{common_header}") + # print("--------------------正文开始-------------------") + result = "" + with open(file_path, 'rb') as file: + reader = PyPDF2.PdfReader(file) + num_pages = len(reader.pages) + # print(f"Total pages: {num_pages}") + for page_num in range(num_pages): + page = reader.pages[page_num] + text = page.extract_text() + if text: + # print(f"--------第{page_num}页-----------") + cleaned_text = clean_page_content(text,common_header) + # print(cleaned_text) + result += cleaned_text + # print(f"Page {page_num + 1} Content:\n{cleaned_text}") + else: + print(f"Page {page_num + 1} is empty or text could not be extracted.") + return result + +def db_call_with_title(file_path): + # 相关参数 + url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions" + api_key = "ad0c363f-1f23-4b13-aba3-698a4f8c3eb8" + model_name = "ep-20241115114052-2clpd" # 豆包Pro 32k模型 + + # 取txt内容提问 + # with open(txt_path, 'r', encoding='utf-8') as f: + # full_text = f.read() + # print(len(full_text)) + + # 取pdf内容提问 + full_text_json = extract_text_json_by_page(file_path) + + # 大模型提取目录 + # query = f""" + # 任务:根据所提供的投标文件格式文件,将其中商务部分的目录准确提出。 + # 要求:1.尽可能保证与原文内容一致,不要进行总结归纳。 + # 2.如果文件中包含目录,其可能是虚假的目录,一切以正文内容为准。 + # 3.所提供文件可能包含技术标或其他内容,要求仅提取商务标内容。 + # 4.按所给出的格式输出,不要输出任何其他内容,目录保留正确的层级关系。 + # 输出格式:{{ + # "一级目录": {{ + # "二级目录": {{ + # "最内层目录": {{}} + # }} + # }} + # }} + # 文件内容:{full_text} + # """ + + # 大模型提取页码 + query = f""" + 任务:根据所提供的投标文件json格式文件,将其中商务部分的页码准确提出。 + 要求:1.所提供的json文件键为页码,值为当前页码的文本,充分理解后提取商务部分的内容页码。 + 2.如果文件中包含目录,其可能是虚假的目录,一切以正文内容为准。 + 3.所提供文件可能包含技术标或其他内容,要求仅提取商务标相关的页码。并且在最后将子目录的页码范围整合到"页码范围"中。 + 4.按所给出的格式输出,不要输出任何其他内容,目录保留正确的层级关系。 + 5.如果商务部分穿插如技术标的相关内容,在"商务部分"中的page_range中需要将这段跳过。 + 举例:假如12到15页为技术标部分,其他都是商务标时,按列表包含二元组的样式展示出来,如[(1,11),(16,33)]。 + 如果这种情况没有发生,则输出完整的页码范围,如[(4,20)] + 输出格式:{{ + "商务部分": {{ + "一级目录": {{ + start_page: 开始页码 + end_page: 结束页码 + children: {{ + "二级目录": {{ + start_page: 开始页码 + end_page: 结束页码 + children: {{ + "最内层目录": {{ + start_page: 开始页码 + end_page: 结束页码 + }} + }} + }} + }} + }} + }} + "页码范围": [(4,33)] + }} + 文件内容:{full_text_json} + """ + + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + api_key + } + data = { + "model": model_name, + "messages": [ + { + "role": "system", + "content": "你是一个专业的标书制作人,现在需要你对传入的数据进行充分理解后,回答我的问题。" + }, + { + "role": "user", + "content": query + } + ] + } + + response = requests.post(url, headers=headers, json=data) + + if response.status_code == 200: + print(response.json()["choices"][0]["message"]["content"]) + else: + print(f"请求失败,状态码:{response.status_code},错误信息:{response.text}") + + +if __name__ == "__main__": + txt_path = "D:/files/bid1/bid_format.txt" + pdf_path_1 = "D:/bid_generator/task_folder/9a447eb0-24b8-4f51-8164-d91a62edea25/tmp/bid_format.pdf" + pdf_path_2 = "D:/files/page_test/技术标穿插测试文件.pdf" + db_call_with_title(pdf_path_2) \ No newline at end of file diff --git a/flask_app/general/读取文件/按页读取pdf.py b/flask_app/general/读取文件/按页读取pdf.py index 64d1a86..1d54779 100644 --- a/flask_app/general/读取文件/按页读取pdf.py +++ b/flask_app/general/读取文件/按页读取pdf.py @@ -20,6 +20,29 @@ def extract_text_by_page(file_path): print(f"Page {page_num + 1} is empty or text could not be extracted.") return result + +def save_extracted_text_to_txt(pdf_path, txt_path): + """ + 调用 extract_text_by_page 函数提取PDF文本,并将其保存到指定的TXT文件中。 + + :param pdf_path: 输入的PDF文件路径 + :param txt_path: 输出的TXT文件路径 + """ + try: + # 提取文本内容 + extracted_text = extract_text_by_page(pdf_path) + + # 将提取的文本写入TXT文件 + with open(txt_path, 'w', encoding='utf-8') as txt_file: + txt_file.write(extracted_text) + + print(f"文本已成功提取并保存到 {txt_path}") + + except FileNotFoundError: + print(f"文件未找到: {pdf_path}") + except Exception as e: + print(f"发生错误: {e}") + # import fitz # PyMuPDF # import re # @@ -93,12 +116,13 @@ def extract_text_by_page(file_path): if __name__ == '__main__': # file_path='D:\\flask_project\\flask_app\\static\\output\\output1\\648e094b-e677-47ce-9073-09e0c82af210\\ztbfile_tobidders_notice_part2.pdf' - file_path=r'C:\Users\Administrator\Documents\WeChat Files\wxid_d11awe5rp1y722\FileStorage\File\2024-11\17F7BF97-1A4D-427D-81F2-5C9AD4B097DB_DF07_Flatten\17F7BF97-1A4D-427D-81F2-5C9AD4B097DB_DF07_Flatten_1-524.pdf' + file_path=r'C:\Users\Administrator\Desktop\货物标\output1\招标文件(实高电子显示屏)_procurement.pdf' # file_path = 'C:\\Users\\Administrator\\Desktop\\货物标\\output4\\磋商文件_tobidders_notice_part2.pdf' # file_path = 'C:\\Users\\Administrator\\Desktop\\货物标\\截取test\\交警支队机动车查验监管系统项目采购_tobidders_notice_part1.pdf' # file_path = "C:\\Users\\Administrator\\Desktop\\招标文件\\招标test文件夹\\zbtest8.pdf" # file_path="C:\\Users\\Administrator\\Desktop\\fsdownload\\45f650ce-e519-457b-9ad6-5840e2ede539\\ztbfile_procurement.pdf" # ress = extract_common_header(file_path) # print(ress) - res=extract_text_by_page(file_path) - # print(res)磋商文件_tobidders_notice_part2.pdf \ No newline at end of file + # res=extract_text_by_page(file_path) + # print(res)磋商文件_tobidders_notice_part2.pdf + save_extracted_text_to_txt(file_path,"output.txt") \ No newline at end of file diff --git a/flask_app/general/通义千问long.py b/flask_app/general/通义千问long.py index 8334b78..a19af39 100644 --- a/flask_app/general/通义千问long.py +++ b/flask_app/general/通义千问long.py @@ -1,3 +1,4 @@ +import json import random import time from pathlib import Path @@ -80,19 +81,18 @@ def qianwen_long_text(file_id, user_query): return completion.choices[0].message.content def qianwen_long_stream(file_id, user_query): - print("call qianwen-long text...") + print("调用 qianwen-long text...") """ - Uses a previously uploaded file to generate a response based on a user query. + 使用之前上传的文件,根据用户查询生成响应,并实时显示流式输出。 """ client = OpenAI( api_key=os.getenv("DASHSCOPE_API_KEY"), base_url="https://dashscope.aliyuncs.com/compatible-mode/v1" ) - # Generate a response based on the file ID + # 生成基于文件ID的响应 completion = client.chat.completions.create( model="qwen-long", - # top_p=0.5, temperature=0.5, messages=[ { @@ -104,29 +104,72 @@ def qianwen_long_stream(file_id, user_query): 'content': user_query } ], - stream=True + stream=True # 启用流式响应 ) - # Return the response content - return completion.choices[0].message.content + + full_response = "" # 用于存储完整的响应内容 + + try: + for chunk in completion: + # 假设chunk是一个对象,先将其转换为字典 + if hasattr(chunk, 'to_dict'): + chunk_data = chunk.to_dict() + else: + # 如果没有to_dict方法,尝试直接转换为JSON + chunk_data = json.loads(chunk.model_dump_json()) + # 检查是否有'choices'字段 + choices = chunk_data.get('choices', []) + if not choices: + continue # 如果没有choices,跳过当前chunk + choice = choices[0] # 获取第一个choice + delta = choice.get('delta', {}) + # 提取'content'字段 + content = delta.get('content', '') + if content: + # print(content, end='', flush=True) # 实时打印内容 + full_response += content + # 检查是否有结束条件 + if choice.get('finish_reason'): + break + except KeyboardInterrupt: + print("\n中断流式响应。") + except Exception as e: + print(f"\n处理流式响应时出错: {e}") + + print() # 换行 + return full_response # 返回完整的响应内容 if __name__ == "__main__": # Example file path - replace with your actual file path - file_path = "C:\\Users\\Administrator\\Desktop\\货物标\\output4\\招标文件111_tobidders_notice_part1.docx" + file_path = r"C:\Users\Administrator\Desktop\货物标\output1\陕西省公安厅交通警察总队高速公路交通安全智能感知巡查系统项目 (1)_procurement.pdf" file_id = upload_file(file_path) - - user_query1 = "该招标文件前附表中的项目名称是什么,请以json格式返回给我" - user_query2 = ("请提供文件中关于资格审查的具体内容和标准。") - start_time=time.time() - # First query - print("starting qianwen-long...") - result1 ,result2= qianwen_long(file_id, user_query1) - print("First Query Result:", result1) - print(type(result1)) - print(result2) + # + # user_query1 = "该招标文件前附表中的项目名称是什么,请以json格式返回给我" + # user_query2 = ("请提供文件中关于资格审查的具体内容和标准。") + # start_time=time.time() + # # First query + # print("starting qianwen-long...") + # result1 ,result2= qianwen_long(file_id, user_query1) + # print("First Query Result:", result1) + # print(type(result1)) + # print(result2) # # Second query # print("starting qianwen-long...") # result2 = qianwen_long(file_id, user_query2) # print("Second Query Result:", result2) # end_time=time.time() # print("elapsed time:"+str(end_time-start_time)) + + user_query = """ +请你根据该货物标中采购要求部分的内容,请你给出"高速公路交通安全智能感知巡查系统软件"的技术参数(或采购要求),请以json格式返回结果,键名为"高速公路交通安全智能感知巡查系统软件", 键值为一个列表,列表中包含若干描述\"{}\"的技术参数(或采购要求)的字符串,需与原文完全一致,即若技术参数前存在序号也要保留,但你不可擅自增添或删减。以下为需要考虑的特殊情况:如果该货物没有相关采购要求或技术参数要求,键值为空列表。示例输出格式如下: +{{ + "摄像机控制键盘": [ + "1、支持串行 RS232/RS422 和 IP 混合控制,允许在一个控制器上使用 RS232/RS422/IP 控制单个系统中的摄像机;", + "2、支持 2 组 RS422 串口 VISCA 协议菊花链控制 2x7 台摄像机。" + ] +}} + """ + response = qianwen_long_stream(file_id, user_query) + print("完整响应内容:") + print(response)