您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

figure_parser.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 rag.app.picture import vision_llm_chunk as picture_vision_llm_chunk
  17. from rag.prompts import vision_llm_figure_describe_prompt
  18. class VisionFigureParser:
  19. def __init__(self, vision_model, figures_data, *args, **kwargs):
  20. self.vision_model = vision_model
  21. self._extract_figures_info(figures_data)
  22. assert len(self.figures) == len(self.descriptions)
  23. assert not self.positions or (len(self.figures) == len(self.positions))
  24. def _extract_figures_info(self, figures_data):
  25. self.figures = []
  26. self.descriptions = []
  27. self.positions = []
  28. for item in figures_data:
  29. # position
  30. if len(item) == 2 and isinstance(item[1], list) and len(item[1]) == 1 and len(item[1][0]) == 5:
  31. img_desc = item[0]
  32. assert len(img_desc) == 2, "Should be (figure, [description])"
  33. self.figures.append(img_desc[0])
  34. self.descriptions.append(img_desc[1])
  35. self.positions.append(item[1])
  36. else:
  37. assert len(item) == 2 and isinstance(item, tuple), f"get {len(item)=}, {item=}"
  38. self.figures.append(item[0])
  39. self.descriptions.append(item[1])
  40. def _assemble(self):
  41. self.assembled = []
  42. self.has_positions = len(self.positions) != 0
  43. for i in range(len(self.figures)):
  44. figure = self.figures[i]
  45. desc = self.descriptions[i]
  46. pos = self.positions[i] if self.has_positions else None
  47. figure_desc = (figure, desc)
  48. if pos is not None:
  49. self.assembled.append((figure_desc, pos))
  50. else:
  51. self.assembled.append((figure_desc,))
  52. return self.assembled
  53. def __call__(self, **kwargs):
  54. callback = kwargs.get("callback", lambda prog, msg: None)
  55. for idx, img_binary in enumerate(self.figures or []):
  56. figure_num = idx # 0-based
  57. txt = picture_vision_llm_chunk(
  58. binary=img_binary,
  59. vision_model=self.vision_model,
  60. prompt=vision_llm_figure_describe_prompt(),
  61. callback=callback,
  62. )
  63. if txt:
  64. self.descriptions[figure_num] = txt + "\n".join(self.descriptions[figure_num])
  65. self._assemble()
  66. return self.assembled