danhtran2mind commited on
Commit
aa6de37
·
verified ·
1 Parent(s): 365d1fc

Upload 8 files

Browse files
Files changed (2) hide show
  1. apps/gradio_app.py +96 -96
  2. 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
- def run_setup_script():
14
- setup_script = os.path.join(os.path.dirname(__file__),
15
- "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 create_gui():
24
- # Load CSS and JavaScript
25
- custom_css = open("apps/gradio_app/static/styles.css").read()
26
- outer_scale_warning = open("apps/gradio_app/static/outer_scale_warning.js").read()
27
-
28
- with gr.Blocks(css=custom_css) as demo:
29
- gr.Markdown("# Anime Super Resolution 🖼️")
30
- gr.Markdown(CONTENT_DESCRIPTION)
31
- gr.Markdown(CONTENT_IN_1)
32
- gr.HTML(CONTENT_IN_2)
33
- with gr.Row():
34
- with gr.Column(scale=2):
35
- input_image = gr.Image(
36
- type="filepath",
37
- label="Input Image",
38
- elem_classes="input-image"
39
- )
40
- model_id = gr.Textbox(
41
- label="Model ID",
42
- value="danhtran2mind/Real-ESRGAN-Anime-finetuning"
43
- )
44
- outer_scale = gr.Slider(
45
- minimum=1,
46
- maximum=8,
47
- step=1,
48
- value=2,
49
- label="Outer Scale",
50
- elem_id="outer-scale-slider"
51
- )
52
- warning_text = gr.HTML(elem_id="warning-text")
53
- gr.Markdown(
54
- "**Note:** For optimal output quality, set `Outer Scale` to a value between 1 and 4. "
55
- "**Values greater than 4 are not recommended**. "
56
- "Please ensure `Outer Scale` is greater than or equal to `Inner Scale` (default: 4)."
57
- )
58
- examples_data = load_examples()
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], # Corrected inputs
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("apps", "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
- if os.path.exists(config_path):
15
- with open(config_path, 'r') as f:
16
- config = json.load(f)
17
- input_filename = config.get("input_file", "input.jpg")
18
- output_filename = config.get("output_file", "output.jpg")
19
- outer_scale = config.get("outer_scale", 4)
20
-
21
- input_image_path = os.path.join(folder_path, input_filename)
22
- output_image_path = os.path.join(folder_path, output_filename)
23
-
24
- if os.path.exists(input_image_path) and os.path.exists(output_image_path):
25
- input_image_data = Image.open(input_image_path)
26
- output_image_data = Image.open(output_image_path)
27
- examples.append([input_image_data, output_image_data, outer_scale])
 
 
 
 
 
28
 
29
  return examples
30
 
31
  def select_example(evt: gr.SelectData, examples_data):
 
 
32
  example_index = evt.index
33
- input_image_data, output_image_data, outer_scale = examples_data[example_index]
34
- return input_image_data, outer_scale, output_image_data, f"Loaded example with Outer Scale: {outer_scale}"
 
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}"