gen_image / create_character_app.py
phuongpropk05's picture
Upload folder using huggingface_hub
f5b4143 verified
import gradio as gr
import json
def update_raw():
with open("characters_story_sensitive.json", 'r', encoding='utf-8') as file:
categories = json.load(file)
cate_list = [c['cate_name'] for c in categories]
tag_list = []
for category in categories:
for character in category['characters']:
if isinstance(character['persona_tag'], list):
tag_list.extend(character['persona_tag'])
else:
tag_list.append(character['persona_tag'])
tag_list = list(set(tag_list))
return categories, cate_list, tag_list
def update_ui():
_, cate_list, tag_list = update_raw()
return (
gr.Dropdown(choices=cate_list),
gr.Dropdown(choices=tag_list)
)
def add_character(cate_name, name, introduction, persona_tag, personality, language_characteristics, character_relationship, preview, age, gender, isBottom, isTop, height, weight, chest_measurement, waist_measurement, hip_measurement, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes):
categories, cate_list, tag_list = update_raw()
category = next((c for c in categories if c["cate_name"] == cate_name), None)
if not category:
category = {
"cate_id": str(len(cate_list) + 1),
"cate_name": cate_name,
"characters": []
}
categories.append(category)
cate_list.append(cate_name)
characters = category['characters']
character = {
"id": f'{str(len(characters)+1)}',
"name": f'{name}',
"introduction": f'{introduction}',
"persona_tag": persona_tag,
"personality": f"{personality}",
"language_characteristics": f"{language_characteristics}",
"character_relationship": f"{character_relationship}",
"preview": f"{preview}",
"Age": f"{age}",
"gender": f"{gender}",
"isBottom": f"{isBottom}",
"isTop": f"{isTop}",
"height": height,
"weight": weight,
"measurement": [chest_measurement, waist_measurement, hip_measurement],
"hair_color": f"{hair_color}",
"face_shape": f"{face_shape}",
"eyes_color": f"{eyes_color}",
"eyebrows": f"{eyebrows}",
"hair_length": f"{hair_length}",
"hair_style": f"{hair_style}",
"clothes": f"{clothes}",
}
characters.append(character)
with open("characters_story_sensitive.json", 'w', encoding='utf-8') as file:
json.dump(categories, file, ensure_ascii=False, indent=4)
def add_character_json(cate_name, text):
categories, cate_list, tag_list = update_raw()
category = next((c for c in categories if c["cate_name"] == cate_name), None)
if not category:
category = {
"cate_id": str(len(cate_list) + 1),
"cate_name": cate_name,
"characters": []
}
categories.append(category)
cate_list.append(cate_name)
characters = category['characters']
object_characters = json.loads(text)
if isinstance(object_characters, list):
print("list")
for object_character in object_characters:
character = {
"id": f'{str(len(characters)+1)}',
"name": object_character["name"],
"introduction": object_character["introduction"],
"persona_tag": object_character["persona_tag"],
"personality": object_character["personality"],
"language_characteristics": object_character["language_characteristics"],
"character_relationship": object_character["character_relationship"],
"preview": object_character["preview"],
"Age": object_character["age"],
"gender": object_character["gender"],
"isBottom": object_character["isBottom"],
"isTop": object_character["isTop"],
"height": object_character["height"],
"weight": object_character["weight"],
"measurement": object_character["measurement"],
"hair_color": object_character["hair_color"],
"face_shape": object_character["face_shape"],
"eyes_color": object_character["eyes_color"],
"eyebrows": object_character["eyebrows"],
"hair_length": object_character["hair_length"],
"hair_style": object_character["hair_style"],
"clothes": object_character["clothes"],
}
characters.append(character)
else:
print("not list")
# object_character = json.loads(text)
character = {
"id": f'{str(len(characters)+1)}',
"name": object_characters["name"],
"introduction": object_characters["introduction"],
"persona_tag": object_characters["persona_tag"],
"personality": object_characters["personality"],
"language_characteristics": object_characters["language_characteristics"],
"character_relationship": object_characters["character_relationship"],
"preview": object_characters["preview"],
"Age": object_characters["age"],
"gender": object_characters["gender"],
"isBottom": object_characters["isBottom"],
"isTop": object_characters["isTop"],
"height": object_characters["height"],
"weight": object_characters["weight"],
"measurement": object_characters["measurement"],
"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"],
}
characters.append(character)
with open("characters_story_sensitive.json", 'w', encoding='utf-8') as file:
json.dump(categories, file, ensure_ascii=False, indent=4)
return category
with gr.Blocks() as demo:
cate_name = gr.Dropdown(label='Name of category', choices=update_raw()[1], allow_custom_value=True)
with gr.Tab("nhập tay"):
name = gr.Text(label="Character's name")
introduction = gr.Text(label="Character's introduction")
persona_tag = gr.Dropdown(label="Character's personal tag", choices=update_raw()[2], allow_custom_value=True, multiselect=True)
personality = gr.Text(label="Character's personality")
language_characteristics = gr.Text(label="Character's language_characteristics")
character_relationship = gr.Text(label="Character's relationship with user")
preview = gr.Text(label="Character's short preview description")
age = gr.Text(label="Character's age")
gender = gr.Dropdown(label="Character's gender", choices=[0, 1, 2], info='0: male, 1: female, 2: non-binary')
isBottom = gr.Checkbox(label="Is character bottom if gender is non-binary")
isTop = gr.Checkbox(label="Is character top if gender is non-binary")
height = gr.Number(label="Character's height")
weight = gr.Number(label="Character's weight")
chest_measurement = gr.Number(label="Character's chest measurement")
waist_measurement = gr.Number(label="Character's waist measurement")
hip_measurement = gr.Number(label="Character's hip measurement")
hair_color = gr.Text(label="Character's hair color")
hair_length = gr.Text(label="Character's hair length")
hair_style = gr.Text(label="Character's hair style")
face_shape = gr.Text(label="Character's face shape")
eyes_color = gr.Text(label="Character's eyes color")
eyebrows = gr.Text(label="Character's eyebrows")
clothes = gr.Text(label="Character's clothes")
tab = 1
submit_button = gr.Button('Submit')
update_button = gr.Button('Reload categories, personal tag')
submit_button.click(
add_character,
inputs=[cate_name, name, introduction, persona_tag, personality, language_characteristics, character_relationship, preview, age, gender, isBottom, isTop, height, weight, chest_measurement, waist_measurement, hip_measurement, hair_color, face_shape, eyes_color, eyebrows, hair_length, hair_style, clothes],
outputs=None
)
update_button.click(
update_ui,
inputs=[],
outputs=[cate_name, persona_tag]
)
with gr.Tab("Import Json"):
tab = 2
json_text = gr.Textbox(label="Paste JSON here", lines=10, placeholder='{"name": "character1"}')
output = gr.JSON(label="Parsed JSON")
# json_text.change(add_character_json, inputs=[cate_name, json_text], outputs=output)
submit_button = gr.Button('Submit')
update_button = gr.Button('Reload categories, personal tag')
submit_button.click(
add_character_json,
inputs=[cate_name, json_text],
outputs=output
)
update_button.click(
update_ui,
inputs=[],
outputs=[cate_name]
)
demo.load(
update_ui,
inputs=[],
outputs=[cate_name, persona_tag]
)
demo.launch(server_port=1111, server_name="192.168.5.155", share=True)