Delete apps/old-gradio_app.py
Browse files- apps/old-gradio_app.py +0 -136
apps/old-gradio_app.py
DELETED
@@ -1,136 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import sys
|
4 |
-
import json
|
5 |
-
from PIL import Image
|
6 |
-
|
7 |
-
from gradio_app.project_info import (
|
8 |
-
CONTENT_DESCRIPTION,
|
9 |
-
CONTENT_IN_1, CONTENT_IN_2,
|
10 |
-
CONTENT_OUT_1, CONTENT_OUT_2
|
11 |
-
)
|
12 |
-
# Add the project root directory to the Python path
|
13 |
-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
14 |
-
|
15 |
-
from src.anime_super_resolution.infer import infer
|
16 |
-
|
17 |
-
def run_inference(input_image, model_id, outer_scale):
|
18 |
-
if not input_image:
|
19 |
-
return None, "Please upload an image."
|
20 |
-
|
21 |
-
models_config_path = "configs/model_ckpts.yaml"
|
22 |
-
try:
|
23 |
-
output_image = infer(
|
24 |
-
input_path=input_image,
|
25 |
-
model_id=model_id,
|
26 |
-
models_config_path=models_config_path,
|
27 |
-
outer_scale=outer_scale,
|
28 |
-
)
|
29 |
-
return output_image, "Inference completed successfully!"
|
30 |
-
except Exception as e:
|
31 |
-
return None, f"Error during inference: {str(e)}"
|
32 |
-
|
33 |
-
def load_examples():
|
34 |
-
examples = []
|
35 |
-
examples_base_path = os.path.join("apps", "assets", "examples", "Real-ESRGAN-Anime-finetuning")
|
36 |
-
|
37 |
-
for folder in ["1", "2", "3", "4"]:
|
38 |
-
folder_path = os.path.join(examples_base_path, folder)
|
39 |
-
config_path = os.path.join(folder_path, "config.json")
|
40 |
-
|
41 |
-
if os.path.exists(config_path):
|
42 |
-
with open(config_path, 'r') as f:
|
43 |
-
config = json.load(f)
|
44 |
-
input_filename = config.get("input_file", "input.jpg")
|
45 |
-
output_filename = config.get("output_file", "output.jpg")
|
46 |
-
outer_scale = config.get("outer_scale", 4)
|
47 |
-
|
48 |
-
input_image_path = os.path.join(folder_path, input_filename)
|
49 |
-
output_image_path = os.path.join(folder_path, output_filename)
|
50 |
-
|
51 |
-
if os.path.exists(input_image_path) and os.path.exists(output_image_path):
|
52 |
-
input_image_data = Image.open(input_image_path)
|
53 |
-
output_image_data = Image.open(output_image_path)
|
54 |
-
examples.append([input_image_data, output_image_data, outer_scale])
|
55 |
-
|
56 |
-
return examples
|
57 |
-
|
58 |
-
def select_example(evt: gr.SelectData, examples_data):
|
59 |
-
example_index = evt.index
|
60 |
-
input_image_data, output_image_data, outer_scale = examples_data[example_index]
|
61 |
-
return input_image_data, outer_scale, output_image_data, f"Loaded example with Outer Scale: {outer_scale}"
|
62 |
-
|
63 |
-
# Load custom CSS
|
64 |
-
custom_css = open("apps/gradio_app/static/styles.css").read()
|
65 |
-
|
66 |
-
# JavaScript function to update warning_text Markdown component
|
67 |
-
outer_scale_warning = open("apps/gradio_app/static/outer_scale_warning.js").read()
|
68 |
-
|
69 |
-
# Define Gradio interface
|
70 |
-
with gr.Blocks(css=custom_css) as demo:
|
71 |
-
gr.Markdown("# Anime Super Resolution 🖼️")
|
72 |
-
gr.Markdown(CONTENT_DESCRIPTION)
|
73 |
-
gr.Markdown(CONTENT_IN_1)
|
74 |
-
gr.HTML(CONTENT_IN_2)
|
75 |
-
with gr.Row():
|
76 |
-
with gr.Column(scale=2):
|
77 |
-
input_image = gr.Image(
|
78 |
-
type="filepath",
|
79 |
-
label="Input Image",
|
80 |
-
elem_classes="input-image"
|
81 |
-
)
|
82 |
-
model_id = gr.Textbox(
|
83 |
-
label="Model ID",
|
84 |
-
value="danhtran2mind/Real-ESRGAN-Anime-finetuning"
|
85 |
-
)
|
86 |
-
|
87 |
-
outer_scale = gr.Slider(
|
88 |
-
minimum=1,
|
89 |
-
maximum=8,
|
90 |
-
step=1,
|
91 |
-
value=2,
|
92 |
-
label="Outer Scale",
|
93 |
-
elem_id="outer-scale-slider"
|
94 |
-
)
|
95 |
-
warning_text = gr.HTML(elem_id="warning-text") # Added elem_id for JS targeting
|
96 |
-
gr.Markdown(
|
97 |
-
"**Note:** For optimal output quality, set `Outer Scale` to a value between 1 and 4. "
|
98 |
-
"**Values greater than 4 are not recommended**. "
|
99 |
-
"Please ensure `Outer Scale` is greater than or equal to `Inner Scale` (default: 4)."
|
100 |
-
)
|
101 |
-
|
102 |
-
examples_data = load_examples()
|
103 |
-
submit_button = gr.Button("Run Inference")
|
104 |
-
|
105 |
-
with gr.Column(scale=3):
|
106 |
-
output_image = gr.Image(
|
107 |
-
label="Output Image",
|
108 |
-
elem_classes="output-image"
|
109 |
-
)
|
110 |
-
output_text = gr.Textbox(label="Status")
|
111 |
-
|
112 |
-
# Client-side warning update for warning_text
|
113 |
-
outer_scale.change(
|
114 |
-
fn=lambda x: x, # Pass-through function to satisfy Gradio
|
115 |
-
inputs=outer_scale,
|
116 |
-
outputs=outer_scale, # Return value to maintain slider functionality
|
117 |
-
js=outer_scale_warning # Update warning_text via JavaScript
|
118 |
-
)
|
119 |
-
|
120 |
-
gr.Examples(
|
121 |
-
examples=[[input_img, output_img, outer_scale] for input_img, output_img, outer_scale in examples_data],
|
122 |
-
inputs=[input_image, output_image, outer_scale],
|
123 |
-
label="Example Inputs",
|
124 |
-
examples_per_page=4
|
125 |
-
)
|
126 |
-
|
127 |
-
submit_button.click(
|
128 |
-
fn=run_inference,
|
129 |
-
inputs=[input_image, model_id, outer_scale],
|
130 |
-
outputs=[output_image, output_text]
|
131 |
-
)
|
132 |
-
gr.HTML(CONTENT_OUT_1)
|
133 |
-
gr.Markdown(CONTENT_OUT_2)
|
134 |
-
|
135 |
-
if __name__ == "__main__":
|
136 |
-
demo.launch(share=True) # Changed to local launch for safety; use share=True for public URL if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|