File size: 2,098 Bytes
f5660c2
 
 
 
 
 
 
2b9e61d
 
 
f5660c2
 
2b9e61d
 
 
 
 
 
 
f5660c2
 
2b9e61d
 
 
 
 
 
 
 
 
 
 
 
f5660c2
 
 
 
2b9e61d
 
f5660c2
 
 
 
 
 
2b9e61d
 
 
 
 
f5660c2
 
 
 
c3fd84a
 
 
 
f5660c2
c3fd84a
 
 
 
f5660c2
 
2b9e61d
f5660c2
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import gradio as gr
import json
from random import sample


with open("meta.json") as f:
    meta = json.load(f)

meta_s = [x for x in meta if x["rating"] == "s"]
meta_g = [x for x in meta if x["rating"] == "g"]
with open("tag2cat.json") as f:
    tag2cat = json.load(f)
f = lambda cat, e: ", ".join(
    t
    for t in (tag.replace("_", " ") for tag in e["tag_string_general"].split())
    if tag2cat.get(t, "") == cat
)

cats = "Character Outfit Action Scene".split()


def do(ratings):
    if not ratings:
        raise gr.Error("Please select at least one of the ratings")
    m = (
        meta_g
        if ratings[0] == "General"
        else meta_s
        if ratings[0] == "Sensitive"
        else meta
    )
    idxs = sample(range(len(m)), 4)
    parts = [f(cat, m[idx]) for cat, idx in zip(cats, idxs)]
    return (",\n".join(parts),) + tuple(
        _
        for p, idx in zip(parts, idxs)
        for _ in (
            f"{p} ([danbooru post](https://danbooru.donmai.us/posts/{m[idx]['id']}))",
            f'![]({m[idx]["large_file_url"]})',
        )
    )


with gr.Blocks() as demo:
    with gr.Row():
        ratings = gr.CheckboxGroup(
            ["General", "Sensitive"],
            label="Allowed ratings",
            value=["General", "Sensitive"],
        )
        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,
        [ratings],
        [
            joined,
            text_char,
            img_char,
            text_outfit,
            img_outfit,
            text_action,
            img_action,
            text_scene,
            img_scene,
        ],
    )

demo.launch()