|
import requests |
|
import os |
|
|
|
from utils.operate_tx import * |
|
from utils.mama_utils import * |
|
import logging |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
logger.setLevel(logging.DEBUG) |
|
|
|
|
|
file_handler = logging.FileHandler('my_app.log') |
|
|
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
|
|
|
|
|
file_handler.setFormatter(formatter) |
|
|
|
|
|
logger.addHandler(file_handler) |
|
|
|
|
|
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')}") |
|
|
|
|
|
|
|
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)) |
|
|