Spaces:
Runtime error
Runtime error
| import random | |
| import gradio as gr | |
| from test_folder.test_gen import gen_image | |
| import json | |
| def update_raw(): | |
| with open('list_choices.json', 'r', encoding='utf-8') as f: | |
| choices = json.load(f) | |
| hair_color_choices = choices['hair_color_choices'] | |
| face_shape_choices = choices['face_shape_choices'] | |
| eyes_color_choices = choices['eyes_color_choices'] | |
| eyebrows_choices = choices['eyebrows_choices'] | |
| hair_length_choices = choices['hair_length_choices'] | |
| hair_style_choices = choices['hair_style_choices'] | |
| clothes_choices = choices['clothes_choices'] | |
| pose_choices = choices['pose_choices'] | |
| background_choices = choices['background_choices'] | |
| return hair_color_choices, face_shape_choices, eyes_color_choices, eyebrows_choices, hair_length_choices, hair_style_choices, hair_style_choices, clothes_choices, pose_choices, background_choices | |
| def update_ui(): | |
| hair_color_choices, face_shape_choices, eyes_color_choices, eyebrows_choices, hair_length_choices, hair_style_choices, hair_style_choices, clothes_choices, pose_choices, background_choices = update_raw() | |
| return ( | |
| gr.Dropdown(choices=hair_color_choices), | |
| gr.Dropdown(choices=face_shape_choices), | |
| gr.Dropdown(choices=eyes_color_choices), | |
| gr.Dropdown(choices=eyebrows_choices), | |
| gr.Dropdown(choices=hair_length_choices), | |
| gr.Dropdown(choices=hair_style_choices), | |
| gr.Dropdown(choices=clothes_choices), | |
| gr.Dropdown(choices=pose_choices), | |
| gr.Dropdown(choices=background_choices) | |
| ) | |
| def add_new(hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background): | |
| hair_color_choices, face_shape_choices, eyes_color_choices, eyebrows_choices, hair_length_choices, hair_style_choices, hair_style_choices, clothes_choices, pose_choices, background_choices = update_raw() | |
| with open('list_choices.json', 'r', encoding='utf-8') as f: | |
| choices = json.load(f) | |
| if hair_color not in hair_color_choices: | |
| choices['hair_color_choices'].append(hair_color) | |
| if face_shape not in face_shape_choices: | |
| choices['face_shape_choices'].append(face_shape) | |
| if eyes_color not in eyes_color_choices: | |
| choices['eyes_color_choices'].append(eyes_color) | |
| if eyebrows not in eyebrows_choices: | |
| choices['eyebrows_choices'].append(eyebrows) | |
| if hair_length not in hair_length_choices: | |
| choices['hair_length_choices'].append(hair_length) | |
| if hair_style not in hair_style_choices: | |
| choices['hair_style_choices'].append(hair_style) | |
| if clothes not in clothes_choices: | |
| choices['clothes_choices'].append(clothes) | |
| if pose not in pose_choices: | |
| choices['pose_choices'].append(pose) | |
| if background not in background_choices: | |
| choices['background_choices'].append(background) | |
| with open("list_choices.json", 'w', encoding='utf-8') as file: | |
| json.dump(choices, file, ensure_ascii=False, indent=4) | |
| def process(image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background): | |
| add_new(hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, pose, background) | |
| link, log = gen_image(image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background) | |
| return link, log | |
| def process_json(text): | |
| object_characters = json.loads(text) | |
| image_link = object_characters["image_link"] | |
| hair_color = object_characters["hair_color"] | |
| face_shape = object_characters["face_shape"] | |
| eyes_color = object_characters["eyes_color"] | |
| eyebrows = object_characters["eyebrows"] | |
| hair_length = object_characters["hair_length"] | |
| hair_style = object_characters["hair_style"] | |
| clothes = object_characters["clothes"] | |
| pose = object_characters["pose"] | |
| background = object_characters["background"] | |
| add_new(hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background) | |
| link, log = gen_image(image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background) | |
| return link, log | |
| def output_data(output_link, image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background): | |
| values = [output_link, image_link, hair_color, face_shape, eyes_color, eyebrows, | |
| hair_length, hair_style, clothes, pose, background] | |
| result = "\t".join(values) | |
| return result | |
| def output_data_json(text, output_link): | |
| object_characters = json.loads(text) | |
| image_link = object_characters["image_link"] | |
| hair_color = object_characters["hair_color"] | |
| face_shape = object_characters["face_shape"] | |
| eyes_color = object_characters["eyes_color"] | |
| eyebrows = object_characters["eyebrows"] | |
| hair_length = object_characters["hair_length"] | |
| hair_style = object_characters["hair_style"] | |
| clothes = object_characters["clothes"] | |
| pose = object_characters["pose"] | |
| background = object_characters["background"] | |
| result = str(output_data(output_link, image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background)) | |
| return result | |
| def random_element(image_link): | |
| with open('list_choices.json', 'r', encoding='utf-8') as f: | |
| elements = json.load(f) | |
| hair_color_choices = elements['hair_color_choices'] | |
| face_shape_choices = elements['face_shape_choices'] | |
| eyes_color_choices = elements['eyes_color_choices'] | |
| eyebrows_choices = elements['eyebrows_choices'] | |
| hair_length_choices = elements['hair_length_choices'] | |
| hair_style_choices = elements['hair_style_choices'] | |
| clothes_choices = elements['clothes_choices'] | |
| pose_choices = elements['pose_choices'] | |
| background_choices = elements['background_choices'] | |
| hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background = random.choice(hair_color_choices), random.choice(face_shape_choices), random.choice(eyes_color_choices), random.choice(eyebrows_choices), random.choice(hair_length_choices), random.choice(hair_style_choices), random.choice(clothes_choices), random.choice(pose_choices), random.choice(background_choices) | |
| link, log = gen_image(image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background) | |
| result = str(output_data(link, image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background)) | |
| jsons = { | |
| "image_link": image_link, | |
| "hair_color": hair_color, | |
| "face_shape": face_shape, | |
| "eyes_color": eyes_color, | |
| "eyebrows": eyebrows, | |
| "hair_length": hair_length, | |
| "hair_style": hair_style, | |
| "clothes": clothes, | |
| "pose": pose, | |
| "background": background | |
| } | |
| return jsons, link, log, result | |
| with gr.Blocks() as demo: | |
| with gr.Tab("Nhập tay"): | |
| image_link = gr.Textbox(label="Link of original image") | |
| tab =1 | |
| hair_color = gr.Dropdown(choices=update_raw()[0], label="Hair Color of Character", allow_custom_value=True) | |
| face_shape = gr.Dropdown(choices=update_raw()[1], label="Face shape of Character", allow_custom_value=True) | |
| eyes_color = gr.Dropdown(choices=update_raw()[2], label="Eyes Color of Character", allow_custom_value=True) | |
| eyebrows = gr.Dropdown(choices=update_raw()[3], label="Eyebrows of Character", allow_custom_value=True) | |
| hair_length = gr.Dropdown(choices=update_raw()[4], label="Hair Length of Character", allow_custom_value=True) | |
| hair_style = gr.Dropdown(choices=update_raw()[5], label="Hair Style of Character", allow_custom_value=True) | |
| clothes = gr.Dropdown(choices=update_raw()[6], label="Clothes of Character", allow_custom_value=True) | |
| pose = gr.Dropdown(choices=update_raw()[7], label="Pose of Character", allow_custom_value=True) | |
| background = gr.Dropdown(choices=update_raw()[8], label="Background of Image", allow_custom_value=True) | |
| output_link = gr.Textbox(value="Link Image") | |
| output_log = gr.Textbox(value="Log Information") | |
| submit_button = gr.Button('Submit') | |
| update_button = gr.Button('Reload choices') | |
| export_json = gr.Button('Export Data') | |
| output_js = gr.Textbox(label="Copy & Paste to Google Sheets", lines=1, show_label=True, show_copy_button=True) | |
| export_json.click( | |
| output_data, | |
| inputs=[output_link, image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background], | |
| outputs=[output_js] | |
| ) | |
| submit_button.click( | |
| process, | |
| inputs=[image_link, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes, pose, background], | |
| outputs=[output_link, output_log] | |
| ) | |
| update_button.click( | |
| update_ui, | |
| inputs=[], | |
| outputs=[hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, pose, background] | |
| ) | |
| with gr.Tab("Import Json"): | |
| tab = 2 | |
| json_text = gr.Textbox(label="Paste JSON here", lines=10, placeholder='{\n\t"image_link: "",\n\t"hair_color": "",\n\t"face_shape": "",\n\t"eyes_color": "",\n\t"eyebrows": "",\n\t"hair_length": "",\n\t"hair_style": "",\n\t"clothes": "",\n\t"pose": "",\n\t"background": ""\n}') | |
| output = gr.JSON(label="Parsed JSON") | |
| output_link = gr.Textbox(value="Link Image") | |
| output_log = gr.Textbox(value="Log Information") | |
| submit_button = gr.Button('Submit') | |
| update_button = gr.Button('Reload choices') | |
| export_json = gr.Button('Export Data') | |
| output_js = gr.Textbox(label="Copy & Paste to Google Sheets", lines=1, show_label=True, show_copy_button=True) | |
| export_json.click( | |
| output_data_json, | |
| inputs=[json_text, output_link], | |
| outputs=[output_js] | |
| ) | |
| submit_button.click( | |
| process_json, | |
| inputs=[json_text], | |
| outputs=[output_link, output_log] | |
| ) | |
| update_button.click( | |
| update_ui, | |
| inputs=[], | |
| outputs=[hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, pose, background] | |
| ) | |
| with gr.Tab("Random thuộc tính"): | |
| tab = 3 | |
| image_link = gr.Textbox(label="Link of original image") | |
| output = gr.JSON(label="Parsed JSON") | |
| output_link = gr.Textbox(label="Link Image", show_label=True, show_copy_button=True) | |
| output_log = gr.Textbox(label="Log Information") | |
| submit_button = gr.Button('Submit') | |
| export_json = gr.Button('Export Data') | |
| output_js = gr.Textbox(label="Copy & Paste to Google Sheets", lines=1, show_label=True, show_copy_button=True) | |
| # export_json.click( | |
| # output_data_json, | |
| # inputs=[json_text, output_link], | |
| # outputs=[output_js] | |
| # ) | |
| submit_button.click( | |
| random_element, | |
| inputs=[image_link], | |
| outputs=[output, output_link, output_log, output_js] | |
| ) | |
| demo.load( | |
| update_ui, | |
| inputs=[], | |
| outputs=[hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, pose, background] | |
| ) | |
| demo.launch(server_port=1111, server_name="192.168.5.155", share=True) |