logan-codes commited on
Commit
c72957e
ยท
0 Parent(s):

Add AI image upscaler Gradio app

Browse files

Add a Gradio-based AI image upscaler application (main.py) using Real-ESRGAN with an RRDBNet model and a cached upsampler loader to avoid reloading models on each request. Add project metadata and dependencies in pyproject.toml, a minimal README frontmatter, .gitignore for Python artifacts, and .python-version (3.11). (Note: a lockfile was also added but is not described here.)

Files changed (6) hide show
  1. .gitignore +10 -0
  2. .python-version +1 -0
  3. README.md +10 -0
  4. main.py +49 -0
  5. pyproject.toml +15 -0
  6. uv.lock +0 -0
.gitignore ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.11
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: AI Image Upscaler
3
+ emoji: ๐Ÿ”
4
+ colorFrom: purple
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: "4.0"
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
main.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ from realesrgan import RealESRGANer
5
+ from basicsr.archs.rrdbnet_arch import RRDBNet
6
+
7
+ # Load model once at startup (not on every call)
8
+ def load_model(scale=4):
9
+ model = RRDBNet(
10
+ num_in_ch=3, num_out_ch=3,
11
+ num_feat=64, num_block=23, num_grow_ch=32, scale=scale
12
+ )
13
+ upsampler = RealESRGANer(
14
+ scale=scale,
15
+ model_path=f"https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x{scale}plus.pth",
16
+ model=model,
17
+ half=False # set True if GPU available
18
+ )
19
+ return upsampler
20
+
21
+ upsampler_cache = {}
22
+
23
+ def upscale(image: Image.Image, scale: int):
24
+ if scale not in upsampler_cache:
25
+ upsampler_cache[scale] = load_model(scale)
26
+
27
+ upsampler = upsampler_cache[scale]
28
+ img_array = np.array(image)
29
+ output, _ = upsampler.enhance(img_array, outscale=scale)
30
+ return Image.fromarray(output)
31
+
32
+ # Gradio UI
33
+ with gr.Blocks(title="AI Image Upscaler") as demo:
34
+ gr.Markdown("## ๐Ÿ” AI Image Upscaler\nPowered by Real-ESRGAN")
35
+
36
+ with gr.Row():
37
+ with gr.Column():
38
+ input_img = gr.Image(type="pil", label="Input Image")
39
+ scale_choice = gr.Radio(
40
+ choices=[2, 4, 8], value=4, label="Upscale Factor"
41
+ )
42
+ btn = gr.Button("Upscale", variant="primary")
43
+
44
+ with gr.Column():
45
+ output_img = gr.Image(type="pil", label="Upscaled Output")
46
+
47
+ btn.click(fn=upscale, inputs=[input_img, scale_choice], outputs=output_img)
48
+
49
+ demo.launch()
pyproject.toml ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "image-upscaler"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ dependencies = [
8
+ "basicsr>=1.4.2",
9
+ "gradio>=6.11.0",
10
+ "numpy<2",
11
+ "pillow>=12.2.0",
12
+ "realesrgan>=0.3.0",
13
+ "torch==2.1.2",
14
+ "torchvision==0.16.2",
15
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff