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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import io
  17. import numpy as np
  18. from PIL import Image
  19. from api.db import LLMType
  20. from api.db.services.llm_service import LLMBundle
  21. from rag.nlp import tokenize
  22. from deepdoc.vision import OCR
  23. ocr = OCR()
  24. def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):
  25. img = Image.open(io.BytesIO(binary)).convert('RGB')
  26. doc = {
  27. "docnm_kwd": filename,
  28. "image": img
  29. }
  30. bxs = ocr(np.array(img))
  31. txt = "\n".join([t[0] for _, t in bxs if t[0]])
  32. eng = lang.lower() == "english"
  33. callback(0.4, "Finish OCR: (%s ...)" % txt[:12])
  34. if (eng and len(txt.split()) > 32) or len(txt) > 32:
  35. tokenize(doc, txt, eng)
  36. callback(0.8, "OCR results is too long to use CV LLM.")
  37. return [doc]
  38. try:
  39. callback(0.4, "Use CV LLM to describe the picture.")
  40. cv_mdl = LLMBundle(tenant_id, LLMType.IMAGE2TEXT, lang=lang)
  41. img_binary = io.BytesIO()
  42. img.save(img_binary, format='JPEG')
  43. img_binary.seek(0)
  44. ans = cv_mdl.describe(img_binary.read())
  45. callback(0.8, "CV LLM respond: %s ..." % ans[:32])
  46. txt += "\n" + ans
  47. tokenize(doc, txt, eng)
  48. return [doc]
  49. except Exception as e:
  50. callback(prog=-1, msg=str(e))
  51. return []