ScreamingOpossum/openai_interaction/image_recognition_test.py

47 lines
1.7 KiB
Python
Raw Normal View History

from openai import OpenAI
client = OpenAI()
SUPPORTED_IMAGE_EXTENSIONS = ('.png', '.jpg', '.jpeg', '.webp', '.gif')
async def image_recognition(content: str, files: list[dict[str: str]]) -> str:
input_data = [{'type': 'input_text', 'text': content}]
input_data.extend(
[
{
'type': 'input_image',
'image_url': i_url.get('url'),
}
for i_url in files if i_url.get('filename').lower().endswith(SUPPORTED_IMAGE_EXTENSIONS)
]
)
response = client.responses.create(
model='gpt-4.1-mini',
input=[{
'role': 'user',
'content': input_data,
}],
)
return response.output_text
if __name__ == '__main__':
data = {
'content': 'What\'s on these images?',
'files': [
{
'filename': '2a264d9a62e7420586ee7873f53552d7.webp',
'url': 'https://cdn.discordapp.com/attachments/1381924995994882150/1388385230108360855/2a264d9a62e7420586ee7873f53552d7.webp?ex=6860ca01&is=685f7881&hm=e8e21d0829cc9be3a21f63cb8f0c5065bf76c1de3952dbb524640694dd5037be&',
},
{
'filename': 'u_312c5f5306d0913c58dd0639fe493bdd_800.png',
'url': 'https://cdn.discordapp.com/attachments/1381924995994882150/1388385230582321253/u_312c5f5306d0913c58dd0639fe493bdd_800.png?ex=6860ca01&is=685f7881&hm=d0ebcfaecc2e69551da0cdefdf88c3833f8cbd59d04b035b787ea3150382bdd2&',
},
{
'filename': 'shitty_pdf_file.pdf',
'url': 'https://example.com/shitty_pdf_file.pdf'
}
],
}
print(image_recognition(**data))