45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import os
|
|
from typing import Optional
|
|
from dashscope import MultiModalConversation
|
|
from models.logger import setup_logger
|
|
|
|
logger = setup_logger()
|
|
|
|
class Generator:
|
|
def __init__(self, api_key: str, model: str = 'qwen3-vl-plus'):
|
|
self.api_key = api_key
|
|
self.model = model
|
|
|
|
def process_single_image(self, image_path: str) -> Optional[str]:
|
|
"""处理单张图片,生成描述词"""
|
|
try:
|
|
image_url = f"file://{image_path}"
|
|
messages = [
|
|
{
|
|
'role': 'user',
|
|
'content': [
|
|
{'image': image_url},
|
|
{'text': '使用简洁语言描述该表情包 ,例如 在吗 ,生气,?'}
|
|
]
|
|
}
|
|
]
|
|
|
|
response = MultiModalConversation.call(
|
|
model=self.model,
|
|
messages=messages
|
|
)
|
|
|
|
if response and hasattr(response, 'output'):
|
|
if hasattr(response.output.choices[0].message.content[0], 'text'):
|
|
return response.output.choices[0].message.content[0]["text"]
|
|
elif isinstance(response.output.choices[0].message.content[0], dict):
|
|
return response.output.choices[0].message.content[0].get("text", "")
|
|
|
|
return None
|
|
|
|
except Exception as e:
|
|
logger.error(f"API调用失败: {e}")
|
|
return None
|
|
|
|
|