Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 api.utils.api_utils import timeout
  19. from rag.app.picture import vision_llm_chunk as picture_vision_llm_chunk
  20. from rag.prompts import vision_llm_figure_describe_prompt
  21. def vision_figure_parser_figure_data_wrapper(figures_data_without_positions):
  22. return [
  23. (
  24. (figure_data[1], [figure_data[0]]),
  25. [(0, 0, 0, 0, 0)],
  26. )
  27. for figure_data in figures_data_without_positions
  28. if isinstance(figure_data[1], Image.Image)
  29. ]
  30. shared_executor = ThreadPoolExecutor(max_workers=10)
  31. class VisionFigureParser:
  32. def __init__(self, vision_model, figures_data, *args, **kwargs):
  33. self.vision_model = vision_model
  34. self._extract_figures_info(figures_data)
  35. assert len(self.figures) == len(self.descriptions)
  36. assert not self.positions or (len(self.figures) == len(self.positions))
  37. def _extract_figures_info(self, figures_data):
  38. self.figures = []
  39. self.descriptions = []
  40. self.positions = []
  41. for item in figures_data:
  42. # position
  43. 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:
  44. img_desc = item[0]
  45. assert len(img_desc) == 2 and isinstance(img_desc[0], Image.Image) and isinstance(img_desc[1], list), "Should be (figure, [description])"
  46. self.figures.append(img_desc[0])
  47. self.descriptions.append(img_desc[1])
  48. self.positions.append(item[1])
  49. else:
  50. 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=}"
  51. self.figures.append(item[0])
  52. self.descriptions.append(item[1])
  53. def _assemble(self):
  54. self.assembled = []
  55. self.has_positions = len(self.positions) != 0
  56. for i in range(len(self.figures)):
  57. figure = self.figures[i]
  58. desc = self.descriptions[i]
  59. pos = self.positions[i] if self.has_positions else None
  60. figure_desc = (figure, desc)
  61. if pos is not None:
  62. self.assembled.append((figure_desc, pos))
  63. else:
  64. self.assembled.append((figure_desc,))
  65. return self.assembled
  66. def __call__(self, **kwargs):
  67. callback = kwargs.get("callback", lambda prog, msg: None)
  68. @timeout(30, 3)
  69. def process(figure_idx, figure_binary):
  70. description_text = picture_vision_llm_chunk(
  71. binary=figure_binary,
  72. vision_model=self.vision_model,
  73. prompt=vision_llm_figure_describe_prompt(),
  74. callback=callback,
  75. )
  76. return figure_idx, description_text
  77. futures = []
  78. for idx, img_binary in enumerate(self.figures or []):
  79. futures.append(shared_executor.submit(process, idx, img_binary))
  80. for future in as_completed(futures):
  81. figure_num, txt = future.result()
  82. if txt:
  83. self.descriptions[figure_num] = txt + "\n".join(self.descriptions[figure_num])
  84. self._assemble()
  85. return self.assembled