File size: 3,499 Bytes
7653bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import os
from openai import OpenAI
import base64


client = OpenAI(
    # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",
    api_key='sk-f3da437ff74841378d562b7d43c3a3f9', # 如何获取API Key:https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)


#  base 64 编码格式
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


def analysis_image(base64_image):
    completion = client.chat.completions.create(
        model="qwen2.5-vl-72b-instruct",
        messages=[{"role": "user","content": [
                {"type": "text","text": "请对这种广告图片进行创意解读。根据色彩、风格、场景、清晰度、美学、任务、文案、利益点等要素进行分析"},
                # {"type": "image_url",
                #  "image_url": {"url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"}},

                {
                        "type": "image_url",
                        # 需要注意,传入BASE64,图像格式(即image/{format})需要与支持的图片列表中的Content Type保持一致。"f"是字符串格式化的方法。
                        # PNG图像:  f"data:image/png;base64,{base64_image}"
                        # JPEG图像: f"data:image/jpeg;base64,{base64_image}"
                        # WEBP图像: f"data:image/webp;base64,{base64_image}"
                        "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
                    },
                ]}]
        )
    result = completion.model_dump_json()
    result = json.loads(result)
    message = result['choices'][0]['message']["content"]
    return message

def compare(text1, text2):
    prompt = f"下面是两个广告创意的解读。第一个广告的转化量很好。第二个广告的转化量很差。请分析较差的原因,并参考广告一给出广告二的优化建议。\n <广告一>是{text1}, \n <广告二>是{text2}"

    completion = client.chat.completions.create(
        model="qwen-max-0125",  # 此处以 deepseek-r1-distill-qwen-7b 为例,可按需更换模型名称。
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': prompt}
        ]
    )
    return completion.choices[0].message.content


if __name__ == '__main__':

    base64_image = encode_image("20250208153020.jpg")
    text1 = analysis_image(base64_image)
    print(text1)
    print("======")


    base64_image = encode_image("20250208153505.jpg")
    text2 = analysis_image(base64_image)
    # text2 = analysis_image("20250208153505.jpg")

    print(text2)
    print("======")
    prompt = f"下面是两个广告创意的解读。第一个广告的转化量很好。第二个广告的转化量很差。请分析较差的原因,并参考广告一给出广告二的优化建议。\n <广告一>是{text1}, \n <广告二>是{text2}"

    completion = client.chat.completions.create(
        model="qwen-max-0125",  # 此处以 deepseek-r1-distill-qwen-7b 为例,可按需更换模型名称。
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': prompt}
            ]
    )
    print(completion.choices[0].message.content)