File size: 8,185 Bytes
4d9eab3
3e6d270
 
41aec71
4d9eab3
 
 
3e6d270
4d9eab3
 
 
 
 
 
 
3e6d270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d9eab3
3e6d270
4d9eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd1ee2
3e6d270
 
 
4d9eab3
 
 
3e6d270
 
a020a22
ce2f083
3e6d270
a020a22
3e6d270
 
 
 
 
 
4d7833f
4d9eab3
 
3e6d270
 
 
a020a22
3e6d270
 
 
 
3cd1ee2
 
a020a22
4d9eab3
 
e6c3592
 
 
 
 
 
 
4d9eab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf31f7
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import gradio as gr
from stablepy import Model_Diffusers

from StableGR import (search_civitai, download_civitai, select_civitai_item, add_civitai_item, get_civitai_tag, select_civitai_all_item,
                           update_civitai_selection, update_civitai_checkbox, from_civitai_checkbox,
                           CIVITAI_TYPE, CIVITAI_BASEMODEL, CIVITAI_SORT, CIVITAI_PERIOD, CIVITAI_FILETYPE, download_file)


css = """
.title { font-size: 3em; align-items: center; text-align: center; }
.info { align-items: center; text-align: center; }
.block.result { margin: 1em 0; padding: 1em; box-shadow: 0 0 3px 3px #664422, 0 0 3px 2px #664422 inset; border-radius: 6px; background: #665544; }
.desc [src$='#float'] { float: right; margin: 20px; }
"""




# Define the function to generate images
def generate_image(model_id, prompt, lora_A, num_steps, guidance_scale, sampler, img_width, img_height):
    model = Model_Diffusers(
        base_model_id=model_id,
        task_name='txt2img',
    )

    image, info_image = model(
        prompt=prompt,
        lora_A=lora_A,
        num_steps=num_steps,
        guidance_scale=guidance_scale,
        sampler=sampler,
        img_width=img_width,
        img_height=img_height,
    )
    return image[0]


