import spaces
import gradio as gr
import numpy as np

# DiffuseCraft
from dc import (infer, _infer, pass_result, get_diffusers_model_list, get_samplers,
    get_vaes, enable_model_recom_prompt, enable_diffusers_model_detail,
    get_t2i_model_info, get_all_lora_tupled_list, update_loras,
    apply_lora_prompt, download_my_lora, search_civitai_lora,
    select_civitai_lora, search_civitai_lora_json,
    preset_quality, preset_styles, process_style_prompt)
# Translator
from llmdolphin import (dolphin_respond_auto, dolphin_parse_simple,
    get_llm_formats, get_dolphin_model_format, get_dolphin_models,
    get_dolphin_model_info, select_dolphin_model, select_dolphin_format, get_dolphin_sysprompt)
# Tagger
from tagger.v2 import v2_upsampling_prompt, V2_ALL_MODELS
from tagger.utils import (gradio_copy_text, gradio_copy_prompt, COPY_ACTION_JS,
    V2_ASPECT_RATIO_OPTIONS, V2_RATING_OPTIONS, V2_LENGTH_OPTIONS, V2_IDENTITY_OPTIONS)
from tagger.tagger import (predict_tags_wd, convert_danbooru_to_e621_prompt,
    remove_specific_prompt, insert_recom_prompt, compose_prompt_to_copy,
    translate_prompt, select_random_character)
from tagger.fl2sd3longcap import predict_tags_fl2_sd3
def description_ui():
    gr.Markdown(
        """
## Danbooru Tags Transformer V2 Demo with WD Tagger & SD3 Long Captioner
(Image =>) Prompt => Upsampled longer prompt
- Mod of p1atdev's [Danbooru Tags Transformer V2 Demo](https://huggingface.co/spaces/p1atdev/danbooru-tags-transformer-v2) and [WD Tagger with 🤗 transformers](https://huggingface.co/spaces/p1atdev/wd-tagger-transformers).
- Models: p1atdev's [wd-swinv2-tagger-v3-hf](https://huggingface.co/p1atdev/wd-swinv2-tagger-v3-hf), [dart-v2-moe-sft](https://huggingface.co/p1atdev/dart-v2-moe-sft), [dart-v2-sft](https://huggingface.co/p1atdev/dart-v2-sft)\
, gokaygokay's [Florence-2-SD3-Captioner](https://huggingface.co/gokaygokay/Florence-2-SD3-Captioner)
"""
    )


MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1216

css = """
#container {  margin: 0 auto; !important; }
#col-container { margin: 0 auto;  align: center; !important; }
#result { display: block; max-width: 520px; max-height: 520px; width: 520px; height: 520px; align: center; !important; }
#model-info { text-align: center; !important; }
"""

