Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

figure_parser.py 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. self.figures.append(item[0])
  47. self.descriptions.append(item[1])
  48. def _assemble(self):
  49. self.assembled = []
  50. self.has_positions = len(self.positions) != 0
  51. for i in range(len(self.figures)):
  52. figure = self.figures[i]
  53. desc = self.descriptions[i]
  54. pos = self.positions[i] if self.has_positions else None
  55. figure_desc = (figure, desc)
  56. if pos is not None:
  57. self.assembled.append((figure_desc, pos))
  58. else:
  59. self.assembled.append((figure_desc,))
  60. return self.assembled
  61. def __call__(self, **kwargs):
  62. callback = kwargs.get("callback", lambda prog, msg: None)
  63. def process(figure_idx, figure_binary):
  64. description_text = picture_vision_llm_chunk(
  65. binary=figure_binary,
  66. vision_model=self.vision_model,
  67. prompt=vision_llm_figure_describe_prompt(),
  68. callback=callback,
  69. )
  70. return figure_idx, description_text
  71. futures = []
  72. for idx, img_binary in enumerate(self.figures or []):
  73. futures.append(shared_executor.submit(process, idx, img_binary))
  74. for future in as_completed(futures):
  75. figure_num, txt = future.result()
  76. if txt:
  77. self.descriptions[figure_num] = txt + "\n".join(self.descriptions[figure_num])
  78. self._assemble()
  79. return self.assembled