John6666 commited on
Commit
6bba96a
β€’
0 Parent(s):

Super-squash branch 'main' using huggingface_hub

Browse files
Files changed (12) hide show
  1. .gitattributes +35 -0
  2. README.md +13 -0
  3. app.py +115 -0
  4. character_series_dict.csv +0 -0
  5. danbooru_e621.csv +0 -0
  6. genimage.py +66 -0
  7. llmdolphin.py +471 -0
  8. pre-requirements.txt +1 -0
  9. requirements.txt +16 -0
  10. tag_group.csv +0 -0
  11. tagger.py +506 -0
  12. utils.py +45 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Natural Text to SD Prompt Translator With LLM alpha
3
+ emoji: πŸ‘€πŸ˜»
4
+ colorFrom: red
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 4.38.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import (
3
+ gradio_copy_text,
4
+ COPY_ACTION_JS,
5
+ )
6
+ from tagger import (
7
+ convert_danbooru_to_e621_prompt,
8
+ insert_recom_prompt,
9
+ )
10
+ from genimage import (
11
+ generate_image,
12
+ )
13
+ from llmdolphin import (
14
+ get_llm_formats,
15
+ get_dolphin_model_format,
16
+ get_dolphin_models,
17
+ get_dolphin_model_info,
18
+ select_dolphin_model,
19
+ select_dolphin_format,
20
+ add_dolphin_models,
21
+ get_dolphin_sysprompt,
22
+ get_dolphin_sysprompt_mode,
23
+ select_dolphin_sysprompt,
24
+ get_dolphin_languages,
25
+ select_dolphin_language,
26
+ dolphin_respond,
27
+ dolphin_parse,
28
+ )
29
+
30
+
31
+ with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css="") as app:
32
+ gr.Markdown("""# Natural Text to SD Prompt Translator With LLM alpha
33
+ Text in natural language (English, Japanese, ...) => Prompt
34
+ """)
35
+ with gr.Column(scale=1):
36
+ with gr.Group():
37
+ chatbot = gr.Chatbot(likeable=False, show_copy_button=True, show_share_button=False, layout="panel", container=True, )
38
+ with gr.Row():
39
+ chat_msg = gr.Textbox(show_label=False, placeholder="Input text in English, Japanese, or any other languages and press Enter or click Send.", scale=4)
40
+ chat_submit = gr.Button("Send", scale=1)
41
+ chat_clear = gr.Button("Clear", scale=1)
42
+ with gr.Accordion("Additional inputs", open=False):
43
+ chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
44
+ chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
45
+ chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
46
+ chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
47
+ chat_tokens = gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max tokens")
48
+ chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
49
+ chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
50
+ chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
51
+ chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
52
+ with gr.Row():
53
+ chat_mode = gr.Dropdown(choices=get_dolphin_sysprompt_mode(), value=get_dolphin_sysprompt_mode()[0], allow_custom_value=False, label="Mode")
54
+ chat_lang = gr.Dropdown(choices=get_dolphin_languages(), value="English", allow_custom_value=True, label="Output language")
55
+ with gr.Accordion("Add models", open=False):
56
+ chat_add_text = gr.Textbox(label="URL or Repo ID", placeholder="http://huggingface.co/.../...gguf or author/model", lines=1)
57
+ chat_add_format = gr.Dropdown(choices=get_llm_formats(), value=get_llm_formats()[0], label="Message format")
58
+ chat_add_submit = gr.Button("Update lists of models")
59
+
60
+ with gr.Column(scale=1):
61
+ with gr.Row():
62
+ with gr.Group():
63
+ output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
64
+ copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
65
+ elapsed_time_md = gr.Markdown(label="Elapsed time", value="", visible=False)
66
+ with gr.Group():
67
+ output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
68
+ copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
69
+ with gr.Accordion(label="Advanced options", open=False, visible=False):
70
+ tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
71
+ dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
72
+ dummy_np_pony = gr.Textbox(label="Negative prompt", value="", visible=False)
73
+ recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
74
+ recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
75
+ generate_image_btn = gr.Button(value="GENERATE IMAGE", size="lg", variant="primary")
76
+ result_image = gr.Gallery(label="Generated images", columns=1, object_fit="contain", container=True, preview=True, show_label=False, show_share_button=False, show_download_button=True, interactive=False, visible=True, format="png")
77
+
78
+ gr.on(
79
+ triggers=[chat_msg.submit, chat_submit.click],
80
+ fn=dolphin_respond,
81
+ inputs=[chat_msg, chatbot, chat_model, chat_sysmsg, chat_tokens, chat_temperature, chat_topp, chat_topk, chat_rp],
82
+ outputs=[chatbot],
83
+ queue=True,
84
+ show_progress="full",
85
+ trigger_mode="once",
86
+ ).success(dolphin_parse, [chatbot], [output_text, copy_btn, copy_btn_pony]).success(
87
+ convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False,
88
+ ).success(
89
+ insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False,
90
+ ).success(
91
+ insert_recom_prompt, [output_text_pony, dummy_np_pony, recom_pony], [output_text_pony, dummy_np_pony], queue=False,
92
+ )
93
+ chat_clear.click(lambda: None, None, chatbot, queue=False)
94
+ chat_model.change(select_dolphin_model, [chat_model], [chat_model, chat_format, chat_model_info], queue=True, show_progress="full")
95
+ chat_format.change(select_dolphin_format, [chat_format], [chat_format], queue=False)
96
+ chat_mode.change(select_dolphin_sysprompt, [chat_mode], [chat_sysmsg], queue=False)
97
+ chat_lang.change(select_dolphin_language, [chat_lang], [chat_sysmsg], queue=False)
98
+ gr.on(
99
+ triggers=[chat_add_text.submit, chat_add_submit.click],
100
+ fn=add_dolphin_models,
101
+ inputs=[chat_add_text, chat_add_format],
102
+ outputs=[chat_model],
103
+ queue=False,
104
+ trigger_mode="once",
105
+ )
106
+
107
+ copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS)
108
+ copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS)
109
+
110
+ generate_image_btn.click(generate_image, [output_text, dummy_np], [result_image], show_progress="full")
111
+
112
+
113
+ if __name__ == "__main__":
114
+ app.queue()
115
+ app.launch()
character_series_dict.csv ADDED
The diff for this file is too large to render. See raw diff
 
danbooru_e621.csv ADDED
The diff for this file is too large to render. See raw diff
 
