Upload 8 files
Browse files- apps/gradio_app.py +96 -96
- apps/gradio_app/examples.py +24 -17
apps/gradio_app.py
CHANGED
@@ -1,97 +1,97 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import subprocess
|
4 |
-
from gradio_app.project_info import (
|
5 |
-
CONTENT_DESCRIPTION,
|
6 |
-
CONTENT_IN_1, CONTENT_IN_2,
|
7 |
-
CONTENT_OUT_1, CONTENT_OUT_2
|
8 |
-
)
|
9 |
-
from gradio_app.inference import run_inference
|
10 |
-
from gradio_app.examples import load_examples, select_example
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
gr.Markdown(
|
30 |
-
gr.
|
31 |
-
gr.
|
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 |
-
submit_button = gr.Button("Run Inference")
|
60 |
-
|
61 |
-
with gr.Column(scale=3):
|
62 |
-
output_image = gr.Image(
|
63 |
-
label="Output Image",
|
64 |
-
elem_classes="output-image"
|
65 |
-
)
|
66 |
-
output_text = gr.Textbox(label="Status")
|
67 |
-
|
68 |
-
outer_scale.change(
|
69 |
-
fn=lambda x: x,
|
70 |
-
inputs=outer_scale,
|
71 |
-
outputs=outer_scale,
|
72 |
-
js=outer_scale_warning
|
73 |
-
)
|
74 |
-
|
75 |
-
gr.Examples(
|
76 |
-
examples=[[input_img, output_img, outer_scale] for input_img, output_img, outer_scale in examples_data],
|
77 |
-
inputs=[input_image, outer_scale],
|
78 |
-
label="Example Inputs",
|
79 |
-
examples_per_page=4,
|
80 |
-
fn=select_example,
|
81 |
-
outputs=[input_image, outer_scale, output_image, output_text]
|
82 |
-
)
|
83 |
-
|
84 |
-
submit_button.click(
|
85 |
-
fn=run_inference,
|
86 |
-
inputs=[input_image, model_id, outer_scale],
|
87 |
-
outputs=[output_image, output_text]
|
88 |
-
)
|
89 |
-
gr.HTML(CONTENT_OUT_1)
|
90 |
-
gr.Markdown(CONTENT_OUT_2)
|
91 |
-
|
92 |
-
return demo
|
93 |
-
|
94 |
-
if __name__ == "__main__":
|
95 |
-
run_setup_script()
|
96 |
-
demo = create_gui()
|
97 |
demo.launch(debug=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import subprocess
|
4 |
+
from gradio_app.project_info import (
|
5 |
+
CONTENT_DESCRIPTION,
|
6 |
+
CONTENT_IN_1, CONTENT_IN_2,
|
7 |
+
CONTENT_OUT_1, CONTENT_OUT_2
|
8 |
+
)
|
9 |
+
from gradio_app.inference import run_inference
|
10 |
+
from gradio_app.examples import load_examples, select_example
|
11 |
+
|
12 |
+
def run_setup_script():
|
13 |
+
setup_script = os.path.join(os.path.dirname(__file__), "gradio_app", "setup_scripts.py")
|
14 |
+
try:
|
15 |
+
result = subprocess.run(["python", setup_script], capture_output=True, text=True, check=True)
|
16 |
+
return result.stdout
|
17 |
+
except subprocess.CalledProcessError as e:
|
18 |
+
print(f"Setup script failed with error: {e.stderr}")
|
19 |
+
return f"Setup script failed: {e.stderr}"
|
20 |
+
|
21 |
+
def create_gui():
|
22 |
+
# Load CSS and JavaScript
|
23 |
+
custom_css = open(os.path.join(os.path.dirname(__file__), "gradio_app", "static", "styles.css")).read()
|
24 |
+
outer_scale_warning = open(os.path.join(os.path.dirname(__file__), "gradio_app", "static", "outer_scale_warning.js")).read()
|
25 |
+
|
26 |
+
with gr.Blocks(css=custom_css) as demo:
|
27 |
+
gr.Markdown("# Anime Super Resolution 🖼️")
|
28 |
+
gr.Markdown(CONTENT_DESCRIPTION)
|
29 |
+
gr.Markdown(CONTENT_IN_1)
|
30 |
+
gr.HTML(CONTENT_IN_2)
|
31 |
+
with gr.Row():
|
32 |
+
with gr.Column(scale=2):
|
33 |
+
input_image = gr.Image(
|
34 |
+
type="filepath",
|
35 |
+
label="Input Image",
|
36 |
+
elem_classes="input-image"
|
37 |
+
)
|
38 |
+
model_id = gr.Textbox(
|
39 |
+
label="Model ID",
|
40 |
+
value="danhtran2mind/Real-ESRGAN-Anime-finetuning"
|
41 |
+
)
|
42 |
+
outer_scale = gr.Slider(
|
43 |
+
minimum=1,
|
44 |
+
maximum=8,
|
45 |
+
step=1,
|
46 |
+
value=2,
|
47 |
+
label="Outer Scale",
|
48 |
+
elem_id="outer-scale-slider"
|
49 |
+
)
|
50 |
+
warning_text = gr.HTML(elem_id="warning-text")
|
51 |
+
gr.Markdown(
|
52 |
+
"**Note:** For optimal output quality, set `Outer Scale` to a value between 1 and 4. "
|
53 |
+
"**Values greater than 4 are not recommended**. "
|
54 |
+
"Please ensure `Outer Scale` is greater than or equal to `Inner Scale` (default: 4)."
|
55 |
+
)
|
56 |
+
examples_data = load_examples()
|
57 |
+
if not examples_data:
|
58 |
+
gr.Warning("No examples found. Check the 'apps/assets/examples' directory.")
|
59 |
+
submit_button = gr.Button("Run Inference")
|
60 |
+
|
61 |
+
with gr.Column(scale=3):
|
62 |
+
output_image = gr.Image(
|
63 |
+
label="Output Image",
|
64 |
+
elem_classes="output-image"
|
65 |
+
)
|
66 |
+
output_text = gr.Textbox(label="Status")
|
67 |
+
|
68 |
+
outer_scale.change(
|
69 |
+
fn=lambda x: x,
|
70 |
+
inputs=outer_scale,
|
71 |
+
outputs=outer_scale,
|
72 |
+
js=outer_scale_warning
|
73 |
+
)
|
74 |
+
|
75 |
+
gr.Examples(
|
76 |
+
examples=[[input_img, output_img, outer_scale] for input_img, output_img, outer_scale in examples_data],
|
77 |
+
inputs=[input_image, outer_scale],
|
78 |
+
label="Example Inputs",
|
79 |
+
examples_per_page=4,
|
80 |
+
fn=select_example,
|
81 |
+
outputs=[input_image, outer_scale, output_image, output_text]
|
82 |
+
)
|
83 |
+
|
84 |
+
submit_button.click(
|
85 |
+
fn=run_inference,
|
86 |
+
inputs=[input_image, model_id, outer_scale],
|
87 |
+
outputs=[output_image, output_text]
|
88 |
+
)
|
89 |
+
gr.HTML(CONTENT_OUT_1)
|
90 |
+
gr.Markdown(CONTENT_OUT_2)
|
91 |
+
|
92 |
+
return demo
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
run_setup_script()
|
96 |
+
demo = create_gui()
|
97 |
demo.launch(debug=True)
|
apps/gradio_app/examples.py
CHANGED
@@ -5,30 +5,37 @@ import gradio as gr
|
|
5 |
|
6 |
def load_examples():
|
7 |
examples = []
|
8 |
-
examples_base_path = os.path.join("
|
9 |
|
10 |
for folder in ["1", "2", "3", "4"]:
|
11 |
folder_path = os.path.join(examples_base_path, folder)
|
12 |
config_path = os.path.join(folder_path, "config.json")
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
return examples
|
30 |
|
31 |
def select_example(evt: gr.SelectData, examples_data):
|
|
|
|
|
32 |
example_index = evt.index
|
33 |
-
|
34 |
-
return
|
|
|
5 |
|
6 |
def load_examples():
|
7 |
examples = []
|
8 |
+
examples_base_path = os.path.join(os.path.dirname(__file__), "..", "assets", "examples", "Real-ESRGAN-Anime-finetuning")
|
9 |
|
10 |
for folder in ["1", "2", "3", "4"]:
|
11 |
folder_path = os.path.join(examples_base_path, folder)
|
12 |
config_path = os.path.join(folder_path, "config.json")
|
13 |
|
14 |
+
try:
|
15 |
+
if os.path.exists(config_path):
|
16 |
+
with open(config_path, 'r') as f:
|
17 |
+
config = json.load(f)
|
18 |
+
input_filename = config.get("input_file", "input.jpg")
|
19 |
+
output_filename = config.get("output_file", "output.jpg")
|
20 |
+
outer_scale = config.get("outer_scale", 4)
|
21 |
+
|
22 |
+
input_image_path = os.path.join(folder_path, input_filename)
|
23 |
+
output_image_path = os.path.join(folder_path, output_filename)
|
24 |
+
|
25 |
+
if os.path.exists(input_image_path) and os.path.exists(output_image_path):
|
26 |
+
# Verify images can be opened
|
27 |
+
Image.open(input_image_path).close()
|
28 |
+
Image.open(output_image_path).close()
|
29 |
+
examples.append([input_image_path, output_image_path, outer_scale])
|
30 |
+
except (json.JSONDecodeError, OSError) as e:
|
31 |
+
print(f"Error loading example in folder {folder}: {e}")
|
32 |
+
continue
|
33 |
|
34 |
return examples
|
35 |
|
36 |
def select_example(evt: gr.SelectData, examples_data):
|
37 |
+
if not examples_data:
|
38 |
+
return None, 2, None, "No examples available"
|
39 |
example_index = evt.index
|
40 |
+
input_image_path, output_image_path, outer_scale = examples_data[example_index]
|
41 |
+
return input_image_path, outer_scale, output_image_path, f"Loaded example with Outer Scale: {outer_scale}"
|