Update apps/gradio_app.py
Browse files- apps/gradio_app.py +187 -187
apps/gradio_app.py
CHANGED
|
@@ -1,187 +1,187 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import random
|
| 5 |
-
from gradio_app.inference import run_inference
|
| 6 |
-
from gradio_app.examples import load_examples, select_example
|
| 7 |
-
from gradio_app.project_info import (
|
| 8 |
-
NAME,
|
| 9 |
-
CONTENT_DESCRIPTION,
|
| 10 |
-
CONTENT_IN_1,
|
| 11 |
-
CONTENT_OUT_1
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
def run_setup_script():
|
| 15 |
-
setup_script = os.path.join(os.path.dirname(__file__), "gradio_app", "setup_scripts.py")
|
| 16 |
-
try:
|
| 17 |
-
result = subprocess.run(["python", setup_script], capture_output=True, text=True, check=True)
|
| 18 |
-
return result.stdout
|
| 19 |
-
except subprocess.CalledProcessError as e:
|
| 20 |
-
print(f"Setup script failed with error: {e.stderr}")
|
| 21 |
-
return f"Setup script failed: {e.stderr}"
|
| 22 |
-
|
| 23 |
-
def stop_app():
|
| 24 |
-
"""Function to stop the Gradio app."""
|
| 25 |
-
try:
|
| 26 |
-
gr.Interface.close_all() # Attempt to close all running Gradio interfaces
|
| 27 |
-
return "Application stopped successfully."
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return f"Error stopping application: {str(e)}"
|
| 30 |
-
|
| 31 |
-
def create_gui():
|
| 32 |
-
try:
|
| 33 |
-
custom_css = open("apps/gradio_app/static/style.css").read()
|
| 34 |
-
except FileNotFoundError:
|
| 35 |
-
print("Error: style.css not found at gradio_app/static/style.css")
|
| 36 |
-
custom_css = "" # Fallback to empty CSS if file is missing
|
| 37 |
-
|
| 38 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 39 |
-
gr.Markdown(NAME)
|
| 40 |
-
gr.HTML(CONTENT_DESCRIPTION)
|
| 41 |
-
gr.HTML(CONTENT_IN_1)
|
| 42 |
-
|
| 43 |
-
with gr.Row():
|
| 44 |
-
with gr.Column(scale=2):
|
| 45 |
-
input_image = gr.Image(type="filepath", label="Input Image")
|
| 46 |
-
prompt = gr.Textbox(
|
| 47 |
-
label="Prompt",
|
| 48 |
-
value="a man is doing yoga"
|
| 49 |
-
)
|
| 50 |
-
negative_prompt = gr.Textbox(
|
| 51 |
-
label="Negative Prompt",
|
| 52 |
-
value="monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
with gr.Row():
|
| 56 |
-
width = gr.Slider(
|
| 57 |
-
minimum=256,
|
| 58 |
-
maximum=1024,
|
| 59 |
-
value=512,
|
| 60 |
-
step=64,
|
| 61 |
-
label="Width"
|
| 62 |
-
)
|
| 63 |
-
height = gr.Slider(
|
| 64 |
-
minimum=256,
|
| 65 |
-
maximum=1024,
|
| 66 |
-
value=512,
|
| 67 |
-
step=64,
|
| 68 |
-
label="Height"
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 72 |
-
num_steps = gr.Slider(
|
| 73 |
-
minimum=1,
|
| 74 |
-
maximum=100,
|
| 75 |
-
value=30,
|
| 76 |
-
step=1,
|
| 77 |
-
label="Number of Inference Steps"
|
| 78 |
-
)
|
| 79 |
-
use_random_seed = gr.Checkbox(label="Use Random Seed", value=False)
|
| 80 |
-
seed = gr.Slider(
|
| 81 |
-
minimum=0,
|
| 82 |
-
maximum=2**32 - 1,
|
| 83 |
-
value=42,
|
| 84 |
-
step=1,
|
| 85 |
-
label="Random Seed",
|
| 86 |
-
visible=True
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
guidance_scale = gr.Slider(
|
| 90 |
-
minimum=1.0,
|
| 91 |
-
maximum=20.0,
|
| 92 |
-
value=7.5,
|
| 93 |
-
step=0.1,
|
| 94 |
-
label="Guidance Scale"
|
| 95 |
-
)
|
| 96 |
-
controlnet_conditioning_scale = gr.Slider(
|
| 97 |
-
minimum=0.0,
|
| 98 |
-
maximum=1.0,
|
| 99 |
-
value=1.0,
|
| 100 |
-
step=0.1,
|
| 101 |
-
label="ControlNet Conditioning Scale"
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
with gr.Column(scale=3):
|
| 105 |
-
output_images = gr.Image(label="Generated Images")
|
| 106 |
-
output_message = gr.Textbox(label="Status")
|
| 107 |
-
|
| 108 |
-
submit_button = gr.Button("Generate Images", elem_classes="submit-btn")
|
| 109 |
-
stop_button = gr.Button("Stop Application", elem_classes="stop-btn")
|
| 110 |
-
|
| 111 |
-
def update_seed_visibility(use_random):
|
| 112 |
-
return gr.update(visible=not use_random)
|
| 113 |
-
|
| 114 |
-
use_random_seed.change(
|
| 115 |
-
fn=update_seed_visibility,
|
| 116 |
-
inputs=use_random_seed,
|
| 117 |
-
outputs=seed
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
# Load examples
|
| 121 |
-
examples_data = load_examples(os.path.join("apps", "gradio_app",
|
| 122 |
-
"assets", "examples", "Stable-Diffusion-2.1-Openpose-ControlNet"))
|
| 123 |
-
examples_component = gr.Examples(
|
| 124 |
-
examples=examples_data,
|
| 125 |
-
inputs=[
|
| 126 |
-
input_image,
|
| 127 |
-
prompt,
|
| 128 |
-
negative_prompt,
|
| 129 |
-
output_images,
|
| 130 |
-
num_steps,
|
| 131 |
-
seed,
|
| 132 |
-
width,
|
| 133 |
-
height,
|
| 134 |
-
guidance_scale,
|
| 135 |
-
controlnet_conditioning_scale,
|
| 136 |
-
use_random_seed
|
| 137 |
-
],
|
| 138 |
-
outputs=[
|
| 139 |
-
input_image,
|
| 140 |
-
prompt,
|
| 141 |
-
negative_prompt,
|
| 142 |
-
output_images,
|
| 143 |
-
num_steps,
|
| 144 |
-
seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
controlnet_conditioning_scale,
|
| 149 |
-
use_random_seed,
|
| 150 |
-
output_message
|
| 151 |
-
],
|
| 152 |
-
fn=select_example,
|
| 153 |
-
cache_examples=False,
|
| 154 |
-
label="Examples: Yoga Poses"
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
submit_button.click(
|
| 158 |
-
fn=run_inference,
|
| 159 |
-
inputs=[
|
| 160 |
-
input_image,
|
| 161 |
-
prompt,
|
| 162 |
-
negative_prompt,
|
| 163 |
-
num_steps,
|
| 164 |
-
seed,
|
| 165 |
-
width,
|
| 166 |
-
height,
|
| 167 |
-
guidance_scale,
|
| 168 |
-
controlnet_conditioning_scale,
|
| 169 |
-
use_random_seed,
|
| 170 |
-
],
|
| 171 |
-
outputs=[output_images, output_message]
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
stop_button.click(
|
| 175 |
-
fn=stop_app,
|
| 176 |
-
inputs=[],
|
| 177 |
-
outputs=[output_message]
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
gr.HTML(CONTENT_OUT_1)
|
| 181 |
-
|
| 182 |
-
return demo
|
| 183 |
-
|
| 184 |
-
if __name__ == "__main__":
|
| 185 |
-
run_setup_script()
|
| 186 |
-
demo = create_gui()
|
| 187 |
-
demo.launch(
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
+
from gradio_app.inference import run_inference
|
| 6 |
+
from gradio_app.examples import load_examples, select_example
|
| 7 |
+
from gradio_app.project_info import (
|
| 8 |
+
NAME,
|
| 9 |
+
CONTENT_DESCRIPTION,
|
| 10 |
+
CONTENT_IN_1,
|
| 11 |
+
CONTENT_OUT_1
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def run_setup_script():
|
| 15 |
+
setup_script = os.path.join(os.path.dirname(__file__), "gradio_app", "setup_scripts.py")
|
| 16 |
+
try:
|
| 17 |
+
result = subprocess.run(["python", setup_script], capture_output=True, text=True, check=True)
|
| 18 |
+
return result.stdout
|
| 19 |
+
except subprocess.CalledProcessError as e:
|
| 20 |
+
print(f"Setup script failed with error: {e.stderr}")
|
| 21 |
+
return f"Setup script failed: {e.stderr}"
|
| 22 |
+
|
| 23 |
+
def stop_app():
|
| 24 |
+
"""Function to stop the Gradio app."""
|
| 25 |
+
try:
|
| 26 |
+
gr.Interface.close_all() # Attempt to close all running Gradio interfaces
|
| 27 |
+
return "Application stopped successfully."
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error stopping application: {str(e)}"
|
| 30 |
+
|
| 31 |
+
def create_gui():
|
| 32 |
+
try:
|
| 33 |
+
custom_css = open("apps/gradio_app/static/style.css").read()
|
| 34 |
+
except FileNotFoundError:
|
| 35 |
+
print("Error: style.css not found at gradio_app/static/style.css")
|
| 36 |
+
custom_css = "" # Fallback to empty CSS if file is missing
|
| 37 |
+
|
| 38 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 39 |
+
gr.Markdown(NAME)
|
| 40 |
+
gr.HTML(CONTENT_DESCRIPTION)
|
| 41 |
+
gr.HTML(CONTENT_IN_1)
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
with gr.Column(scale=2):
|
| 45 |
+
input_image = gr.Image(type="filepath", label="Input Image")
|
| 46 |
+
prompt = gr.Textbox(
|
| 47 |
+
label="Prompt",
|
| 48 |
+
value="a man is doing yoga"
|
| 49 |
+
)
|
| 50 |
+
negative_prompt = gr.Textbox(
|
| 51 |
+
label="Negative Prompt",
|
| 52 |
+
value="monochrome, lowres, bad anatomy, worst quality, low quality"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
width = gr.Slider(
|
| 57 |
+
minimum=256,
|
| 58 |
+
maximum=1024,
|
| 59 |
+
value=512,
|
| 60 |
+
step=64,
|
| 61 |
+
label="Width"
|
| 62 |
+
)
|
| 63 |
+
height = gr.Slider(
|
| 64 |
+
minimum=256,
|
| 65 |
+
maximum=1024,
|
| 66 |
+
value=512,
|
| 67 |
+
step=64,
|
| 68 |
+
label="Height"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 72 |
+
num_steps = gr.Slider(
|
| 73 |
+
minimum=1,
|
| 74 |
+
maximum=100,
|
| 75 |
+
value=30,
|
| 76 |
+
step=1,
|
| 77 |
+
label="Number of Inference Steps"
|
| 78 |
+
)
|
| 79 |
+
use_random_seed = gr.Checkbox(label="Use Random Seed", value=False)
|
| 80 |
+
seed = gr.Slider(
|
| 81 |
+
minimum=0,
|
| 82 |
+
maximum=2**32 - 1,
|
| 83 |
+
value=42,
|
| 84 |
+
step=1,
|
| 85 |
+
label="Random Seed",
|
| 86 |
+
visible=True
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
guidance_scale = gr.Slider(
|
| 90 |
+
minimum=1.0,
|
| 91 |
+
maximum=20.0,
|
| 92 |
+
value=7.5,
|
| 93 |
+
step=0.1,
|
| 94 |
+
label="Guidance Scale"
|
| 95 |
+
)
|
| 96 |
+
controlnet_conditioning_scale = gr.Slider(
|
| 97 |
+
minimum=0.0,
|
| 98 |
+
maximum=1.0,
|
| 99 |
+
value=1.0,
|
| 100 |
+
step=0.1,
|
| 101 |
+
label="ControlNet Conditioning Scale"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
with gr.Column(scale=3):
|
| 105 |
+
output_images = gr.Image(label="Generated Images")
|
| 106 |
+
output_message = gr.Textbox(label="Status")
|
| 107 |
+
|
| 108 |
+
submit_button = gr.Button("Generate Images", elem_classes="submit-btn")
|
| 109 |
+
stop_button = gr.Button("Stop Application", elem_classes="stop-btn")
|
| 110 |
+
|
| 111 |
+
def update_seed_visibility(use_random):
|
| 112 |
+
return gr.update(visible=not use_random)
|
| 113 |
+
|
| 114 |
+
use_random_seed.change(
|
| 115 |
+
fn=update_seed_visibility,
|
| 116 |
+
inputs=use_random_seed,
|
| 117 |
+
outputs=seed
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Load examples
|
| 121 |
+
examples_data = load_examples(os.path.join("apps", "gradio_app",
|
| 122 |
+
"assets", "examples", "Stable-Diffusion-2.1-Openpose-ControlNet"))
|
| 123 |
+
examples_component = gr.Examples(
|
| 124 |
+
examples=examples_data,
|
| 125 |
+
inputs=[
|
| 126 |
+
input_image,
|
| 127 |
+
prompt,
|
| 128 |
+
negative_prompt,
|
| 129 |
+
output_images,
|
| 130 |
+
num_steps,
|
| 131 |
+
seed,
|
| 132 |
+
width,
|
| 133 |
+
height,
|
| 134 |
+
guidance_scale,
|
| 135 |
+
controlnet_conditioning_scale,
|
| 136 |
+
use_random_seed
|
| 137 |
+
],
|
| 138 |
+
outputs=[
|
| 139 |
+
input_image,
|
| 140 |
+
prompt,
|
| 141 |
+
negative_prompt,
|
| 142 |
+
output_images,
|
| 143 |
+
num_steps,
|
| 144 |
+
seed,
|
| 145 |
+
width,
|
| 146 |
+
height,
|
| 147 |
+
guidance_scale,
|
| 148 |
+
controlnet_conditioning_scale,
|
| 149 |
+
use_random_seed,
|
| 150 |
+
output_message
|
| 151 |
+
],
|
| 152 |
+
fn=select_example,
|
| 153 |
+
cache_examples=False,
|
| 154 |
+
label="Examples: Yoga Poses"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
submit_button.click(
|
| 158 |
+
fn=run_inference,
|
| 159 |
+
inputs=[
|
| 160 |
+
input_image,
|
| 161 |
+
prompt,
|
| 162 |
+
negative_prompt,
|
| 163 |
+
num_steps,
|
| 164 |
+
seed,
|
| 165 |
+
width,
|
| 166 |
+
height,
|
| 167 |
+
guidance_scale,
|
| 168 |
+
controlnet_conditioning_scale,
|
| 169 |
+
use_random_seed,
|
| 170 |
+
],
|
| 171 |
+
outputs=[output_images, output_message]
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
stop_button.click(
|
| 175 |
+
fn=stop_app,
|
| 176 |
+
inputs=[],
|
| 177 |
+
outputs=[output_message]
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
gr.HTML(CONTENT_OUT_1)
|
| 181 |
+
|
| 182 |
+
return demo
|
| 183 |
+
|
| 184 |
+
if __name__ == "__main__":
|
| 185 |
+
run_setup_script()
|
| 186 |
+
demo = create_gui()
|
| 187 |
+
demo.launch()
|