Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,18 +4,13 @@ from RealESRGAN import RealESRGAN
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 7 |
-
model2 = RealESRGAN(device, scale=2)
|
| 8 |
-
model2.load_weights('weights/RealESRGAN_x2.pth', download=True)
|
| 9 |
-
model4 = RealESRGAN(device, scale=4)
|
| 10 |
-
model4.load_weights('weights/RealESRGAN_x4.pth', download=True)
|
| 11 |
-
model8 = RealESRGAN(device, scale=8)
|
| 12 |
-
model8.load_weights('weights/RealESRGAN_x8.pth', download=True)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def inference(image, size):
|
| 16 |
-
global model2
|
| 17 |
-
global model4
|
| 18 |
-
global model8
|
| 19 |
if image is None:
|
| 20 |
raise gr.Error("Image not uploaded")
|
| 21 |
|
|
@@ -26,48 +21,28 @@ def inference(image, size):
|
|
| 26 |
if torch.cuda.is_available():
|
| 27 |
torch.cuda.empty_cache()
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
try:
|
| 39 |
-
result = model4.predict(image.convert('RGB'))
|
| 40 |
-
except torch.cuda.OutOfMemoryError as e:
|
| 41 |
-
print(e)
|
| 42 |
-
model4 = RealESRGAN(device, scale=4)
|
| 43 |
-
model4.load_weights('weights/RealESRGAN_x4.pth', download=False)
|
| 44 |
-
result = model2.predict(image.convert('RGB'))
|
| 45 |
-
else:
|
| 46 |
-
try:
|
| 47 |
-
result = model8.predict(image.convert('RGB'))
|
| 48 |
-
except torch.cuda.OutOfMemoryError as e:
|
| 49 |
-
print(e)
|
| 50 |
-
model8 = RealESRGAN(device, scale=8)
|
| 51 |
-
model8.load_weights('weights/RealESRGAN_x8.pth', download=False)
|
| 52 |
-
result = model2.predict(image.convert('RGB'))
|
| 53 |
|
| 54 |
print(f"Image size ({device}): {size} ... OK")
|
| 55 |
return result
|
| 56 |
|
| 57 |
-
|
| 58 |
title = "RealESRGAN UpScale Model: 2x 4x 8x"
|
| 59 |
-
description = "This model running on
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
description=description,
|
| 71 |
-
allow_flagging='never',
|
| 72 |
-
cache_examples=False,
|
| 73 |
-
).queue(api_open=False).launch(show_error=True, show_api=False)
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def load_model(scale):
|
| 9 |
+
model = RealESRGAN(device, scale=scale)
|
| 10 |
+
model.load_weights(f'weights/RealESRGAN_x{scale}.pth', download=True)
|
| 11 |
+
return model
|
| 12 |
|
| 13 |
def inference(image, size):
|
|
|
|
|
|
|
|
|
|
| 14 |
if image is None:
|
| 15 |
raise gr.Error("Image not uploaded")
|
| 16 |
|
|
|
|
| 21 |
if torch.cuda.is_available():
|
| 22 |
torch.cuda.empty_cache()
|
| 23 |
|
| 24 |
+
scale = int(size[0])
|
| 25 |
+
model = load_model(scale)
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
result = model.predict(image.convert('RGB'))
|
| 29 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 30 |
+
print(e)
|
| 31 |
+
model = load_model(scale)
|
| 32 |
+
result = model.predict(image.convert('RGB'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
print(f"Image size ({device}): {size} ... OK")
|
| 35 |
return result
|
| 36 |
|
|
|
|
| 37 |
title = "RealESRGAN UpScale Model: 2x 4x 8x"
|
| 38 |
+
description = "This model running on CPU so it takes a bit of time, please be patient :)"
|
| 39 |
+
|
| 40 |
+
gr.Interface(
|
| 41 |
+
inference,
|
| 42 |
+
[gr.Image(type="pil"), gr.Radio(['2x', '4x', '8x'], type="value", value='2x', label='Resolution model')],
|
| 43 |
+
gr.Image(type="pil", label="Output"),
|
| 44 |
+
title=title,
|
| 45 |
+
description=description,
|
| 46 |
+
allow_flagging='never',
|
| 47 |
+
cache_examples=False,
|
| 48 |
+
).queue(api_open=False).launch(show_error=True, show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|