Created
February 6, 2025 07:20
-
-
Save sunnysab/3123fd55c2ba2a2441a11c7494800a1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import re | |
from typing import Optional | |
from openai import OpenAI | |
client = OpenAI(base_url='https://open.bigmodel.cn/api/paas/v4', api_key='') | |
def describe_image(prompt: str, image: bytes | str) -> Optional[str]: | |
""" 图像描述 """ | |
encoded_image = base64.b64encode(image).decode('utf-8') | |
response = client.chat.completions.create( | |
model='glm-4v-flash', | |
temperature=0.95, | |
top_p=0.70, | |
messages=[{'role': 'user', 'content': [ | |
{'type': 'image_url', 'image_url': {'url': encoded_image}}, | |
{'type': 'text', 'text': prompt}, | |
]}], | |
) | |
completion_message = response.choices[0].message | |
response_text: str = completion_message.content | |
response_text = re.sub(r'\s\S\n', '', response_text) | |
return response_text | |
if __name__ == '__main__': | |
prompt = '尽可能少的字数描述图片主体是什么, 里面物品有什么. 给人的感觉如何. 不要描述物品放置的目的.' | |
image = open('image/dog.jpg', 'rb').read() | |
description = describe_image(prompt, image) | |
print(description) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment