File size: 6,205 Bytes
ad35335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import subprocess
import gradio as gr
import random
from gradio_app.inference import run_inference
from gradio_app.examples import load_examples, select_example
from gradio_app.project_info import (
    NAME, 
    CONTENT_DESCRIPTION, 
    CONTENT_IN_1, 
    CONTENT_OUT_1
)

def run_setup_script():
    setup_script = os.path.join(os.path.dirname(__file__), "gradio_app", "setup_scripts.py")
    try:
        result = subprocess.run(["python", setup_script], capture_output=True, text=True, check=True)
        return result.stdout
    except subprocess.CalledProcessError as e:
        print(f"Setup script failed with error: {e.stderr}")
        return f"Setup script failed: {e.stderr}"

def stop_app():
    """Function to stop the Gradio app."""
    try:
        gr.Interface.close_all()  # Attempt to close all running Gradio interfaces
        return "Application stopped successfully."
    except Exception as e:
        return f"Error stopping application: {str(e)}"

def create_gui():
    try:
        custom_css = open("apps/gradio_app/static/style.css").read()
    except FileNotFoundError:
        print("Error: style.css not found at gradio_app/static/style.css")
        custom_css = ""  # Fallback to empty CSS if file is missing

    with gr.Blocks(css=custom_css) as demo:
        gr.Markdown(NAME)
        gr.HTML(CONTENT_DESCRIPTION)
        gr.HTML(CONTENT_IN_1)

        with gr.Row():
            with gr.Column(scale=2):
                input_image = gr.Image(type="filepath", label="Input Image")
                prompt = gr.Textbox(
                    label="Prompt",
                    value="a man is doing yoga"
                )
                negative_prompt = gr.Textbox(
                    label="Negative Prompt",
                    value="monochrome, lowres, bad anatomy, worst quality, low quality"
                )
                
                with gr.Row():
                    width = gr.Slider(
                        minimum=256,
                        maximum=1024,
                        value=512,
                        step=64,
                        label="Width"
                    )
                    height = gr.Slider(
                        minimum=256,
                        maximum=1024,
                        value=512,
                        step=64,
                        label="Height"
                    )
                
                with gr.Accordion("Advanced Settings", open=False):
                    num_steps = gr.Slider(
                        minimum=1,
                        maximum=100,
                        value=30,
                        step=1,
                        label="Number of Inference Steps"
                    )
                    use_random_seed = gr.Checkbox(label="Use Random Seed", value=False)
                    seed = gr.Slider(
                        minimum=0,
                        maximum=2**32 - 1,
                        value=42,
                        step=1,
                        label="Random Seed",
                        visible=True
                    )
                    
                    guidance_scale = gr.Slider(
                        minimum=1.0,
                        maximum=20.0,
                        value=7.5,
                        step=0.1,
                        label="Guidance Scale"
                    )
                    controlnet_conditioning_scale = gr.Slider(
                        minimum=0.0,
                        maximum=1.0,
                        value=1.0,
                        step=0.1,
                        label="ControlNet Conditioning Scale"
                    )  
                    
            with gr.Column(scale=3):
                output_images = gr.Image(label="Generated Images")
                output_message = gr.Textbox(label="Status")
                
                submit_button = gr.Button("Generate Images", elem_classes="submit-btn")
                stop_button = gr.Button("Stop Application", elem_classes="stop-btn")

        def update_seed_visibility(use_random):
            return gr.update(visible=not use_random)
        
        use_random_seed.change(
            fn=update_seed_visibility,
            inputs=use_random_seed,
            outputs=seed
        )
        
        # Load examples
        examples_data = load_examples(os.path.join("apps", "gradio_app", 
            "assets", "examples", "Stable-Diffusion-2.1-Openpose-ControlNet"))
        examples_component = gr.Examples(
            examples=examples_data,
            inputs=[
                input_image,
                prompt,
                negative_prompt,
                output_images,
                num_steps,
                seed,
                width,
                height,
                guidance_scale,
                controlnet_conditioning_scale,
                use_random_seed
            ],
            outputs=[
                input_image,
                prompt,
                negative_prompt,
                output_images,
                num_steps,
                seed,
                width,
                height,
                guidance_scale,
                controlnet_conditioning_scale,
                use_random_seed,
                output_message
            ],
            fn=select_example,
            cache_examples=False,
            label="Examples: Yoga Poses"
        )
        
        submit_button.click(
            fn=run_inference,
            inputs=[
                input_image,
                prompt,
                negative_prompt,
                num_steps,
                seed,
                width,
                height,
                guidance_scale,
                controlnet_conditioning_scale,
                use_random_seed,
            ],
            outputs=[output_images, output_message]
        )
        
        stop_button.click(
            fn=stop_app,
            inputs=[],
            outputs=[output_message]
        )
        
        gr.HTML(CONTENT_OUT_1)
        
    return demo

if __name__ == "__main__":
    run_setup_script()
    demo = create_gui()
    demo.launch()