Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

figure_parser.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from concurrent.futures import ThreadPoolExecutor, as_completed
  17. from PIL import Image
  18. from rag.app.picture import vision_llm_chunk as picture_vision_llm_chunk
  19. from rag.prompts import vision_llm_figure_describe_prompt
  20. def vision_figure_parser_figure_data_wrapper(figures_data_without_positions):
  21. return [
  22. (
  23. (figure_data[1], [figure_data[0]]),
  24. [(0, 0, 0, 0, 0)],
  25. )
  26. for figure_data in figures_data_without_positions
  27. if isinstance(figure_data[1], Image.Image)
  28. ]
  29. shared_executor = ThreadPoolExecutor(max_workers=10)
  30. class VisionFigureParser:
  31. def __init__(self, vision_model, figures_data, *args, **kwargs):
  32. self.vision_model = vision_model
  33. self._extract_figures_info(figures_data)
  34. assert len(self.figures) == len(self.descriptions)
  35. assert not self.positions or (len(self.figures) == len(self.positions))
  36. def _extract_figures_info(self, figures_data):
  37. self.figures = []
  38. self.descriptions = []
  39. self.positions = []
  40. for item in figures_data:
  41. # position
  42. if len(item) == 2 and isinstance(item[0], tuple) and len(item[0]) == 2 and isinstance(item[1], list) and isinstance(item[1][0], tuple) and len(item[1][0]) == 5:
  43. img_desc = item[0]
  44. assert len(img_desc) == 2 and isinstance(img_desc[0], Image.Image) and isinstance(img_desc[1], list), "Should be (figure, [description])"
  45. self.figures.append(img_desc[0])
  46. self.descriptions.append(img_desc[1])
  47. self.positions.append(item[1])
  48. else:
  49. assert len(item) == 2 and isinstance(item[0], Image.Image) and isinstance(item[1], list), f"Unexpected form of figure data: get {len(item)=}, {item=}"
  50. self.figures.append(item[0])
  51. self.descriptions.append(item[1])
  52. def _assemble(self):
  53. self.assembled = []
  54. self.has_positions = len(self.positions) != 0
  55. for i in range(len(self.figures)):
  56. figure = self.figures[i]
  57. desc = self.descriptions[i]
  58. pos = self.positions[i] if self.has_positions else None
  59. figure_desc = (figure, desc)
  60. if pos is not None:
  61. self.assembled.append((figure_desc, pos))
  62. else:
  63. self.assembled.append((figure_desc,))
  64. return self.assembled
  65. def __call__(self, **kwargs):
  66. callback = kwargs.get("callback", lambda prog, msg: None)
  67. def process(figure_idx, figure_binary):
  68. description_text = picture_vision_llm_chunk(
  69. binary=figure_binary,
  70. vision_model=self.vision_model,
  71. prompt=vision_llm_figure_describe_prompt(),
  72. callback=callback,
  73. )
  74. return figure_idx, description_text
  75. futures = []
  76. for idx, img_binary in enumerate(self.figures or []):
  77. futures.append(shared_executor.submit(process, idx, img_binary))
  78. for future in as_completed(futures):
  79. figure_num, txt = future.result()
  80. if txt:
  81. self.descriptions[figure_num] = txt + "\n".join(self.descriptions[figure_num])
  82. self._assemble()
  83. return self.assembled