Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. #
  13. import io
  14. import numpy as np
  15. from PIL import Image
  16. from api.db import LLMType
  17. from api.db.services.llm_service import LLMBundle
  18. from rag.nlp import tokenize
  19. from deepdoc.vision import OCR
  20. ocr = OCR()
  21. def chunk(filename, binary, tenant_id, lang, callback=None, **kwargs):
  22. img = Image.open(io.BytesIO(binary)).convert('RGB')
  23. doc = {
  24. "docnm_kwd": filename,
  25. "image": img
  26. }
  27. bxs = ocr(np.array(img))
  28. txt = "\n".join([t[0] for _, t in bxs if t[0]])
  29. eng = lang.lower() == "english"
  30. callback(0.4, "Finish OCR: (%s ...)" % txt[:12])
  31. if (eng and len(txt.split()) > 32) or len(txt) > 32:
  32. tokenize(doc, txt, eng)
  33. callback(0.8, "OCR results is too long to use CV LLM.")
  34. return [doc]
  35. try:
  36. callback(0.4, "Use CV LLM to describe the picture.")
  37. cv_mdl = LLMBundle(tenant_id, LLMType.IMAGE2TEXT, lang=lang)
  38. img_binary = io.BytesIO()
  39. img.save(img_binary, format='JPEG')
  40. img_binary.seek(0)
  41. ans = cv_mdl.describe(img_binary.read())
  42. callback(0.8, "CV LLM respond: %s ..." % ans[:32])
  43. txt += "\n" + ans
  44. tokenize(doc, txt, eng)
  45. return [doc]
  46. except Exception as e:
  47. callback(prog=-1, msg=str(e))
  48. return []