赞
踩
极客时间《AI 大模型应用开发实战营》
最近在极客时间学习《AI 大模型应用开发实战营》,通过输入提示词让chatgpt完成了简单的pdf上传、下载的前端页面.目前上传英文pdf文档后,服务器后台会解析pdf文档,并翻译成中文pdf文档,可以下载下来。
prompt提示词内容如下:‘帮我生成上传、下载pdf文件的前端页面,表单请求触发,后端上传路由是localhost:5000/upload,下载路由是localhost:5000/download/<filename>,上传文件和下载文件文件名是相同的’。页面截图如下:
web接口开发采用的是flask框架,接口开发代码,也是chatgpt自动生成的。提示词如下:’请用flask框架 写上传pdf和下载pdf的接口文件’。chatgpt自动生成的代码还需要修改,我在它生成的代码文件中添加ip和port的设置,并将翻译功能的主要代码添加进upload_file函数接口中,最终形成的代码文件如下:
from flask import Flask, request, send_file import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__))) from utils import ArgumentParser, ConfigLoader, LOG from model import GLMModel, OpenAIModel from translator import PDFTranslator app = Flask(__name__) # 设置上传文件存储目录 UPLOAD_FOLDER = 'uploads' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # 确保上传文件存储目录存在 if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) @app.route('/upload', methods=['POST']) def upload_file(): if 'file' not in request.files: return 'No file part' file = request.files['file'] if file.filename == '': return 'No selected file' if file: filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) file.save(filename) argument_parser = ArgumentParser() args = argument_parser.parse_arguments() config_loader = ConfigLoader(args.config) config = config_loader.load_config() model_name = args.openai_model if args.openai_model else config['OpenAIModel']['model'] api_key = args.openai_api_key if args.openai_api_key else config['OpenAIModel']['api_key'] model = OpenAIModel(model=model_name, api_key=api_key) pdf_file_path = filename file_format = args.file_format if args.file_format else config['common']['file_format'] # 实例化 PDFTranslator 类,并调用 translate_pdf() 方法 translator = PDFTranslator(model) translator.translate_pdf(pdf_file_path, file_format) return 'File uploaded successfully' @app.route('/download/<filename>', methods=['GET']) def download_file(filename): file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) if os.path.exists(file_path): return send_file(file_path, as_attachment=True) else: return 'File not found' if __name__ == '__main__': ip='127.0.0.1' post=5000 app.run(ip=ip,post=post)
在pdf的解析函数中,我先把一个page文件中的所有表格提取出来,使用pdfplumber包提供的crop方法将表格和文字全部剪切成各自部分的文件,并存入切片数组中,再从切片数组中提取各部分文件,最后按照文件类型提取文字和表格存入page类中。pdf解析的代码函数如下:
def parse_pdf(self, pdf_file_path: str, pages: Optional[int] = None) -> Book: book = Book(pdf_file_path) with pdfplumber.open(pdf_file_path) as pdf: if pages is not None and pages > len(pdf.pages): raise PageOutOfRangeException(len(pdf.pages), pages) if pages is None: pages_to_parse = pdf.pages else: pages_to_parse = pdf.pages[:pages] for pdf_page in pages_to_parse: page = Page() # Store the original text content tables=pdf_page.find_tables(table_settings={}) # Remove each cell's content from the original text content = [] for i in range(len(tables) - 1): table_page = pdf_page.crop(tables[i].bbox, relative=False, strict=True) content.append(('table',table_page.extract_table())) middle_page = pdf_page.crop((0, tables[i].bbox[-1], pdf_page.width, tables[i + 1].bbox[1]), relative=False, strict=True) content.append(('text',middle_page.extract_text())) if len(tables) > 0: init_page = pdf_page.crop((0, 0, pdf_page.width, tables[0].bbox[1]), relative=False, strict=True) init_text = init_page.extract_text() content.insert(0, ('text',init_text)) last_table_page = pdf_page.crop(tables[-1].bbox, relative=False, strict=True) content.append(('table',last_table_page.extract_table())) last_page = pdf_page.crop((0, tables[-1].bbox[-1], pdf_page.width, pdf_page.height), relative=False, strict=True) content.append(('text',last_page.extract_text())) else: content.append(('text',pdf_page.extract_text())) # Handling text for item_type,item in content: # Remove empty lines and leading/trailing whitespaces if item_type=='text' and item: raw_text_lines = item.splitlines() cleaned_raw_text_lines = [line.strip() for line in raw_text_lines if line.strip()] cleaned_raw_text = "\n".join(cleaned_raw_text_lines) text_content = Content(content_type=ContentType.TEXT, original=cleaned_raw_text) page.add_content(text_content) LOG.debug(f"[raw_text]\n {cleaned_raw_text}") if item_type == 'table' and item: table = TableContent(tables) page.add_content(table) LOG.debug(f"[table]\n{table}") book.add_page(page) return book
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。