kgrag/app_pkg/examples/get_content_list.py
2026-07-29 18:10:19 +08:00

86 lines
3.1 KiB
Python

import json
import os
import httpx
async def process_pdf_logic(pdf_file):
"""
接收 PDF 文件并转发到解析服务
"""
# 验证文件类型
if not pdf_file.content_type.startswith("application/pdf"):
raise Exception(400, "仅支持 PDF 文件")
try:
# 读取文件内容
# file_content = await pdf_file.read()
# 配置目标接口参数
target_url = os.getenv("Mineru_URL","http://172.18.30.122:9988")
target_url = target_url + "/analyze-pdf"
# target_url = os.getenv("Mineru_URL","http://172.18.127.124:9088/pdf_parse")
# target_url = "http://172.18.30.122:8998/pdf_parse"
# params = {
# "parse_method": "auto",
# "is_json_md_dump": "true",
# "output_dir": "output"
# }
# 构建文件上传格式
files = {
"file": (pdf_file.filename, pdf_file.file, "application/pdf")
# "pdf_file": (pdf_file.filename, pdf_file.file, "application/pdf")
}
# 发送请求到解析服务
async with httpx.AsyncClient(timeout=1200.0) as client:
response = await client.post(
target_url,
# params=params,
files=files,
headers={"Accept": "application/json"}
)
response.raise_for_status()
result = response.json()
full_content = result.get("data", {}).get("full_content", "")
content_list = result.get("data", {}).get("content_list", [])
# markdown_content =""
# for content in content_list:
# text = content["text"]
return full_content,content_list
except httpx.RequestError as e:
raise Exception(503, f"无法连接到解析服务: {str(e)}")
except Exception as e:
raise Exception(500, f"服务器内部错误: {str(e)}")
import asyncio
from pathlib import Path
async def test_request(path):
pdf_path = Path(path)
async with httpx.AsyncClient() as client:
with open(pdf_path, "rb") as f:
files = {"file": (pdf_path.name, f, "application/pdf")}
response = await client.post(
"http://172.18.30.122:9988/analyze-pdf",
files=files,
timeout=120
)
result = response.json()
full_content = result.get("data", {}).get("full_content", "")
content_list = result.get("data", {}).get("content_list", [])
combined_json = ''.join(content_list)
content_data = json.loads(combined_json)
for item in content_data:
if isinstance(item, dict): # 确保是字典类型
text = item.get("text", "")
page_idx = item.get("page_idx", -1) # 默认值 -1 表示缺失
print(f"Text: {text}\nPage Index: {page_idx}\n{'-'*50}")
if __name__ == "__main__":
# asyncio.run(test_request("/Users/lijinxuan/PycharmProjects/Sea/AutoRag/src/examples/邱茜茜.pdf"))
asyncio.run(test_request("/Users/lijinxuan/PycharmProjects/Sea/AutoRag/src/examples/邱茜茜.pdf"))