You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cv_model.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #
  2. # Copyright 2024 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 abc import ABC
  17. from openai import OpenAI
  18. import os
  19. import base64
  20. from io import BytesIO
  21. class Base(ABC):
  22. def __init__(self, key, model_name):
  23. pass
  24. def describe(self, image, max_tokens=300):
  25. raise NotImplementedError("Please implement encode method!")
  26. def image2base64(self, image):
  27. if isinstance(image, BytesIO):
  28. return base64.b64encode(image.getvalue()).decode("utf-8")
  29. buffered = BytesIO()
  30. try:
  31. image.save(buffered, format="JPEG")
  32. except Exception as e:
  33. image.save(buffered, format="PNG")
  34. return base64.b64encode(buffered.getvalue()).decode("utf-8")
  35. def prompt(self, b64):
  36. return [
  37. {
  38. "role": "user",
  39. "content": [
  40. {
  41. "type": "text",
  42. "text": "请用中文详细描述一下图中的内容,比如时间,地点,人物,事情,人物心情等。",
  43. },
  44. {
  45. "type": "image_url",
  46. "image_url": {
  47. "url": f"data:image/jpeg;base64,{b64}"
  48. },
  49. },
  50. ],
  51. }
  52. ]
  53. class GptV4(Base):
  54. def __init__(self, key, model_name="gpt-4-vision-preview"):
  55. self.client = OpenAI(key)
  56. self.model_name = model_name
  57. def describe(self, image, max_tokens=300):
  58. b64 = self.image2base64(image)
  59. res = self.client.chat.completions.create(
  60. model=self.model_name,
  61. messages=self.prompt(b64),
  62. max_tokens=max_tokens,
  63. )
  64. return res.choices[0].message.content.strip()
  65. class QWenCV(Base):
  66. def __init__(self, key, model_name="qwen-vl-chat-v1"):
  67. import dashscope
  68. dashscope.api_key = key
  69. self.model_name = model_name
  70. def describe(self, image, max_tokens=300):
  71. from http import HTTPStatus
  72. from dashscope import MultiModalConversation
  73. response = MultiModalConversation.call(model=self.model_name,
  74. messages=self.prompt(self.image2base64(image)))
  75. if response.status_code == HTTPStatus.OK:
  76. return response.output.choices[0]['message']['content']
  77. return response.message