with gr.Blocks(fill_width=True, css=css) as demo:
    with gr.Column():
        gr.Markdown("# StableGR", elem_classes="title")
        state = gr.State(value={})
        with gr.Accordion("Search Civitai", open=True):
            with gr.Row():
                search_civitai_type = gr.CheckboxGroup(label="Type", choices=CIVITAI_TYPE, value=["Checkpoint", "LORA"])
                search_civitai_basemodel = gr.CheckboxGroup(label="Base Model", choices=CIVITAI_BASEMODEL, value=[])
                search_civitai_filetype = gr.CheckboxGroup(label="File type", choices=CIVITAI_FILETYPE, value=["Model"])
            with gr.Row():
                search_civitai_sort = gr.Radio(label="Sort", choices=CIVITAI_SORT, value=CIVITAI_SORT[0])
                search_civitai_period = gr.Radio(label="Period", choices=CIVITAI_PERIOD, value="Month")
                search_civitai_limit = gr.Number(label="Limit", minimum=1, maximum=100, step=1, value=100)
                search_civitai_page = gr.Number(label="Page", info="If 0, retrieve all pages", minimum=0, maximum=10, step=1, value=1)
            with gr.Row(equal_height=True):
                search_civitai_query = gr.Textbox(label="Query", placeholder="flux", lines=1)
                search_civitai_tag = gr.Dropdown(label="Tag", choices=get_civitai_tag(), value=get_civitai_tag()[0], allow_custom_value=True)
                search_civitai_user = gr.Textbox(label="Username", lines=1)
                search_civitai_submit = gr.Button("Search on Civitai")
            with gr.Accordion("Results", open=True):
                with gr.Row():
                    search_civitai_desc = gr.Markdown(value="", visible=False, elem_classes="desc")
                    search_civitai_json = gr.JSON(value={}, visible=False)
                with gr.Row(equal_height=True):
                    with gr.Column(scale=9):
                        with gr.Accordion("Select from Gallery", open=False):
                            search_civitai_gallery = gr.Gallery([], label="Results", allow_preview=False, columns=5, elem_id="gallery", show_share_button=False, interactive=False)
                        with gr.Accordion("Select by Checkbox", open=False):
                            search_civitai_result_checkbox = gr.CheckboxGroup(label="", choices=[], value=[])
                        search_civitai_result = gr.Dropdown(label="Search Results", choices=[("", "")], value=[],
                                                            allow_custom_value=True, visible=True, multiselect=True)
                        search_civitai_result_info = gr.Markdown("Search result.", elem_classes="info")
                    with gr.Column(scale=1):
                        search_civitai_add = gr.Button("Add to download URLs")
                        search_civitai_select_all = gr.Button("Select All", variant="secondary", size="sm")
        with gr.Group():
            dl_url = gr.Textbox(label="Download URL(s)", placeholder="https://civitai.com/api/download/models/28907\n...", value="", lines=3, max_lines=255)
            with gr.Column():
                civitai_key = gr.Textbox(label="Your Civitai Key", value="", max_lines=1)
                gr.Markdown("Your Civitai API key is available at [https://civitai.com/user/account](https://civitai.com/user/account).", elem_classes="info")
        
        with gr.Row():
            run_base = gr.Button(value="Download Base Model", variant="primary")
            run_lora = gr.Button(value="Download Lora", variant="primary")
        uploaded_urls = gr.CheckboxGroup(visible=False, choices=[], value=None) # hidden
        urls_md = gr.Markdown("<br><br>", elem_classes="result")
        urls_remain = gr.Textbox("Remaining URLs", value="", show_copy_button=True, visible=False)
    with gr.Column():
        base_model = gr.File(label="Base Models")
        lora_A = gr.File(label="Lora")
        with gr.Row():
            prompt = gr.Textbox(label="Prompt", value="A highly detailed portrait of an underwater city, with towering spires and domes rising up from the ocean floor")
        
        num_steps = gr.Slider(label="Number of Steps", minimum=1, maximum=100, value=30, step=1)
        guidance_scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=20.0, value=7.5, step=0.1)
        sampler = gr.Dropdown(label="Sampler", choices=["DPM++ 2M", "OtherSampler1", "OtherSampler2"], value="DPM++ 2M")
        img_width = gr.Slider(label="Image Width", minimum=64, maximum=2048, value=512, step=64)
        img_height = gr.Slider(label="Image Height", minimum=64, maximum=2048, value=1024, step=64)
        generate_button = gr.Button("Generate Image")
        output_image = gr.Image(label="output")

    gr.on(
        triggers=[run_base.click],
        fn=download_file,
        inputs=[dl_url, civitai_key],
        outputs=base_model,
        queue=True,
    )
    gr.on(
        triggers=[run_lora.click],
        fn=download_file,
        inputs=[dl_url, civitai_key],
        outputs=lora_A,
        queue=True,
    )
    gr.on(
        triggers=[generate_button.click],
        fn=generate_image,
        inputs=[prompt, num_steps, guidance_scale, sampler, img_width, img_height],
        outputs=output_image,
        queue=True,
    )
    gr.on(
        triggers=[search_civitai_submit.click, search_civitai_query.submit, search_civitai_user.submit],
        fn=search_civitai,
        inputs=[search_civitai_query, search_civitai_type, search_civitai_basemodel, search_civitai_sort,
                search_civitai_period, search_civitai_tag, search_civitai_user, search_civitai_limit,
                search_civitai_page, search_civitai_filetype, civitai_key, search_civitai_gallery, state],
        outputs=[search_civitai_result, search_civitai_desc, search_civitai_submit, search_civitai_query, search_civitai_gallery,
                 search_civitai_result_checkbox, search_civitai_result_info, state],
        queue=False,
        show_api=False,
    )
    search_civitai_result.change(select_civitai_item, [search_civitai_result, state], [search_civitai_desc, search_civitai_json, state], queue=False, show_api=False)\
    .success(update_civitai_checkbox, [search_civitai_result], [search_civitai_result_checkbox], queue=True, show_api=False)
    search_civitai_result_checkbox.select(from_civitai_checkbox, [search_civitai_result_checkbox], [search_civitai_result], queue=False, show_api=False)
    search_civitai_add.click(add_civitai_item, [search_civitai_result, dl_url], [dl_url], queue=False, show_api=False)
    search_civitai_select_all.click(select_civitai_all_item, [search_civitai_select_all, state], [search_civitai_select_all, search_civitai_result], queue=False, show_api=False)
    search_civitai_gallery.select(update_civitai_selection, [search_civitai_result, state], [search_civitai_result], queue=False, show_api=False)

demo.queue()
demo.launch(ssr_mode=False, share=True)