Spaces:
Running
Running
import gradio as gr | |
import json | |
from random import sample | |
with open("meta.json") as f: | |
meta = json.load(f) | |
with open("tag2cat.json") as f: | |
tag2cat = json.load(f) | |
f = lambda cat: [ | |
", ".join( | |
t | |
for t in (tag.replace("_", " ") for tag in e["tag_string_general"].split()) | |
if tag2cat.get(t, "") == cat | |
) | |
for e in meta | |
] | |
l = [f(c) for c in "Character Outfit Action Scene".split()] | |
def do(): | |
idxs = sample(range(len(meta)), 4) | |
parts = [ll[idx] for ll, idx in zip(l, idxs)] | |
return (",\n".join(parts),) + tuple( | |
_ | |
for p, idx in zip(parts, idxs) | |
for _ in ( | |
f"{p} ([danbooru post](https://danbooru.donmai.us/posts/{meta[idx]['id']}))", | |
f'', | |
) | |
) | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
button = gr.Button("Get Random Prompt", variant="primary") | |
with gr.Row(): | |
joined = gr.Text(label="generated prompt", show_copy_button=True) | |
with gr.Row(equal_height=True): | |
text_char = gr.Markdown() | |
text_outfit = gr.Markdown() | |
text_action = gr.Markdown() | |
text_scene = gr.Markdown() | |
with gr.Row(equal_height=True): | |
img_char = gr.Markdown() | |
img_outfit = gr.Markdown() | |
img_action = gr.Markdown() | |
img_scene = gr.Markdown() | |
button.click( | |
do, | |
[], | |
[ | |
joined, | |
text_char, | |
img_char, | |
text_outfit, | |
img_outfit, | |
text_action, | |
img_action, | |
text_scene, | |
img_scene, | |
], | |
) | |
demo.launch() | |