genimage.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+
3
+
4
+ def load_pipeline():
5
+ from diffusers import DiffusionPipeline
6
+ import torch
7
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
8
+ pipe = DiffusionPipeline.from_pretrained(
9
+ "John6666/rae-diffusion-xl-v2-sdxl-spo-dpo-turbo",
10
+ custom_pipeline="lpw_stable_diffusion_xl",
11
+ torch_dtype=torch.float16,
12
+ )
13
+ pipe.to(device)
14
+ return pipe
15
+
16
+
17
+ def save_image(image, metadata, output_dir):
18
+ import os
19
+ import uuid
20
+ import json
21
+ from PIL import PngImagePlugin
22
+ filename = str(uuid.uuid4()) + ".png"
23
+ os.makedirs(output_dir, exist_ok=True)
24
+ filepath = os.path.join(output_dir, filename)
25
+ metadata_str = json.dumps(metadata)
26
+ info = PngImagePlugin.PngInfo()
27
+ info.add_text("metadata", metadata_str)
28
+ image.save(filepath, "PNG", pnginfo=info)
29
+ return filepath
30
+
31
+
32
+ pipe = load_pipeline()
33
+
34
+
35
+ @spaces.GPU
36
+ def generate_image(prompt, neg_prompt):
37
+ metadata = {
38
+ "prompt": prompt,
39
+ "negative_prompt": neg_prompt,
40
+ "resolution": f"{1024} x {1024}",
41
+ "guidance_scale": 2,
42
+ "num_inference_steps": 16,
43
+ "sampler": "LCM",
44
+ }
45
+ try:
46
+ images = pipe(
47
+ prompt=prompt,
48
+ prompt_2="anime artwork, anime style, vibrant, studio anime, highly detailed, masterpiece, best quality, very aesthetic, absurdres",
49
+ negative_prompt=neg_prompt,
50
+ negative_prompt_2="lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], photo, deformed, disfigured, low contrast, photo, deformed, disfigured, low contrast",
51
+ width=1024,
52
+ height=1024,
53
+ guidance_scale=2,
54
+ num_inference_steps=16,
55
+ output_type="pil",
56
+ clip_skip=1,
57
+ ).images
58
+ if images:
59
+ image_paths = [
60
+ save_image(image, metadata, "./outputs")
61
+ for image in images
62
+ ]
63
+ return image_paths
64
+ except Exception as e:
65
+ return []
66
+
llmdolphin.py ADDED
@@ -0,0 +1,471 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from llama_cpp import Llama
4
+ from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
5
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
6
+ from llama_cpp_agent.chat_history import BasicChatHistory
7
+ from llama_cpp_agent.chat_history.messages import Roles
8
+
9
+
10
+ llm_models_dir = "./llm_models"
11
+ llm_models = {
12
+ "SwallowMaid-8B-L3-SPPO-abliterated.i1-Q5_K_M.gguf": ["mradermacher/SwallowMaid-8B-L3-SPPO-abliterated-i1-GGUF", MessagesFormatterType.LLAMA_3],
13
+ "Tiger-Gemma-9B-v1-Q4_K_M.gguf": ["bartowski/Tiger-Gemma-9B-v1-GGUF", MessagesFormatterType.LLAMA_3],
14
+ "TooManyMixRolePlay-7B-Story_V3.5.Q4_K_M.gguf": ["mradermacher/TooManyMixRolePlay-7B-Story_V3.5-GGUF", MessagesFormatterType.LLAMA_3],
15
+ "natsumura-llama3-v1.1-8b.Q4_K_M.gguf": ["mradermacher/natsumura-llama3-v1.1-8b-GGUF", MessagesFormatterType.LLAMA_3],
16
+ "natsumura-llama3-v1-8b.i1-Q4_K_M.gguf": ["mradermacher/natsumura-llama3-v1-8b-i1-GGUF", MessagesFormatterType.LLAMA_3],
17
+ "nephra_v1.0.Q5_K_M.gguf": ["PrunaAI/yodayo-ai-nephra_v1.0-GGUF-smashed", MessagesFormatterType.LLAMA_3],
18
+ "DPO-ONLY-Zephyr-7B.Q6_K.gguf": ["mradermacher/DPO-ONLY-Zephyr-7B-GGUF", MessagesFormatterType.LLAMA_3],
19
+ "L3-Deluxe-Scrambled-Eggs-On-Toast-8B.Q8_0.gguf": ["mradermacher/L3-Deluxe-Scrambled-Eggs-On-Toast-8B-GGUF", MessagesFormatterType.LLAMA_3],
20
+ "L3-Scrambled-Eggs-On-Toast-8B.i1-Q6_K.gguf": ["mradermacher/L3-Scrambled-Eggs-On-Toast-8B-i1-GGUF", MessagesFormatterType.LLAMA_3],
21
+ "llama-3-Nephilim-v2.1-8B.Q5_K_M.gguf": ["grimjim/llama-3-Nephilim-v2.1-8B-GGUF", MessagesFormatterType.LLAMA_3],
22
+ "Llama-3-uncensored-Dare-1.Q4_K_M.gguf": ["mradermacher/Llama-3-uncensored-Dare-1-GGUF", MessagesFormatterType.LLAMA_3],
23
+ "llama3-8B-DarkIdol-2.2-Uncensored-1048K.i1-Q6_K.gguf": ["mradermacher/llama3-8B-DarkIdol-2.2-Uncensored-1048K-i1-GGUF", MessagesFormatterType.LLAMA_3],
24
+ "llama3-8B-DarkIdol-2.2-Uncensored-1048K.Q8_0.gguf": ["mradermacher/llama3-8B-DarkIdol-2.2-Uncensored-1048K-GGUF", MessagesFormatterType.LLAMA_3],
25
+ "dolphin-2.9.3-mistral-7b-32k-q4_k_m.gguf": ["huggingkot/dolphin-2.9.3-mistral-7B-32k-Q4_K_M-GGUF", MessagesFormatterType.MISTRAL],
26
+ "dolphin-2.9.3-mistral-7B-32k-Q5_K_M.gguf": ["bartowski/dolphin-2.9.3-mistral-7B-32k-GGUF", MessagesFormatterType.MISTRAL],
27
+ "Lexi-Llama-3-8B-Uncensored_Q5_K_M.gguf": ["Orenguteng/Llama-3-8B-Lexi-Uncensored-GGUF", MessagesFormatterType.LLAMA_3],
28
+ "Llama3-Sophie.Q8_0.gguf": ["mradermacher/Llama3-Sophie-GGUF", MessagesFormatterType.LLAMA_3],
29
+ "Aura-Uncensored-OAS-8B-L3.i1-Q4_K_M.gguf": ["mradermacher/Aura-Uncensored-OAS-8B-L3-i1-GGUF", MessagesFormatterType.LLAMA_3],
30
+ "L3-Uncen-Merger-Omelette-RP-v0.2-8B-Q5_K_S-imat.gguf": ["LWDCLS/L3-Uncen-Merger-Omelette-RP-v0.2-8B-GGUF-IQ-Imatrix-Request", MessagesFormatterType.LLAMA_3],
31
+ "qwen2-diffusion-prompter-v01-q6_k.gguf": ["trollek/Qwen2-0.5B-DiffusionPrompter-v0.1-GGUF", MessagesFormatterType.LLAMA_3],
32
+ "Smegmma-Deluxe-9B-v1-Q6_K.gguf": ["bartowski/Smegmma-Deluxe-9B-v1-GGUF", MessagesFormatterType.MISTRAL],
33
+ "Mahou-1.3c-mistral-7B.i1-Q6_K.gguf": ["mradermacher/Mahou-1.3c-mistral-7B-i1-GGUF", MessagesFormatterType.MISTRAL],
34
+ "Silicon-Maid-7B-Q8_0_X.gguf": ["duyntnet/Silicon-Maid-7B-imatrix-GGUF", MessagesFormatterType.ALPACA],
35
+ "l3-umbral-mind-rp-v3.0-8b-q5_k_m-imat.gguf": ["Casual-Autopsy/L3-Umbral-Mind-RP-v3.0-8B-Q5_K_M-GGUF", MessagesFormatterType.LLAMA_3],
36
+ "EZO-Common-9B-gemma-2-it.i1-Q4_K_M.gguf": ["mradermacher/EZO-Common-9B-gemma-2-it-i1-GGUF", MessagesFormatterType.MISTRAL],
37
+ }
38
+ llm_formats = {
39
+ "MISTRAL": MessagesFormatterType.MISTRAL,
40
+ "CHATML": MessagesFormatterType.CHATML,
41
+ "VICUNA": MessagesFormatterType.VICUNA,
42
+ "LLAMA 2": MessagesFormatterType.LLAMA_2,
43
+ "SYNTHIA": MessagesFormatterType.SYNTHIA,
44
+ "NEURAL CHAT": MessagesFormatterType.NEURAL_CHAT,
45
+ "SOLAR": MessagesFormatterType.SOLAR,
46
+ "OPEN CHAT": MessagesFormatterType.OPEN_CHAT,
47
+ "ALPACA": MessagesFormatterType.ALPACA,
48
+ "CODE DS": MessagesFormatterType.CODE_DS,
49
+ "B22": MessagesFormatterType.B22,
50
+ "LLAMA 3": MessagesFormatterType.LLAMA_3,
51
+ "PHI 3": MessagesFormatterType.PHI_3,
52
+ }
53
+ # https://github.com/Maximilian-Winter/llama-cpp-agent
54
+ llm_languages = ["English", "Japanese", "Chinese"]
55
+ llm_models_tupled_list = []
56
+ default_llm_model_filename = list(llm_models.keys())[0]
57
+ override_llm_format = None
58
+
59
+
60
+ def to_list(s):
61
+ return [x.strip() for x in s.split(",") if not s == ""]
62
+
63
+
64
+ def list_uniq(l):
65
+ return sorted(set(l), key=l.index)
66
+
67
+
68
+ def is_japanese(s):
69
+ import unicodedata
70
+ for ch in s:
71
+ name = unicodedata.name(ch, "")
72
+ if "CJK UNIFIED" in name or "HIRAGANA" in name or "KATAKANA" in name:
73
+ return True
74
+ return False
75
+
76
+
77
+ def update_llm_model_tupled_list():
78
+ from pathlib import Path
79
+ global llm_models_tupled_list
80
+ llm_models_tupled_list = []
81
+ for k, v in llm_models.items():
82
+ name = k
83
+ value = k
84
+ llm_models_tupled_list.append((name, value))
85
+ model_files = Path(llm_models_dir).glob('*.gguf')
86
+ for path in model_files:
87
+ name = path.name
88
+ value = path.name
89
+ llm_models_tupled_list.append((name, value))
90
+ llm_models_tupled_list = list_uniq(llm_models_tupled_list)
91
+ return llm_models_tupled_list
92
+
93
+
94
+ def download_llm_models():
95
+ from huggingface_hub import hf_hub_download
96
+ global llm_models_tupled_list
97
+ llm_models_tupled_list = []
98
+ for k, v in llm_models.items():
99
+ try:
100
+ hf_hub_download(repo_id = v[0], filename = k, local_dir = llm_models_dir)
101
+ except Exception:
102
+ continue
103
+ name = k
104
+ value = k
105
+ llm_models_tupled_list.append((name, value))
106
+
107
+
108
+ def download_llm_model(filename):
109
+ from huggingface_hub import hf_hub_download
110
+ if not filename in llm_models.keys(): return default_llm_model_filename
111
+ try:
112
+ hf_hub_download(repo_id = llm_models[filename][0], filename = filename, local_dir = llm_models_dir)
113
+ except Exception:
114
+ return default_llm_model_filename
115
+ update_llm_model_tupled_list()
116
+ return filename
117
+
118
+
119
+ def get_dolphin_model_info(filename):
120
+ md = "None"
121
+ items = llm_models.get(filename, None)
122
+ if items:
123
+ md = f'Repo: [{items[0]}](https://huggingface.co/{items[0]})'
124
+ return md
125
+
126
+
127
+ def select_dolphin_model(filename, progress=gr.Progress(track_tqdm=True)):
128
+ global override_llm_format
129
+ override_llm_format = None
130
+ progress(0, desc="Loading model...")
131
+ value = download_llm_model(filename)
132
+ progress(1, desc="Model loaded.")
133
+ md = get_dolphin_model_info(filename)
134
+ return gr.update(value=value, choices=get_dolphin_models()), gr.update(value=get_dolphin_model_format(value)), gr.update(value=md)
135
+
136
+
137
+ def select_dolphin_format(format_name):
138
+ global override_llm_format
139
+ override_llm_format = llm_formats[format_name]
140
+ return gr.update(value=format_name)
141
+
142
+
143
+ #download_llm_models()
144
+ download_llm_model(default_llm_model_filename)
145
+
146
+
147
+ def get_dolphin_models():
148
+ return update_llm_model_tupled_list()
149
+
150
+
151
+ def get_llm_formats():
152
+ return list(llm_formats.keys())
153
+
154
+
155
+ def get_key_from_value(d, val):
156
+ keys = [k for k, v in d.items() if v == val]
157
+ if keys:
158
+ return keys[0]
159
+ return None
160
+
161
+
162
+ def get_dolphin_model_format(filename):
163
+ if not filename in llm_models.keys(): filename = default_llm_model_filename
164
+ format = llm_models[filename][1]
165
+ format_name = get_key_from_value(llm_formats, format)
166
+ return format_name
167
+
168
+
169
+ def add_dolphin_models(query, format_name):
170
+ import re
171
+ from huggingface_hub import HfApi
172
+ global llm_models
173
+ api = HfApi()
174
+ add_models = {}
175
+ format = llm_formats[format_name]
176
+ filename = ""
177
+ repo = ""
178
+ try:
179
+ s = list(re.findall(r'^(?:https?://huggingface.co/)?(.+?/.+?)(?:/.*/(.+?.gguf).*?)?$', query)[0])
180
+ if s and "" in s: s.remove("")
181
+ if len(s) == 1:
182
+ repo = s[0]
183
+ if not api.repo_exists(repo_id = repo): return gr.update(visible=True)
184
+ files = api.list_repo_files(repo_id = repo)
185
+ for file in files:
186
+ if str(file).endswith(".gguf"): add_models[filename] = [repo, format]
187
+ elif len(s) >= 2:
188
+ repo = s[0]
189
+ filename = s[1]
190
+ if not api.repo_exists(repo_id = repo) or not api.file_exists(repo_id = repo, filename = filename): return gr.update(visible=True)
191
+ add_models[filename] = [repo, format]
192
+ else: return gr.update(visible=True)
193
+ except Exception:
194
+ return gr.update(visible=True)
195
+ print(add_models)
196
+ llm_models = (llm_models | add_models).copy()
197
+ return gr.update(choices=get_dolphin_models())
198
+
199
+
200
+ dolphin_output_language = "Japanese"
201
+ dolphin_sysprompt_mode = "Default"
202
+ dolphin_system_prompt = {"Default": r'''You are a helpful AI assistant to generate messages for AI that outputs an image when I enter a message.
203
+ The message must have the following [Tags] generated in strict accordance with the following [Rules]:
204
+ ```
205
+ [Tags]
206
+ - Words to describe full names of characters and names of series in which they appear AFAP.
207
+ - Words to describe names of the people there and their numbers, such as 2girls, 1boy.
208
+ - Words to describe their hair color, hairstyle, hair length, hair accessory, eye color, eye shape, facial expression, breast size, and clothing of them in detail, such as long hair.
209
+ - Words to describe their external features, ornaments and belongings (also specify colors, patterns, shapes) in detail.
210
+ - Words to describe their stance from head to toe in detail AFAP.
211
+ - Words to describe their acting, especially with sexual activity in detail AFAP.
212
+ - Words to describe their surroundings in detail.
213
+ - Words to describe background details, such as inside room, forest, starry sky.
214
+ [Rules]
215
+ - Any output should be plain text in English and don't use line breaks.
216
+ - Output only composed of Tags in 1 line, separated by commas with spaces between Tags, in lower case English.
217
+ - Output should be in the format: "//GENBEGIN//://1girl, Tag, Tag, ..., Tag//://GENEND//".
218
+ - Preferably refer to and describe the information obtained from Danbooru. If not, describe it in own way.
219
+ - It's preferable that each Tag is a plain phrase, word, caption, Danbooru tag, or E621 tag.
220
+ - Convert any nicknames to full names first AFAP.
221
+ - If a sexual theme is given, priority should be given to specific and rich descriptions of sexual activity, especially about genitals, fluids.
222
+ - Assemble a short story internally which is developed from the themes provided, then describe a scene into an detailed English sentences based on the central character internally.
223
+ - Split sentences into short phrases or words, and then convert them to Tags.
224
+ - Use associated Danbooru tags, E621 tags.
225
+ - Same Tags should be used only once per output.
226
+ - Anyway, keep processing until you've finished outputting message.
227
+ ```
228
+ Based on these Rules, please tell me message within 40 Tags that can generate an image for the following themes:
229
+ ''',
230
+ "With dialogue and description": f'''You are a helpful AI assistant to generate messages for AI that outputs an image when I enter a message.
231
+ The message must have the following [Tags] generated in strict accordance with the following [Rules]:
232
+ ```
233
+ [Tags]
234
+ - Words to describe full names of characters and names of series in which they appear AFAP.
235
+ - Words to describe names of the people there and their numbers, such as 2girls, 1boy.
236
+ - Words to describe their hair color, hairstyle, hair length, hair accessory, eye color, eye shape, facial expression, breast size, and clothing of them in detail, such as long hair.
237
+ - Words to describe their external features, ornaments and belongings (also specify colors, patterns, shapes) in detail.
238
+ - Words to describe their stance from head to toe in detail AFAP.
239
+ - Words to describe their acting, especially with sexual activity in detail AFAP.
240
+ - Words to describe their surroundings in detail.
241
+ - Words to describe background details, such as inside room, forest, starry sky.
242
+ [Rules]
243
+ - Any Tags should be plain text in English and don't use line breaks.
244
+ - Message is only composed of Tags in 1 line, separated by commas with spaces between Tags, in lower case English.
245
+ - Message should be in the format: "//GENBEGIN//://1girl, Tag, Tag, ..., Tag//://GENEND//".
246
+ - Preferably refer to and describe the information obtained from Danbooru. If not, describe it in own way.
247
+ - It's preferable that each Tag is a plain phrase, word, caption, Danbooru tag, or E621 tag.
248
+ - Convert any nicknames to full names first AFAP.
249
+ - If a sexual theme is given, priority should be given to specific and rich descriptions of sexual activity, especially about genitals, fluids.
250
+ - Assemble a short story internally which is developed from the themes provided, then describe a scene into an detailed English sentences based on the central character internally.
251
+ - Split sentences into short phrases or words, and then convert them to Tags.
252
+ - Use associated Danbooru tags, E621 tags.
253
+ - Same Tags should be used only once per output.
254
+ - Anyway, keep processing until you've finished outputting message.
255
+ ```
256
+ Based on these Rules, please tell me message within 40 Tags that can generate an image for the following themes,
257
+ then write the character's long actor's line composed of one's voices and moaning and voices in thought, based on the story you have assembled, in {dolphin_output_language} only,
258
+ enclosed in //VOICEBEGIN//:// and //://VOICEEND//, then describe the message you've generated in short, in {dolphin_output_language} only.:
259
+ ''', "Chat with LLM": "You are a helpful AI assistant."}
260
+
261
+
262
+ def get_dolphin_sysprompt():
263
+ return dolphin_system_prompt.get(dolphin_sysprompt_mode, "")
264
+
265
+
266
+ def get_dolphin_sysprompt_mode():
267
+ return list(dolphin_system_prompt.keys())
268
+
269
+
270
+ def select_dolphin_sysprompt(key: str):
271
+ global dolphin_sysprompt_mode
272
+ if not key in dolphin_system_prompt.keys():
273
+ dolphin_sysprompt_mode = "Default"
274
+ else:
275
+ dolphin_sysprompt_mode = key
276
+ return get_dolphin_sysprompt()
277
+
278
+
279
+ def get_dolphin_languages():
280
+ return llm_languages
281
+
282
+
283
+ def select_dolphin_language(lang: str):
284
+ global dolphin_output_language
285
+ dolphin_output_language = lang
286
+ return get_dolphin_sysprompt()
287
+
288
+
289
+ @spaces.GPU
290
+ def dolphin_respond(
291
+ message: str,
292
+ history: list[tuple[str, str]],
293
+ model: str = default_llm_model_filename,
294
+ system_message: str = get_dolphin_sysprompt(),
295
+ max_tokens: int = 1024,
296
+ temperature: float = 0.7,
297
+ top_p: float = 0.95,
298
+ top_k: int = 40,
299
+ repeat_penalty: float = 1.1,
300
+ progress=gr.Progress(track_tqdm=True),
301
+ ):
302
+ from pathlib import Path
303
+ progress(0, desc="Processing...")
304
+
305
+ if override_llm_format:
306
+ chat_template = override_llm_format
307
+ else:
308
+ chat_template = llm_models[model][1]
309
+
310
+ llm = Llama(
311
+ model_path=str(Path(f"{llm_models_dir}/{model}")),
312
+ flash_attn=True,
313
+ n_gpu_layers=81,
314
+ n_batch=1024,
315
+ n_ctx=8192,
316
+ )
317
+ provider = LlamaCppPythonProvider(llm)
318
+
319
+ agent = LlamaCppAgent(
320
+ provider,
321
+ system_prompt=f"{system_message}",
322
+ predefined_messages_formatter_type=chat_template,
323
+ debug_output=False
324
+ )
325
+
326
+ settings = provider.get_provider_default_settings()
327
+ settings.temperature = temperature
328
+ settings.top_k = top_k
329
+ settings.top_p = top_p
330
+ settings.max_tokens = max_tokens
331
+ settings.repeat_penalty = repeat_penalty
332
+ settings.stream = True
333
+
334
+ messages = BasicChatHistory()
335
+
336
+ for msn in history:
337
+ user = {
338
+ 'role': Roles.user,
339
+ 'content': msn[0]
340
+ }
341
+ assistant = {
342
+ 'role': Roles.assistant,
343
+ 'content': msn[1]
344
+ }
345
+ messages.add_message(user)
346
+ messages.add_message(assistant)
347
+
348
+ stream = agent.get_chat_response(
349
+ message,
350
+ llm_sampling_settings=settings,
351
+ chat_history=messages,
352
+ returns_streaming_generator=True,
353
+ print_output=False
354
+ )
355
+
356
+ progress(0.5, desc="Processing...")
357
+
358
+ outputs = ""
359
+ for output in stream:
360
+ outputs += output
361
+ yield [(outputs, None)]
362
+
363
+
364
+ def dolphin_parse(
365
+ history: list[tuple[str, str]],
366
+ ):
367
+ import re
368
+ if not history or len(history) < 1: return ""
369
+ try:
370
+ msg = history[-1][0]
371
+ except Exception:
372
+ return ""
373
+ m = re.findall(r'/GENBEGIN/((?:.|\s)+?)/GENEND/', msg)
374
+ raw_prompt = re.sub(r'[*/:_"#]|\n', ' ', ", ".join(m)).lower() if m else ""
375
+ prompts = list_uniq(to_list(raw_prompt) + ["nsfw", "explicit"])
376
+ return ", ".join(prompts), gr.update(interactive=True), gr.update(interactive=True)
377
+
378
+
379
+ @spaces.GPU
380
+ def dolphin_respond_auto(
381
+ message: str,
382
+ history: list[tuple[str, str]],
383
+ model: str = default_llm_model_filename,
384
+ system_message: str = get_dolphin_sysprompt(),
385
+ max_tokens: int = 1024,
386
+ temperature: float = 0.7,
387
+ top_p: float = 0.95,
388
+ top_k: int = 40,
389
+ repeat_penalty: float = 1.1,
390
+ progress=gr.Progress(track_tqdm=True),
391
+ ):
392
+ #if not is_japanese(message): return [(None, None)]
393
+
394
+ from pathlib import Path
395
+ progress(0, desc="Processing...")
396
+
397
+ if override_llm_format:
398
+ chat_template = override_llm_format
399
+ else:
400
+ chat_template = llm_models[model][1]
401
+
402
+ llm = Llama(
403
+ model_path=str(Path(f"{llm_models_dir}/{model}")),
404
+ flash_attn=True,
405
+ n_gpu_layers=81,
406
+ n_batch=1024,
407
+ n_ctx=8192,
408
+ )
409
+ provider = LlamaCppPythonProvider(llm)
410
+
411
+ agent = LlamaCppAgent(
412
+ provider,
413
+ system_prompt=f"{system_message}",
414
+ predefined_messages_formatter_type=chat_template,
415
+ debug_output=False
416
+ )
417
+
418
+ settings = provider.get_provider_default_settings()
419
+ settings.temperature = temperature
420
+ settings.top_k = top_k
421
+ settings.top_p = top_p
422
+ settings.max_tokens = max_tokens
423
+ settings.repeat_penalty = repeat_penalty
424
+ settings.stream = True
425
+
426
+ messages = BasicChatHistory()
427
+
428
+ for msn in history:
429
+ user = {
430
+ 'role': Roles.user,
431
+ 'content': msn[0]
432
+ }
433
+ assistant = {
434
+ 'role': Roles.assistant,
435
+ 'content': msn[1]
436
+ }
437
+ messages.add_message(user)
438
+ messages.add_message(assistant)
439
+
440
+ progress(0, desc="Translating...")
441
+ stream = agent.get_chat_response(
442
+ message,
443
+ llm_sampling_settings=settings,
444
+ chat_history=messages,
445
+ returns_streaming_generator=True,
446
+ print_output=False
447
+ )
448
+
449
+ progress(0.5, desc="Processing...")
450
+
451
+ outputs = ""
452
+ for output in stream:
453
+ outputs += output
454
+ yield [(outputs, None)]
455
+
456
+
457
+ def dolphin_parse_simple(
458
+ message: str,
459
+ history: list[tuple[str, str]],
460
+ ):
461
+ import re
462
+ #if not is_japanese(message) or not history or len(history) < 1: return message
463
+ if not history or len(history) < 1: return message
464
+ try:
465
+ msg = history[-1][0]
466
+ except Exception:
467
+ return ""
468
+ m = re.findall(r'/GENBEGIN/((?:.|\s)+?)/GENEND/', msg)
469
+ raw_prompt = re.sub(r'[*/:_"#]|\n', ' ', ", ".join(m)).lower() if m else ""
470
+ prompts = list_uniq(to_list(raw_prompt) + ["nsfw", "explicit"])
471
+ return ", ".join(prompts)
pre-requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pip>=23.0.0
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ huggingface_hub
2
+ scikit-build-core
3
+ https://github.com/abetlen/llama-cpp-python/releases/download/v0.2.81-cu124/llama_cpp_python-0.2.81-cp310-cp310-linux_x86_64.whl
4
+ llama-cpp-agent>=0.2.25
5
+ pybind11>=2.12
6
+ torch
7
+ torchvision
8
+ accelerate
9
+ transformers
10
+ optimum[onnxruntime]
11
+ spaces
12
+ dartrs
13
+ httpx==0.13.3
14
+ httpcore
15
+ googletrans==4.0.0rc1
16
+ git+https://github.com/huggingface/diffusers
tag_group.csv ADDED
The diff for this file is too large to render. See raw diff
 