with gr.Blocks(css=css, fill_width=True, elem_id="container") as demo:
    with gr.Tab("Image Generator"):
        with gr.Column(elem_id="col-container"):
            with gr.Row():
                prompt = gr.Text(label="Prompt", show_label=False, lines=1, max_lines=8, placeholder="Enter your prompt", container=False)
            
            with gr.Row():
                run_button = gr.Button("Run")
                run_translate_button = gr.Button("Translate")

            with gr.Row():
                result = gr.Image(label="Result", elem_id="result", show_label=False, interactive=False,
                                  show_download_button=True, show_share_button=False, container=True)
                
            with gr.Accordion("Advanced Settings", open=False):
                with gr.Row():
                    negative_prompt = gr.Text(label="Negative prompt", lines=1, max_lines=6, placeholder="Enter a negative prompt",
                        value="(low quality, worst quality:1.2), very displeasing, watermark, signature, ugly")

                with gr.Row():
                    seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
                    randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
                    width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 832
                    height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024) # 1216
                    guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=30.0, step=0.1, value=7)
                    num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=100, step=1, value=28)

                with gr.Row():
                    model_name = gr.Dropdown(label="Model", info="You can enter a huggingface model repo_id to want to use.", choices=get_diffusers_model_list(), value=get_diffusers_model_list()[0], allow_custom_value=True, interactive=True)
                    model_info = gr.Markdown(elem_id="model-info")
                    model_detail = gr.Checkbox(label="Show detail of model in list", value=False)

                with gr.Row():
                    sampler = gr.Dropdown(label="Sampler", choices=get_samplers(), value="Euler a")
                    vae_model = gr.Dropdown(label="VAE Model", choices=get_vaes(), value=get_vaes()[0])

                with gr.Accordion("LoRA", open=True, visible=True):
                    with gr.Row():
                        with gr.Group():
                            lora1 = gr.Dropdown(label="LoRA 1", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True)
                            lora1_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 1: weight")
                            with gr.Row():
                                lora1_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
                                lora1_copy = gr.Button(value="Copy example to prompt", visible=False)
                            lora1_md = gr.Markdown(value="", visible=False)
                        with gr.Group():
                            lora2 = gr.Dropdown(label="LoRA 2", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True)
                            lora2_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 2: weight")
                            with gr.Row():
                                lora2_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
                                lora2_copy = gr.Button(value="Copy example to prompt", visible=False)
                            lora2_md = gr.Markdown(value="", visible=False)
                        with gr.Group():
                            lora3 = gr.Dropdown(label="LoRA 3", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True)
                            lora3_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 3: weight")
                            with gr.Row():
                                lora3_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
                                lora3_copy = gr.Button(value="Copy example to prompt", visible=False)
                            lora3_md = gr.Markdown(value="", visible=False)
                        with gr.Group():
                            lora4 = gr.Dropdown(label="LoRA 4", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True)
                            lora4_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 4: weight")
                            with gr.Row():
                                lora4_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
                                lora4_copy = gr.Button(value="Copy example to prompt", visible=False)
                            lora4_md = gr.Markdown(value="", visible=False)
                        with gr.Group():
                            lora5 = gr.Dropdown(label="LoRA 5", choices=get_all_lora_tupled_list(), value="", allow_custom_value=True)
                            lora5_wt = gr.Slider(minimum=-2, maximum=2, step=0.01, value=1.00, label="LoRA 5: weight")
                            with gr.Row():
                                lora5_info = gr.Textbox(label="", info="Example of prompt:", value="", show_copy_button=True, interactive=False, visible=False)
                                lora5_copy = gr.Button(value="Copy example to prompt", visible=False)
                            lora5_md = gr.Markdown(value="", visible=False)
                    with gr.Accordion("From URL", open=True, visible=True):
                        with gr.Row():
                            lora_search_civitai_query = gr.Textbox(label="Query", placeholder="oomuro sakurako...", lines=1)
                            lora_search_civitai_basemodel = gr.CheckboxGroup(label="Search LoRA for", choices=["Pony", "SD 1.5", "SDXL 1.0"], value=["Pony", "SDXL 1.0"])
                            lora_search_civitai_submit = gr.Button("Search on Civitai")
                        lora_search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value="", allow_custom_value=True, visible=False)
                        lora_search_civitai_json = gr.JSON(value={}, visible=False)
                        lora_search_civitai_desc = gr.Markdown(value="", visible=False)
                        lora_download_url = gr.Textbox(label="URL", placeholder="http://...my_lora_url.safetensors", lines=1)
                        lora_download = gr.Button("Get and set LoRA and apply to prompt")

                with gr.Row():
                    recom_prompt = gr.Checkbox(label="Recommended prompt", value=True)
                    quality_selector = gr.Radio(label="Quality Tag Presets", interactive=True, choices=list(preset_quality.keys()), value="None")
                    style_selector = gr.Radio(label="Style Presets", interactive=True, choices=list(preset_styles.keys()), value="None")
                
                with gr.Accordion("Translation Settings", open=False):
                    with gr.Row():
                        chatbot = gr.Chatbot(likeable=False, render_markdown=False, visible=False) # component for auto-translation
                        chat_model = gr.Dropdown(choices=get_dolphin_models(), value=get_dolphin_models()[0][1], allow_custom_value=True, label="Model")
                        chat_model_info = gr.Markdown(value=get_dolphin_model_info(get_dolphin_models()[0][1]), label="Model info")
                        chat_format = gr.Dropdown(choices=get_llm_formats(), value=get_dolphin_model_format(get_dolphin_models()[0][1]), label="Message format")
                        chat_sysmsg = gr.Textbox(value=get_dolphin_sysprompt(), label="System message")
                        chat_tokens = gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max tokens")
                        chat_temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
                        chat_topp = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
                        chat_topk = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="Top-k")
                        chat_rp = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")

            examples = gr.Examples(
                examples = [
                    ["souryuu asuka langley, 1girl, neon genesis evangelion, plugsuit, pilot suit, red bodysuit, sitting, crossing legs, black eye patch, cat hat, throne, symmetrical, looking down, from bottom, looking at viewer, outdoors"],
                    ["sailor moon, magical girl transformation, sparkles and ribbons, soft pastel colors, crescent moon motif, starry night sky background, shoujo manga style"],
                    ["kafuu chino, 1girl, solo"],
                    ["1girl"],
                    ["beautiful sunset"],
                ],
                inputs=[prompt],
            )

    gr.on( #lambda x: None, inputs=None, outputs=result).then(
        triggers=[run_button.click, prompt.submit],
        fn=infer,
        inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
                guidance_scale, num_inference_steps, model_name,
                lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
                sampler, vae_model],
        outputs=[result],
        queue=True,
        show_progress="full",
        show_api=True,
    )

    gr.on( #lambda x: None, inputs=None, outputs=result).then(
        triggers=[run_translate_button.click],
        fn=_infer, # dummy fn for api
        inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
                guidance_scale, num_inference_steps, model_name,
                lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
                sampler, vae_model],
        outputs=[result],
        queue=False,
        show_api=True,
        api_name="infer_translate",
    ).success(
        fn=dolphin_respond_auto,
        inputs=[prompt, chatbot],
        outputs=[chatbot],
        queue=True,
        show_progress="full",
        show_api=False,
    ).success(
        fn=dolphin_parse_simple,
        inputs=[prompt, chatbot],
        outputs=[prompt],
        queue=False,
        show_api=False,
    ).success(
        fn=infer,
        inputs=[prompt, negative_prompt, seed, randomize_seed, width, height,
                guidance_scale, num_inference_steps, model_name,
                lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt,
                sampler, vae_model],
        outputs=[result],
        queue=True,
        show_progress="full",
        show_api=False,
    ).success(lambda: None, None, chatbot, queue=False, show_api=False)\
    .success(pass_result, [result], [result], queue=False, show_api=False) # dummy fn for api

    gr.on(
        triggers=[lora1.change, lora1_wt.change, lora2.change, lora2_wt.change, lora3.change, lora3_wt.change,
                   lora4.change, lora4_wt.change, lora5.change, lora5_wt.change],
        fn=update_loras,
        inputs=[prompt, lora1, lora1_wt, lora2, lora2_wt, lora3, lora3_wt, lora4, lora4_wt, lora5, lora5_wt],
        outputs=[prompt, lora1, lora1_wt, lora1_info, lora1_copy, lora1_md,
                 lora2, lora2_wt, lora2_info, lora2_copy, lora2_md, lora3, lora3_wt, lora3_info, lora3_copy, lora3_md, 
                 lora4, lora4_wt, lora4_info, lora4_copy, lora4_md, lora5, lora5_wt, lora5_info, lora5_copy, lora5_md],
        queue=False,
        trigger_mode="once",
        show_api=False,
    )
    lora1_copy.click(apply_lora_prompt, [prompt, lora1_info], [prompt], queue=False, show_api=False)
    lora2_copy.click(apply_lora_prompt, [prompt, lora2_info], [prompt], queue=False, show_api=False)
    lora3_copy.click(apply_lora_prompt, [prompt, lora3_info], [prompt], queue=False, show_api=False)
    lora4_copy.click(apply_lora_prompt, [prompt, lora4_info], [prompt], queue=False, show_api=False)
    lora5_copy.click(apply_lora_prompt, [prompt, lora5_info], [prompt], queue=False, show_api=False)

    gr.on(
        triggers=[lora_search_civitai_submit.click, lora_search_civitai_query.submit],
        fn=search_civitai_lora,
        inputs=[lora_search_civitai_query, lora_search_civitai_basemodel],
        outputs=[lora_search_civitai_result, lora_search_civitai_desc, lora_search_civitai_submit, lora_search_civitai_query],
        scroll_to_output=True,
        queue=True,
        show_api=False,
    )
    lora_search_civitai_json.change(search_civitai_lora_json, [lora_search_civitai_query, lora_search_civitai_basemodel], [lora_search_civitai_json], queue=True, show_api=True)  # fn for api
    lora_search_civitai_result.change(select_civitai_lora, [lora_search_civitai_result], [lora_download_url, lora_search_civitai_desc], scroll_to_output=True, queue=False, show_api=False)
    gr.on(
        triggers=[lora_download.click, lora_download_url.submit],
        fn=download_my_lora,
        inputs=[lora_download_url,lora1, lora2, lora3, lora4, lora5],
        outputs=[lora1, lora2, lora3, lora4, lora5],
        scroll_to_output=True,
        queue=True,
        show_api=False,
    )

    recom_prompt.change(enable_model_recom_prompt, [recom_prompt], [recom_prompt], queue=False, show_api=False)
    gr.on(
        triggers=[quality_selector.change, style_selector.change],
        fn=process_style_prompt,
        inputs=[prompt, negative_prompt, style_selector, quality_selector],
        outputs=[prompt, negative_prompt],
        queue=False,
        trigger_mode="once",
    )

    model_detail.change(enable_diffusers_model_detail, [model_detail, model_name], [model_detail, model_name], queue=False, show_api=False)
    model_name.change(get_t2i_model_info, [model_name], [model_info], queue=False, show_api=False)

    chat_model.change(select_dolphin_model, [chat_model], [chat_model, chat_format, chat_model_info], queue=True, show_progress="full", show_api=False)\
    .success(lambda: None, None, chatbot, queue=False, show_api=False)
    chat_format.change(select_dolphin_format, [chat_format], [chat_format], queue=False, show_api=False)\
    .success(lambda: None, None, chatbot, queue=False, show_api=False)

    # Tagger
    with gr.Tab("Tags Transformer with Tagger"):
        with gr.Column():
                with gr.Group():
                    input_image = gr.Image(label="Input image", type="pil", sources=["upload", "clipboard"], height=256)
                    with gr.Accordion(label="Advanced options", open=False):
                        general_threshold = gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.3, step=0.01, interactive=True)
                        character_threshold = gr.Slider(label="Character threshold", minimum=0.0, maximum=1.0, value=0.8, step=0.01, interactive=True)
                        input_tag_type = gr.Radio(label="Convert tags to", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="danbooru")
                        recom_prompt = gr.Radio(label="Insert reccomended prompt", choices=["None", "Animagine", "Pony"], value="None", interactive=True)
                    image_algorithms = gr.CheckboxGroup(["Use WD Tagger", "Use Florence-2-SD3-Long-Captioner"], label="Algorithms", value=["Use WD Tagger"])
                    keep_tags = gr.Radio(label="Remove tags leaving only the following", choices=["body", "dress", "all"], value="all")
                    generate_from_image_btn = gr.Button(value="GENERATE TAGS FROM IMAGE", size="lg", variant="primary")
                with gr.Group():
                    with gr.Row():
                        input_character = gr.Textbox(label="Character tags", placeholder="hatsune miku")
                        input_copyright = gr.Textbox(label="Copyright tags", placeholder="vocaloid")
                        random_character = gr.Button(value="Random character 🎲", size="sm")
                    input_general = gr.TextArea(label="General tags", lines=4, placeholder="1girl, ...", value="")
                    input_tags_to_copy = gr.Textbox(value="", visible=False)
                    with gr.Row():
                        copy_input_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
                        copy_prompt_btn_input = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
                    translate_input_prompt_button = gr.Button(value="Translate prompt to English", size="sm", variant="secondary")
                    tag_type = gr.Radio(label="Output tag conversion", info="danbooru for Animagine, e621 for Pony.", choices=["danbooru", "e621"], value="e621", visible=False)
                    input_rating = gr.Radio(label="Rating", choices=list(V2_RATING_OPTIONS), value="explicit")
                    with gr.Accordion(label="Advanced options", open=False):
                        input_aspect_ratio = gr.Radio(label="Aspect ratio", info="The aspect ratio of the image.", choices=list(V2_ASPECT_RATIO_OPTIONS), value="square")
                        input_length = gr.Radio(label="Length", info="The total length of the tags.", choices=list(V2_LENGTH_OPTIONS), value="very_long")
                        input_identity = gr.Radio(label="Keep identity", info="How strictly to keep the identity of the character or subject. If you specify the detail of subject in the prompt, you should choose `strict`. Otherwise, choose `none` or `lax`. `none` is very creative but sometimes ignores the input prompt.", choices=list(V2_IDENTITY_OPTIONS), value="lax")                    
                        input_ban_tags = gr.Textbox(label="Ban tags", info="Tags to ban from the output.", placeholder="alternate costumen, ...", value="censored")
                        model_name = gr.Dropdown(label="Model", choices=list(V2_ALL_MODELS.keys()), value=list(V2_ALL_MODELS.keys())[0])
                        dummy_np = gr.Textbox(label="Negative prompt", value="", visible=False)
                        recom_animagine = gr.Textbox(label="Animagine reccomended prompt", value="Animagine", visible=False)
                        recom_pony = gr.Textbox(label="Pony reccomended prompt", value="Pony", visible=False)
                    generate_btn = gr.Button(value="GENERATE TAGS", size="lg", variant="primary")
                with gr.Row():
                    with gr.Group():
                        output_text = gr.TextArea(label="Output tags", interactive=False, show_copy_button=True)
                        with gr.Row():
                            copy_btn = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
                            copy_prompt_btn = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)
                    with gr.Group():
                        output_text_pony = gr.TextArea(label="Output tags (Pony e621 style)", interactive=False, show_copy_button=True)
                        with gr.Row():
                            copy_btn_pony = gr.Button(value="Copy to clipboard", size="sm", interactive=False)
                            copy_prompt_btn_pony = gr.Button(value="Copy to primary prompt", size="sm", interactive=False)

        random_character.click(select_random_character, [input_copyright, input_character], [input_copyright, input_character], queue=False, show_api=False)

        translate_input_prompt_button.click(translate_prompt, [input_general], [input_general], queue=False, show_api=False)
        translate_input_prompt_button.click(translate_prompt, [input_character], [input_character], queue=False, show_api=False)
        translate_input_prompt_button.click(translate_prompt, [input_copyright], [input_copyright], queue=False, show_api=False)

        generate_from_image_btn.click(
            lambda: ("", "", ""), None, [input_copyright, input_character, input_general], queue=False, show_api=False,
        ).success(
            predict_tags_wd,
            [input_image, input_general, image_algorithms, general_threshold, character_threshold],
            [input_copyright, input_character, input_general, copy_input_btn],
            show_api=False,
        ).success(
            predict_tags_fl2_sd3, [input_image, input_general, image_algorithms], [input_general], show_api=False,
        ).success(
            remove_specific_prompt, [input_general, keep_tags], [input_general], queue=False, show_api=False,
        ).success(
            convert_danbooru_to_e621_prompt, [input_general, input_tag_type], [input_general], queue=False, show_api=False,
        ).success(
            insert_recom_prompt, [input_general, dummy_np, recom_prompt], [input_general, dummy_np], queue=False, show_api=False,
        ).success(lambda: gr.update(interactive=True), None, [copy_prompt_btn_input], queue=False, show_api=False)
        copy_input_btn.click(compose_prompt_to_copy, [input_character, input_copyright, input_general], [input_tags_to_copy], show_api=False)\
            .success(gradio_copy_text, [input_tags_to_copy], js=COPY_ACTION_JS, show_api=False)
        copy_prompt_btn_input.click(compose_prompt_to_copy, inputs=[input_character, input_copyright, input_general], outputs=[input_tags_to_copy], show_api=False)\
            .success(gradio_copy_prompt, inputs=[input_tags_to_copy], outputs=[prompt], show_api=False)
        
        generate_btn.click(
            v2_upsampling_prompt,
            [model_name, input_copyright, input_character, input_general,
            input_rating, input_aspect_ratio, input_length, input_identity, input_ban_tags],
            [output_text],
            show_api=False,
        ).success(
            convert_danbooru_to_e621_prompt, [output_text, tag_type], [output_text_pony], queue=False, show_api=False,
        ).success(
            insert_recom_prompt, [output_text, dummy_np, recom_animagine], [output_text, dummy_np], queue=False, show_api=False,
        ).success(
            insert_recom_prompt, [output_text_pony, dummy_np, recom_pony], [output_text_pony, dummy_np], queue=False, show_api=False,
        ).success(lambda: (gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)),
                   None, [copy_btn, copy_btn_pony, copy_prompt_btn, copy_prompt_btn_pony], queue=False, show_api=False)
        copy_btn.click(gradio_copy_text, [output_text], js=COPY_ACTION_JS, show_api=False)
        copy_btn_pony.click(gradio_copy_text, [output_text_pony], js=COPY_ACTION_JS, show_api=False)
        copy_prompt_btn.click(gradio_copy_prompt, inputs=[output_text], outputs=[prompt], show_api=False)
        copy_prompt_btn_pony.click(gradio_copy_prompt, inputs=[output_text_pony], outputs=[prompt], show_api=False)

demo.queue()
demo.launch()