Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from RealESRGAN import RealESRGAN
|
| 6 |
+
|
| 7 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 8 |
+
model2 = RealESRGAN(device, scale=2)
|
| 9 |
+
model2.load_weights('weights/RealESRGAN_x2.pth', download=True)
|
| 10 |
+
model4 = RealESRGAN(device, scale=4)
|
| 11 |
+
model4.load_weights('weights/RealESRGAN_x4.pth', download=True)
|
| 12 |
+
model8 = RealESRGAN(device, scale=8)
|
| 13 |
+
model8.load_weights('weights/RealESRGAN_x8.pth', download=True)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@spaces.GPU(duration=17)
|
| 17 |
+
def inference(image, size):
|
| 18 |
+
global model2
|
| 19 |
+
global model4
|
| 20 |
+
global model8
|
| 21 |
+
if image is None:
|
| 22 |
+
raise gr.Error("Image not uploaded")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
if torch.cuda.is_available():
|
| 26 |
+
torch.cuda.empty_cache()
|
| 27 |
+
|
| 28 |
+
if size == '2x':
|
| 29 |
+
try:
|
| 30 |
+
result = model2.predict(image.convert('RGB'))
|
| 31 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 32 |
+
print(e)
|
| 33 |
+
model2 = RealESRGAN(device, scale=2)
|
| 34 |
+
model2.load_weights('weights/RealESRGAN_x2.pth', download=False)
|
| 35 |
+
result = model2.predict(image.convert('RGB'))
|
| 36 |
+
elif size == '4x':
|
| 37 |
+
try:
|
| 38 |
+
result = model4.predict(image.convert('RGB'))
|
| 39 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 40 |
+
print(e)
|
| 41 |
+
model4 = RealESRGAN(device, scale=4)
|
| 42 |
+
model4.load_weights('weights/RealESRGAN_x4.pth', download=False)
|
| 43 |
+
result = model2.predict(image.convert('RGB'))
|
| 44 |
+
else:
|
| 45 |
+
try:
|
| 46 |
+
result = model8.predict(image.convert('RGB'))
|
| 47 |
+
except torch.cuda.OutOfMemoryError as e:
|
| 48 |
+
print(e)
|
| 49 |
+
model8 = RealESRGAN(device, scale=8)
|
| 50 |
+
model8.load_weights('weights/RealESRGAN_x8.pth', download=False)
|
| 51 |
+
result = model2.predict(image.convert('RGB'))
|
| 52 |
+
|
| 53 |
+
print(f"Image size ({device}): {size} ... OK")
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
title = "Face Real ESRGAN UpScale: 2x 4x 8x"
|
| 58 |
+
description = "This is an unofficial demo for Real-ESRGAN. Scales the resolution of a photo. This model shows better results on faces compared to the original version.<br>Telegram BOT: https://t.me/restoration_photo_bot"
|
| 59 |
+
article = "<div style='text-align: center;'>Twitter <a href='https://twitter.com/DoEvent' target='_blank'>Max Skobeev</a> | <a href='https://huggingface.co/sberbank-ai/Real-ESRGAN' target='_blank'>Model card</a><div>"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
gr.Interface(inference,
|
| 63 |
+
[gr.Image(type="pil"),
|
| 64 |
+
gr.Radio(["2x", "4x", "8x"],
|
| 65 |
+
type="value",
|
| 66 |
+
value="2x",
|
| 67 |
+
label="Resolution model")],
|
| 68 |
+
gr.Image(type="pil", label="Output"),
|
| 69 |
+
title=title,
|
| 70 |
+
description=description,
|
| 71 |
+
article=article,
|
| 72 |
+
examples=[["groot.jpeg", "2x"]],
|
| 73 |
+
flagging_mode="never",
|
| 74 |
+
cache_mode="lazy",
|
| 75 |
+
delete_cache=(44000, 44000),
|
| 76 |
+
).queue(api_open=True).launch(show_error=True, show_api=True, mcp_server=True)
|