File size: 2,684 Bytes
117b368
 
 
6806c9f
88e28b1
 
117b368
88e28b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88e28b1
 
117b368
88e28b1
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
import requests
import os

from utils.operate_tx import *
from utils.mama_utils import *
import logging

# Create a logger
logger = logging.getLogger(__name__)

# Set the logging level
logger.setLevel(logging.DEBUG)

# Create a file handler
file_handler = logging.FileHandler('my_app.log')

# Create a formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Add the formatter to the handler
file_handler.setFormatter(formatter)

# Add the handler to the logger
logger.addHandler(file_handler)

# AI评估人物图像(URL)
def eval_fashion_person(img_url):
    url = os.environ["LINKAI_HOST"]
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + os.environ["LINKAI_KEY"]
    }
    body = {
        "app_code": os.environ["LINKAI_CODE_2"],
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "识别图片中的人物,并根据知识库的知识必须给出评分和评分原因,其中,评分需要严格谨慎一点且评分原因必须包含所参考的知识的详细内容"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": img_url
                        }
                    }
                ]
            }
        ]
    }
    res = requests.post(url, json=body, headers=headers)
    if res.status_code == 200:
        reply_text = res.json().get("choices")[0]['message']['content']
        return reply_text
    else:
        error = res.json().get("error")
        print(f"请求异常, 错误码={res.status_code}, 错误类型={error.get('type')}, 错误信息={error.get('message')}")


# 从腾讯云获取图片url并评分
def eval_fashion_person_tx_url(img_name):
    person_name = img_name.split(".")[0]
    img_url = generate_tx_presigned_url(img_name)
    logger.debug(person_name, img_url)

    score, conclusion = get_score_conclusion(img_url)
    logger.debug(score, conclusion)
    result = {"username": person_name,
              "avatar_url": img_url,
              "fashion_score_predict": score,
              "fashion_eval_reason": conclusion
              }
    return result


# 获取分数和评语
def get_score_conclusion(img_url):
    conclusion = eval_fashion_person(img_url)
    score = extract_first_number(conclusion)
    return score, conclusion


if __name__ == '__main__':
    img_url = ''
    img_name = ''
    print(eval_fashion_person(img_url))
    print(eval_fashion_person_tx_url(img_name))