read_api.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. read_api
  2. =========
  3. 从文件或目录读取内容以创建 Dataset。目前,我们提供了几个覆盖某些场景的函数。如果你有新的、大多数用户都会遇到的场景,可以在官方 GitHub 问题页面上发布详细描述。同时,实现你自己的读取相关函数也非常容易。
  4. 重要函数
  5. ---------
  6. read_jsonl
  7. ^^^^^^^^^^^^^^^^
  8. 从本地机器或远程 S3 上的 JSONL 文件读取内容。如果你想了解更多关于 JSONL 的信息,请参阅 :doc:`../../additional_notes/glossary`。
  9. .. code:: python
  10. from magic_pdf.data.read_api import *
  11. from magic_pdf.data.data_reader_writer import MultiBucketS3DataReader
  12. from magic_pdf.data.schemas import S3Config
  13. # 读取本地 jsonl 文件
  14. datasets = read_jsonl("tt.jsonl", None) # 替换为有效的文件
  15. # 读取 s3 jsonl 文件
  16. bucket = "bucket_1" # 替换为有效的 s3 bucket
  17. ak = "access_key_1" # 替换为有效的 s3 access key
  18. sk = "secret_key_1" # 替换为有效的 s3 secret key
  19. endpoint_url = "endpoint_url_1" # 替换为有效的 s3 endpoint url
  20. bucket_2 = "bucket_2" # 替换为有效的 s3 bucket
  21. ak_2 = "access_key_2" # 替换为有效的 s3 access key
  22. sk_2 = "secret_key_2" # 替换为有效的 s3 secret key
  23. endpoint_url_2 = "endpoint_url_2" # 替换为有效的 s3 endpoint url
  24. s3configs = [
  25. S3Config(
  26. bucket_name=bucket, access_key=ak, secret_key=sk, endpoint_url=endpoint_url
  27. ),
  28. S3Config(
  29. bucket_name=bucket_2,
  30. access_key=ak_2,
  31. secret_key=sk_2,
  32. endpoint_url=endpoint_url_2,
  33. ),
  34. ]
  35. s3_reader = MultiBucketS3DataReader(bucket, s3configs)
  36. datasets = read_jsonl(f"s3://bucket_1/tt.jsonl", s3_reader) # 替换为有效的 s3 jsonl file
  37. read_local_pdfs
  38. ^^^^^^^^^^^^^^^^
  39. 从路径或目录读取 PDF 文件。
  40. .. code:: python
  41. from magic_pdf.data.read_api import *
  42. # 读取 PDF 路径
  43. datasets = read_local_pdfs("tt.pdf") # 替换为有效的文件
  44. # 读取目录下的 PDF 文件
  45. datasets = read_local_pdfs("pdfs/") # 替换为有效的文件目录
  46. read_local_images
  47. ^^^^^^^^^^^^^^^^^^^
  48. 从路径或目录读取图像。
  49. .. code:: python
  50. from magic_pdf.data.read_api import *
  51. # 从图像路径读取
  52. datasets = read_local_images("tt.png") # 替换为有效的文件
  53. # 从目录读取以 suffixes 数组中指定后缀结尾的文件
  54. datasets = read_local_images("images/", suffixes=["png", "jpg"]) # 替换为有效的文件目录