Feruza8 commited on
Commit
4f14c30
·
verified ·
1 Parent(s): e5b7d77

Add app.py with basic Gradio interface for image upscaling

Browse files

Initial commit of AI Image Upscaler using Gradio. This app allows users to upload an image and receive a 2x 4x 6x 8x upscaled version. Built with PIL and Gradio.

Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import uuid
4
+ import tempfile
5
+ import zipfile
6
+ import numpy as np
7
+ from PIL import Image
8
+ import onnxruntime as ort
9
+
10
+ MODEL_PATHS = {
11
+ "Real-ESRGAN": "models/Real-ESRGAN_4x.onnx",
12
+ "Real-ESRGAN Anime6B": "models/Real-ESRGAN_Anime6B_4x.onnx"
13
+ }
14
+
15
+ def load_model(model_name):
16
+ return ort.InferenceSession(MODEL_PATHS[model_name], providers=["CPUExecutionProvider"])
17
+
18
+ def upscale_single_image(img, session):
19
+ image = Image.open(img).convert("RGB")
20
+ img_array = np.array(image).astype(np.float32) / 255.0
21
+ img_array = img_array.transpose(2, 0, 1)[None, :, :, :]
22
+
23
+ output = session.run(None, {"input": img_array})[0]
24
+ output = np.clip(output.squeeze().transpose(1, 2, 0), 0, 1)
25
+ upscaled = Image.fromarray((output * 255).astype(np.uint8))
26
+
27
+ original_name = os.path.basename(img.name)
28
+ name, ext = os.path.splitext(original_name)
29
+ output_name = f"{name}_upscaled{ext}"
30
+
31
+ temp_path = os.path.join(tempfile.gettempdir(), output_name)
32
+ upscaled.save(temp_path)
33
+ return output_name, temp_path
34
+
35
+ def upscale_batch(images, resolution, model_name):
36
+ session = load_model(model_name)
37
+ results = []
38
+ for img in images:
39
+ name, path = upscale_single_image(img, session)
40
+ results.append((name, path))
41
+ return results
42
+
43
+ def prepare_display(images_info):
44
+ return [(name, path) for name, path in images_info]
45
+
46
+ def generate_zip(images_info):
47
+ zip_name = f"upscaled_batch_{uuid.uuid4().hex[:8]}.zip"
48
+ zip_path = os.path.join(tempfile.gettempdir(), zip_name)
49
+ with zipfile.ZipFile(zip_path, "w") as zipf:
50
+ for name, path in images_info:
51
+ zipf.write(path, arcname=name)
52
+ return zip_path
53
+
54
+ with gr.Blocks(title="Ultra Pixel Image Upscaling") as app:
55
+ gr.Markdown("## Ultra Pixel Image Upscaling")
56
+ gr.Markdown("Perfect AI upscaling with zero distortion — bigger, better, sharper. Supports up to 16K resolution.")
57
+
58
+ with gr.Row():
59
+ file_input = gr.File(file_types=["image"], file_count="multiple", label="Upload Images")
60
+ resolution = gr.Dropdown(["2K", "4K", "6K", "8K", "16K"], value="4K", label="Choose Resolution")
61
+ model_choice = gr.Dropdown(["Real-ESRGAN", "Real-ESRGAN Anime6B"], value="Real-ESRGAN", label="Model")
62
+
63
+ upscale_btn = gr.Button("Start Upscaling")
64
+ output_gallery = gr.Gallery(label="Upscaled Images").style(grid=4)
65
+ zip_btn = gr.Button("Download All as ZIP")
66
+ zip_output = gr.File(label="ZIP File")
67
+
68
+ session_data = gr.State([])
69
+
70
+ def process_and_store(files, resolution, model_choice):
71
+ results = upscale_batch(files, resolution, model_choice)
72
+ return prepare_display(results), results
73
+
74
+ def return_zip(saved_state):
75
+ return generate_zip(saved_state)
76
+
77
+ upscale_btn.click(fn=process_and_store, inputs=[file_input, resolution, model_choice],
78
+ outputs=[output_gallery, session_data])
79
+ zip_btn.click(fn=return_zip, inputs=[session_data], outputs=zip_output)
80
+
81
+ app.launch()