pythonpLumberx
commited on
Commit
•
a998f86
1
Parent(s):
76f9508
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
from RealESRGAN import RealESRGAN
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
7 |
+
model_scales = {'2x': 2, '4x': 4, '8x': 8}
|
8 |
+
|
9 |
+
# Load RealESRGAN models for different scales
|
10 |
+
models = {scale: RealESRGAN(device, scale=scale) for scale in model_scales.values()}
|
11 |
+
|
12 |
+
def inference(images, scale):
|
13 |
+
results = []
|
14 |
+
|
15 |
+
if images is None or len(images) == 0:
|
16 |
+
raise gr.Error("No image uploaded. Please upload at least one image.")
|
17 |
+
|
18 |
+
for image in images:
|
19 |
+
width, height = image.size
|
20 |
+
if width >= 5000 or height >= 5000:
|
21 |
+
raise gr.Error("The image is too large.")
|
22 |
+
|
23 |
+
if torch.cuda.is_available():
|
24 |
+
torch.cuda.empty_cache()
|
25 |
+
|
26 |
+
# Select the appropriate model based on the chosen scale
|
27 |
+
model = models[model_scales[scale]]
|
28 |
+
result = model.predict(image.convert('RGB'))
|
29 |
+
print(f"Image size ({device}): {scale} ... OK")
|
30 |
+
results.append(result)
|
31 |
+
|
32 |
+
return results
|
33 |
+
|
34 |
+
title = "Advanced Real ESRGAN UpScale: 2x 4x 8x"
|
35 |
+
description = (
|
36 |
+
"This advanced demo for Real-ESRGAN allows you to upscale multiple images "
|
37 |
+
"with different models and resolutions. Choose the scale and upload images for high-resolution enhancement."
|
38 |
+
)
|
39 |
+
article = (
|
40 |
+
"<div style='text-align: center;'>Twitter "
|
41 |
+
"<a href='https://twitter.com/DoEvent' target='_blank'>Max Skobeev</a> | "
|
42 |
+
"<a href='https://huggingface.co/sberbank-ai/Real-ESRGAN' target='_blank'>Model card</a></div>"
|
43 |
+
)
|
44 |
+
|
45 |
+
gr.Interface(
|
46 |
+
inference,
|
47 |
+
[
|
48 |
+
gr.Image(type="pil", label="Upload Image", multiple=True),
|
49 |
+
gr.Radio(
|
50 |
+
list(model_scales.keys()),
|
51 |
+
type="value",
|
52 |
+
value='2x',
|
53 |
+
label='Resolution model',
|
54 |
+
),
|
55 |
+
],
|
56 |
+
gr.Image(type="pil", label="Output"),
|
57 |
+
title=title,
|
58 |
+
description=description,
|
59 |
+
article=article,
|
60 |
+
examples=[['groot.jpeg', '2x']],
|
61 |
+
allow_flagging='never',
|
62 |
+
cache_examples=False,
|
63 |
+
).launch()
|