Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

figure_parser.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_wraper(figures_data_without_positions):
  21. return [(
  22. (figure_data[1], [figure_data[0]]),
  23. [(0, 0, 0, 0, 0)]
  24. ) for figure_data in figures_data_without_positions if isinstance(figure_data[1], Image.Image)]
  25. shared_executor = ThreadPoolExecutor(max_workers=10)
  26. class VisionFigureParser:
  27. def __init__(self, vision_model, figures_data, *args, **kwargs):
  28. self.vision_model = vision_model
  29. self._extract_figures_info(figures_data)
  30. assert len(self.figures) == len(self.descriptions)
  31. assert not self.positions or (len(self.figures) == len(self.positions))
  32. def _extract_figures_info(self, figures_data):
  33. self.figures = []
  34. self.descriptions = []
  35. self.positions = []
  36. for item in figures_data:
  37. # position
  38. if len(item) == 2 and isinstance(item[1], list) and len(item[1]) == 1 and isinstance(item[1][0], tuple) and len(item[1][0]) == 5:
  39. img_desc = item[0]
  40. assert len(img_desc) == 2 and isinstance(img_desc[0], Image.Image) and isinstance(img_desc[1], list), "Should be (figure, [description])"
  41. self.figures.append(img_desc[0])
  42. self.descriptions.append(img_desc[1])
  43. self.positions.append(item[1])
  44. else:
  45. assert len(item) == 2 and isinstance(item, tuple) and isinstance(item[1], list), f"get {len(item)=}, {item=}"
  46. if isinstance(item[0], tuple) and len(item[0]) == 2 and isinstance(item[0][0], Image.Image) and isinstance(item[0][1], list):
  47. self.figures.append(item[0][0])
  48. self.descriptions.append(item[0][1])
  49. else:
  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