weijiang2024's picture
Upload folder using huggingface_hub
88e28b1 verified
import gradio as gr
from eval_fashion_person import *
from name_entity_recognition import *
from utils.operate_json 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)
def eval_image(input_image, progress=gr.Progress()):
if input_image is None:
raise gr.Error("未上传图片,请先上传图片")
progress((0, 3), desc="上传图片中")
logger.debug("上传图片中")
img_name = get_one_name()
input_image = output_to_binary(input_image)
save_tx(file_data=input_image,
file_name=img_name)
progress((1, 3), desc="评估分数中")
logger.debug("评估分数中")
data = eval_fashion_person_tx_url(img_name)
progress((2, 3), desc="识别类型中")
logger.debug("识别类型中")
reason = data['fashion_eval_reason']
score = data['fashion_score_predict']
logger.debug(data, reason, score)
score = ("未识别出" if score == -1 else score)
data = {}
while not data:
text = name_entity_recognition(reason)
data = text_to_json(text)
keys_to_check = [
'type',
'upper_garment',
'lower_garment',
'headgear',
'sock',
'shoe',
'accessory',
'backpack',
'action',
'countenance'
]
data = {k: ("未识别出" if data.get(k) is None else data[k]) for k in keys_to_check}
data = {k: ("未识别出" if v == "None" else v) for k, v in data.items()}
result = [reason,
score,
data['type'],
data['upper_garment'],
data['lower_garment'],
data['headgear'],
data['sock'],
data['shoe'],
data['accessory'],
data['backpack'],
data['action'],
data['countenance']]
return result
with gr.Blocks() as demo:
demo.title = "时尚买手小玲智能体" # Add title
demo.description = "上传一张人物图片,让时尚买手小玲为你评估穿搭!" # Add description
with gr.Row():
with gr.Column():
Image_input = gr.Image(label="请上传图片",
height=640,
show_share_button=False)
with gr.Row():
run_button = gr.Button(value="智能时尚评估")
score = gr.Textbox(label="认知计算时尚总得分")
with gr.Column():
with gr.Group():
with gr.Row():
with gr.Column():
type = gr.Textbox(label="认知-人物类型")
headgear = gr.Textbox(label="认知-帽子类型")
shoe = gr.Textbox(label="认知-鞋子类型")
backpack = gr.Textbox(label="认知-背包类型")
countenance = gr.Textbox(label="认知-表情类型")
with gr.Column():
upper_garment = gr.Textbox(label="认知-上衣类型")
lower_garment = gr.Textbox(label="认知-下衣类型")
sock = gr.Textbox(label="认知-袜子类型")
accessory = gr.Textbox(label="认知-配饰类型")
action = gr.Textbox(label="认知-动作类型")
text_output = gr.Textbox(label="小玲时尚穿搭建议")
run_button.click(eval_image,
inputs=Image_input,
outputs=[text_output,
score,
type,
upper_garment,
lower_garment,
headgear,
sock,
shoe,
accessory,
backpack,
action,
countenance])
demo.queue().launch(share=True)