tagger.py ADDED
@@ -0,0 +1,506 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import torch
3
+ import gradio as gr
4
+ import spaces # ZERO GPU
5
+
6
+ from transformers import (
7
+ AutoImageProcessor,
8
+ AutoModelForImageClassification,
9
+ )
10
+
11
+ WD_MODEL_NAMES = ["p1atdev/wd-swinv2-tagger-v3-hf"]
12
+ WD_MODEL_NAME = WD_MODEL_NAMES[0]
13
+
14
+ wd_model = AutoModelForImageClassification.from_pretrained(WD_MODEL_NAME, trust_remote_code=True)
15
+ wd_model.to("cuda" if torch.cuda.is_available() else "cpu")
16
+ wd_processor = AutoImageProcessor.from_pretrained(WD_MODEL_NAME, trust_remote_code=True)
17
+
18
+
19
+ def _people_tag(noun: str, minimum: int = 1, maximum: int = 5):
20
+ return (
21
+ [f"1{noun}"]
22
+ + [f"{num}{noun}s" for num in range(minimum + 1, maximum + 1)]
23
+ + [f"{maximum+1}+{noun}s"]
24
+ )
25
+
26
+
27
+ PEOPLE_TAGS = (
28
+ _people_tag("girl") + _people_tag("boy") + _people_tag("other") + ["no humans"]
29
+ )
30
+
31
+
32
+ RATING_MAP = {
33
+ "general": "safe",
34
+ "sensitive": "sensitive",
35
+ "questionable": "nsfw",
36
+ "explicit": "explicit, nsfw",
37
+ }
38
+ DANBOORU_TO_E621_RATING_MAP = {
39
+ "safe": "rating_safe",
40
+ "sensitive": "rating_safe",
41
+ "nsfw": "rating_explicit",
42
+ "explicit, nsfw": "rating_explicit",
43
+ "explicit": "rating_explicit",
44
+ "rating:safe": "rating_safe",
45
+ "rating:general": "rating_safe",
46
+ "rating:sensitive": "rating_safe",
47
+ "rating:questionable, nsfw": "rating_explicit",
48
+ "rating:explicit, nsfw": "rating_explicit",
49
+ }
50
+
51
+
52
+ def to_list(s):
53
+ return [x.strip() for x in s.split(",") if not s == ""]
54
+
55
+
56
+ def list_sub(a, b):
57
+ return [e for e in a if e not in b]
58
+
59
+
60
+ def list_uniq(l):
61
+ return sorted(set(l), key=l.index)
62
+
63
+
64
+ def load_dict_from_csv(filename):
65
+ with open(filename, 'r', encoding="utf-8") as f:
66
+ lines = f.readlines()
67
+ dict = {}
68
+ for line in lines:
69
+ parts = line.strip().split(',')
70
+ dict[parts[0]] = parts[1]
71
+ return dict
72
+
73
+
74
+ anime_series_dict = load_dict_from_csv('character_series_dict.csv')
75
+
76
+
77
+ def character_list_to_series_list(character_list):
78
+ output_series_tag = []
79
+ series_tag = ""
80
+ series_dict = anime_series_dict
81
+ for tag in character_list:
82
+ series_tag = series_dict.get(tag, "")
83
+ if tag.endswith(")"):
84
+ tags = tag.split("(")
85
+ character_tag = "(".join(tags[:-1])
86
+ if character_tag.endswith(" "):
87
+ character_tag = character_tag[:-1]
88
+ series_tag = tags[-1].replace(")", "")
89
+
90
+ if series_tag:
91
+ output_series_tag.append(series_tag)
92
+
93
+ return output_series_tag
94
+
95
+
96
+ def select_random_character(series: str, character: str):
97
+ from random import randrange
98
+ character_list = list(anime_series_dict.keys())
99
+ character = character_list[randrange(len(character_list) - 1)]
100
+ series = anime_series_dict.get(character.split(",")[0].strip(), "")
101
+ return series, character
102
+
103
+
104
+ def danbooru_to_e621(dtag, e621_dict):
105
+ def d_to_e(match, e621_dict):
106
+ dtag = match.group(0)
107
+ etag = e621_dict.get(dtag.strip().replace("_", " "), "")
108
+ if etag:
109
+ return etag
110
+ else:
111
+ return dtag
112
+
113
+ import re
114
+ tag = re.sub(r'[\w ]+', lambda wrapper: d_to_e(wrapper, e621_dict), dtag, 2)
115
+
116
+ return tag
117
+
118
+
119
+ danbooru_to_e621_dict = load_dict_from_csv('danbooru_e621.csv')
120
+
121
+
122
+ def convert_danbooru_to_e621_prompt(input_prompt: str = "", prompt_type: str = "danbooru"):
123
+ if prompt_type == "danbooru": return input_prompt
124
+ tags = input_prompt.split(",") if input_prompt else []
125
+ people_tags: list[str] = []
126
+ other_tags: list[str] = []
127
+ rating_tags: list[str] = []
128
+
129
+ e621_dict = danbooru_to_e621_dict
130
+ for tag in tags:
131
+ tag = tag.strip().replace("_", " ")
132
+ tag = danbooru_to_e621(tag, e621_dict)
133
+ if tag in PEOPLE_TAGS:
134
+ people_tags.append(tag)
135
+ elif tag in DANBOORU_TO_E621_RATING_MAP.keys():
136
+ rating_tags.append(DANBOORU_TO_E621_RATING_MAP.get(tag.replace(" ",""), ""))
137
+ else:
138
+ other_tags.append(tag)
139
+
140
+ rating_tags = sorted(set(rating_tags), key=rating_tags.index)
141
+ rating_tags = [rating_tags[0]] if rating_tags else []
142
+ rating_tags = ["explicit, nsfw"] if rating_tags and rating_tags[0] == "explicit" else rating_tags
143
+
144
+ output_prompt = ", ".join(people_tags + other_tags + rating_tags)
145
+
146
+ return output_prompt
147
+
148
+
149
+ def translate_prompt(prompt: str = ""):
150
+ def translate_to_english(prompt):
151
+ import httpcore
152
+ setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
153
+ from googletrans import Translator
154
+ translator = Translator()
155
+ try:
156
+ translated_prompt = translator.translate(prompt, src='auto', dest='en').text
157
+ return translated_prompt
158
+ except Exception as e:
159
+ return prompt
160
+
161
+ def is_japanese(s):
162
+ import unicodedata
163
+ for ch in s:
164
+ name = unicodedata.name(ch, "")
165
+ if "CJK UNIFIED" in name or "HIRAGANA" in name or "KATAKANA" in name:
166
+ return True
167
+ return False
168
+
169
+ def to_list(s):
170
+ return [x.strip() for x in s.split(",")]
171
+
172
+ prompts = to_list(prompt)
173
+ outputs = []
174
+ for p in prompts:
175
+ p = translate_to_english(p) if is_japanese(p) else p
176
+ outputs.append(p)
177
+
178
+ return ", ".join(outputs)
179
+
180
+
181
+ def translate_prompt_to_ja(prompt: str = ""):
182
+ def translate_to_japanese(prompt):
183
+ import httpcore
184
+ setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
185
+ from googletrans import Translator
186
+ translator = Translator()
187
+ try:
188
+ translated_prompt = translator.translate(prompt, src='en', dest='ja').text
189
+ return translated_prompt
190
+ except Exception as e:
191
+ return prompt
192
+
193
+ def is_japanese(s):
194
+ import unicodedata
195
+ for ch in s:
196
+ name = unicodedata.name(ch, "")
197
+ if "CJK UNIFIED" in name or "HIRAGANA" in name or "KATAKANA" in name:
198
+ return True
199
+ return False
200
+
201
+ def to_list(s):
202
+ return [x.strip() for x in s.split(",")]
203
+
204
+ prompts = to_list(prompt)
205
+ outputs = []
206
+ for p in prompts:
207
+ p = translate_to_japanese(p) if not is_japanese(p) else p
208
+ outputs.append(p)
209
+
210
+ return ", ".join(outputs)
211
+
212
+
213
+ def tags_to_ja(itag, dict):
214
+ def t_to_j(match, dict):
215
+ tag = match.group(0)
216
+ ja = dict.get(tag.strip().replace("_", " "), "")
217
+ if ja:
218
+ return ja
219
+ else:
220
+ return tag
221
+
222
+ import re
223
+ tag = re.sub(r'[\w ]+', lambda wrapper: t_to_j(wrapper, dict), itag, 2)
224
+
225
+ return tag
226
+
227
+
228
+ def convert_tags_to_ja(input_prompt: str = ""):
229
+ tags = input_prompt.split(",") if input_prompt else []
230
+ out_tags = []
231
+
232
+ tags_to_ja_dict = load_dict_from_csv('all_tags_ja_ext.csv')
233
+ dict = tags_to_ja_dict
234
+ for tag in tags:
235
+ tag = tag.strip().replace("_", " ")
236
+ tag = tags_to_ja(tag, dict)
237
+ out_tags.append(tag)
238
+
239
+ return ", ".join(out_tags)
240
+
241
+
242
+ enable_auto_recom_prompt = True
243
+
244
+
245
+ animagine_ps = to_list("anime artwork, anime style, vibrant, studio anime, highly detailed, masterpiece, best quality, very aesthetic, absurdres")
246
+ animagine_nps = to_list("lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]")
247
+ pony_ps = to_list("source_anime, score_9, score_8_up, score_7_up, masterpiece, best quality, very aesthetic, absurdres")
248
+ pony_nps = to_list("source_pony, source_furry, source_cartoon, score_6, score_5, score_4, busty, ugly face, mutated hands, low res, blurry face, black and white, the simpsons, overwatch, apex legends")
249
+ other_ps = to_list("anime artwork, anime style, vibrant, studio anime, highly detailed, cinematic photo, 35mm photograph, film, bokeh, professional, 4k, highly detailed")
250
+ other_nps = to_list("photo, deformed, black and white, realism, disfigured, low contrast, drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly")
251
+ default_ps = to_list("score_9, score_8_up, score_7_up, highly detailed, masterpiece, best quality, very aesthetic, absurdres")
252
+ default_nps = to_list("score_6, score_5, score_4, lowres, (bad), text, error, fewer, extra, missing, worst quality, jpeg artifacts, low quality, watermark, unfinished, displeasing, oldest, early, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]")
253
+ def insert_recom_prompt(prompt: str = "", neg_prompt: str = "", type: str = "None"):
254
+ global enable_auto_recom_prompt
255
+ prompts = to_list(prompt)
256
+ neg_prompts = to_list(neg_prompt)
257
+
258
+ prompts = list_sub(prompts, animagine_ps + pony_ps)
259
+ neg_prompts = list_sub(neg_prompts, animagine_nps + pony_nps)
260
+
261
+ last_empty_p = [""] if not prompts and type != "None" else []
262
+ last_empty_np = [""] if not neg_prompts and type != "None" else []
263
+
264
+ if type == "Auto":
265
+ enable_auto_recom_prompt = True
266
+ else:
267
+ enable_auto_recom_prompt = False
268
+ if type == "Animagine":
269
+ prompts = prompts + animagine_ps
270
+ neg_prompts = neg_prompts + animagine_nps
271
+ elif type == "Pony":
272
+ prompts = prompts + pony_ps
273
+ neg_prompts = neg_prompts + pony_nps
274
+
275
+ prompt = ", ".join(list_uniq(prompts) + last_empty_p)
276
+ neg_prompt = ", ".join(list_uniq(neg_prompts) + last_empty_np)
277
+
278
+ return prompt, neg_prompt
279
+
280
+
281
+ def load_model_prompt_dict():
282
+ import json
283
+ dict = {}
284
+ try:
285
+ with open('model_dict.json', encoding='utf-8') as f:
286
+ dict = json.load(f)
287
+ except Exception:
288
+ pass
289
+ return dict
290
+
291
+
292
+ model_prompt_dict = load_model_prompt_dict()
293
+
294
+
295
+ def insert_model_recom_prompt(prompt: str = "", neg_prompt: str = "", model_name: str = "None"):
296
+ if not model_name or not enable_auto_recom_prompt: return prompt, neg_prompt
297
+ prompts = to_list(prompt)
298
+ neg_prompts = to_list(neg_prompt)
299
+ prompts = list_sub(prompts, animagine_ps + pony_ps + other_ps)
300
+ neg_prompts = list_sub(neg_prompts, animagine_nps + pony_nps + other_nps)
301
+ last_empty_p = [""] if not prompts and type != "None" else []
302
+ last_empty_np = [""] if not neg_prompts and type != "None" else []
303
+ ps = []
304
+ nps = []
305
+ if model_name in model_prompt_dict.keys():
306
+ ps = to_list(model_prompt_dict[model_name]["prompt"])
307
+ nps = to_list(model_prompt_dict[model_name]["negative_prompt"])
308
+ else:
309
+ ps = default_ps
310
+ nps = default_nps
311
+ prompts = prompts + ps
312
+ neg_prompts = neg_prompts + nps
313
+ prompt = ", ".join(list_uniq(prompts) + last_empty_p)
314
+ neg_prompt = ", ".join(list_uniq(neg_prompts) + last_empty_np)
315
+ return prompt, neg_prompt
316
+
317
+
318
+ tag_group_dict = load_dict_from_csv('tag_group.csv')
319
+
320
+
321
+ def remove_specific_prompt(input_prompt: str = "", keep_tags: str = "all"):
322
+ def is_dressed(tag):
323
+ import re
324
+ p = re.compile(r'dress|cloth|uniform|costume|vest|sweater|coat|shirt|jacket|blazer|apron|leotard|hood|sleeve|skirt|shorts|pant|loafer|ribbon|necktie|bow|collar|glove|sock|shoe|boots|wear|emblem')
325
+ return p.search(tag)
326
+
327
+ def is_background(tag):
328
+ import re
329
+ p = re.compile(r'background|outline|light|sky|build|day|screen|tree|city')
330
+ return p.search(tag)
331
+
332
+ un_tags = ['solo']
333
+ group_list = ['groups', 'body_parts', 'attire', 'posture', 'objects', 'creatures', 'locations', 'disambiguation_pages', 'commonly_misused_tags', 'phrases', 'verbs_and_gerunds', 'subjective', 'nudity', 'sex_objects', 'sex', 'sex_acts', 'image_composition', 'artistic_license', 'text', 'year_tags', 'metatags']
334
+ keep_group_dict = {
335
+ "body": ['groups', 'body_parts'],
336
+ "dress": ['groups', 'body_parts', 'attire'],
337
+ "all": group_list,
338
+ }
339
+
340
+ def is_necessary(tag, keep_tags, group_dict):
341
+ if keep_tags == "all":
342
+ return True
343
+ elif tag in un_tags or group_dict.get(tag, "") in explicit_group:
344
+ return False
345
+ elif keep_tags == "body" and is_dressed(tag):
346
+ return False
347
+ elif is_background(tag):
348
+ return False
349
+ else:
350
+ return True
351
+
352
+ if keep_tags == "all": return input_prompt
353
+ keep_group = keep_group_dict.get(keep_tags, keep_group_dict["body"])
354
+ explicit_group = list(set(group_list) ^ set(keep_group))
355
+
356
+ tags = input_prompt.split(",") if input_prompt else []
357
+ people_tags: list[str] = []
358
+ other_tags: list[str] = []
359
+
360
+ group_dict = tag_group_dict
361
+ for tag in tags:
362
+ tag = tag.strip().replace("_", " ")
363
+ if tag in PEOPLE_TAGS:
364
+ people_tags.append(tag)
365
+ elif is_necessary(tag, keep_tags, group_dict):
366
+ other_tags.append(tag)
367
+
368
+ output_prompt = ", ".join(people_tags + other_tags)
369
+
370
+ return output_prompt
371
+
372
+
373
+ def sort_taglist(tags: list[str]):
374
+ if not tags: return []
375
+ character_tags: list[str] = []
376
+ series_tags: list[str] = []
377
+ people_tags: list[str] = []
378
+ group_list = ['groups', 'body_parts', 'attire', 'posture', 'objects', 'creatures', 'locations', 'disambiguation_pages', 'commonly_misused_tags', 'phrases', 'verbs_and_gerunds', 'subjective', 'nudity', 'sex_objects', 'sex', 'sex_acts', 'image_composition', 'artistic_license', 'text', 'year_tags', 'metatags']
379
+ group_tags = {}
380
+ other_tags: list[str] = []
381
+ rating_tags: list[str] = []
382
+
383
+ group_dict = tag_group_dict
384
+ group_set = set(group_dict.keys())
385
+ character_set = set(anime_series_dict.keys())
386
+ series_set = set(anime_series_dict.values())
387
+ rating_set = set(DANBOORU_TO_E621_RATING_MAP.keys()) | set(DANBOORU_TO_E621_RATING_MAP.values())
388
+
389
+ for tag in tags:
390
+ tag = tag.strip().replace("_", " ")
391
+ if tag in PEOPLE_TAGS:
392
+ people_tags.append(tag)
393
+ elif tag in rating_set:
394
+ rating_tags.append(tag)
395
+ elif tag in group_set:
396
+ elem = group_dict[tag]
397
+ group_tags[elem] = group_tags[elem] + [tag] if elem in group_tags else [tag]
398
+ elif tag in character_set:
399
+ character_tags.append(tag)
400
+ elif tag in series_set:
401
+ series_tags.append(tag)
402
+ else:
403
+ other_tags.append(tag)
404
+
405
+ output_group_tags: list[str] = []
406
+ for k in group_list:
407
+ output_group_tags.extend(group_tags.get(k, []))
408
+
409
+ rating_tags = [rating_tags[0]] if rating_tags else []
410
+ rating_tags = ["explicit, nsfw"] if rating_tags and rating_tags[0] == "explicit" else rating_tags
411
+
412
+ output_tags = character_tags + series_tags + people_tags + output_group_tags + other_tags + rating_tags
413
+
414
+ return output_tags
415
+
416
+
417
+ def sort_tags(tags: str):
418
+ if not tags: return ""
419
+ taglist: list[str] = []
420
+ for tag in tags.split(","):
421
+ taglist.append(tag.strip())
422
+ taglist = list(filter(lambda x: x != "", taglist))
423
+ return ", ".join(sort_taglist(taglist))
424
+
425
+
426
+ def postprocess_results(results: dict[str, float], general_threshold: float, character_threshold: float):
427
+ results = {
428
+ k: v for k, v in sorted(results.items(), key=lambda item: item[1], reverse=True)
429
+ }
430
+
431
+ rating = {}
432
+ character = {}
433
+ general = {}
434
+
435
+ for k, v in results.items():
436
+ if k.startswith("rating:"):
437
+ rating[k.replace("rating:", "")] = v
438
+ continue
439
+ elif k.startswith("character:"):
440
+ character[k.replace("character:", "")] = v
441
+ continue
442
+
443
+ general[k] = v
444
+
445
+ character = {k: v for k, v in character.items() if v >= character_threshold}
446
+ general = {k: v for k, v in general.items() if v >= general_threshold}
447
+
448
+ return rating, character, general
449
+
450
+
451
+ def gen_prompt(rating: list[str], character: list[str], general: list[str]):
452
+ people_tags: list[str] = []
453
+ other_tags: list[str] = []
454
+ rating_tag = RATING_MAP[rating[0]]
455
+
456
+ for tag in general:
457
+ if tag in PEOPLE_TAGS:
458
+ people_tags.append(tag)
459
+ else:
460
+ other_tags.append(tag)
461
+
462
+ all_tags = people_tags + other_tags
463
+
464
+ return ", ".join(all_tags)
465
+
466
+
467
+ @spaces.GPU()
468
+ def predict_tags(image: Image.Image, general_threshold: float = 0.3, character_threshold: float = 0.8):
469
+ inputs = wd_processor.preprocess(image, return_tensors="pt")
470
+
471
+ outputs = wd_model(**inputs.to(wd_model.device, wd_model.dtype))
472
+ logits = torch.sigmoid(outputs.logits[0]) # take the first logits
473
+
474
+ # get probabilities
475
+ results = {
476
+ wd_model.config.id2label[i]: float(logit.float()) for i, logit in enumerate(logits)
477
+ }
478
+ # rating, character, general
479
+ rating, character, general = postprocess_results(
480
+ results, general_threshold, character_threshold
481
+ )
482
+ prompt = gen_prompt(
483
+ list(rating.keys()), list(character.keys()), list(general.keys())
484
+ )
485
+ output_series_tag = ""
486
+ output_series_list = character_list_to_series_list(character.keys())
487
+ if output_series_list:
488
+ output_series_tag = output_series_list[0]
489
+ else:
490
+ output_series_tag = ""
491
+ return output_series_tag, ", ".join(character.keys()), prompt, gr.update(interactive=True),
492
+
493
+
494
+ def predict_tags_wd(image: Image.Image, input_tags: str, algo: list[str], general_threshold: float = 0.3, character_threshold: float = 0.8):
495
+ if not "Use WD Tagger" in algo and len(algo) != 0:
496
+ return "", "", input_tags, gr.update(interactive=True),
497
+ return predict_tags(image, general_threshold, character_threshold)
498
+
499
+
500
+ def compose_prompt_to_copy(character: str, series: str, general: str):
501
+ characters = character.split(",") if character else []
502
+ serieses = series.split(",") if series else []
503
+ generals = general.split(",") if general else []
504
+ tags = characters + serieses + generals
505
+ cprompt = ",".join(tags) if tags else ""
506
+ return cprompt
utils.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from dartrs.v2 import AspectRatioTag, LengthTag, RatingTag, IdentityTag
3
+
4
+
5
+ V2_ASPECT_RATIO_OPTIONS: list[AspectRatioTag] = [
6
+ "ultra_wide",
7
+ "wide",
8
+ "square",
9
+ "tall",
10
+ "ultra_tall",
11
+ ]
12
+ V2_RATING_OPTIONS: list[RatingTag] = [
13
+ "sfw",
14
+ "general",
15
+ "sensitive",
16
+ "nsfw",
17
+ "questionable",
18
+ "explicit",
19
+ ]
20
+ V2_LENGTH_OPTIONS: list[LengthTag] = [
21
+ "very_short",
22
+ "short",
23
+ "medium",
24
+ "long",
25
+ "very_long",
26
+ ]
27
+ V2_IDENTITY_OPTIONS: list[IdentityTag] = [
28
+ "none",
29
+ "lax",
30
+ "strict",
31
+ ]
32
+
33
+
34
+ # ref: https://qiita.com/tregu148/items/fccccbbc47d966dd2fc2
35
+ def gradio_copy_text(_text: None):
36
+ gr.Info("Copied!")
37
+
38
+
39
+ COPY_ACTION_JS = """\
40
+ (inputs, _outputs) => {
41
+ // inputs is the string value of the input_text
42
+ if (inputs.trim() !== "") {
43
+ navigator.clipboard.writeText(inputs);
44
+ }
45
+ }"""