Profakerr commited on
Commit
0ba1507
·
verified ·
1 Parent(s): 661c2db

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +78 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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
+
22
+
23
+ if torch.cuda.is_available():
24
+ torch.cuda.empty_cache()
25
+
26
+ if size == '2x':
27
+ try:
28
+ result = model2.predict(image.convert('RGB'))
29
+ except torch.cuda.OutOfMemoryError as e:
30
+ print(e)
31
+ model2 = RealESRGAN(device, scale=2)
32
+ model2.load_weights('weights/RealESRGAN_x2.pth', download=False)
33
+ result = model2.predict(image.convert('RGB'))
34
+ elif size == '4x':
35
+ try:
36
+ result = model4.predict(image.convert('RGB'))
37
+ except torch.cuda.OutOfMemoryError as e:
38
+ print(e)
39
+ model4 = RealESRGAN(device, scale=4)
40
+ model4.load_weights('weights/RealESRGAN_x4.pth', download=False)
41
+ result = model2.predict(image.convert('RGB'))
42
+ else:
43
+ try:
44
+ width, height = image.size
45
+ if width >= 5000 or height >= 5000:
46
+ raise gr.Error("The image is too large.")
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 = "Face Real ESRGAN UpScale: 2x 4x 8x"
59
+ 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"
60
+ 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>"
61
+
62
+
63
+ gr.Interface(inference,
64
+ [gr.Image(type="pil"),
65
+ gr.Radio(["2x", "4x", "8x"],
66
+ type="value",
67
+ value="2x",
68
+ label="Resolution model")],
69
+ gr.Image(type="pil", label="Output"),
70
+ title=title,
71
+ description=description,
72
+ article=article,
73
+ examples=[["groot.jpeg", "2x"]],
74
+ flagging_mode="never",
75
+ cache_mode="lazy",
76
+ delete_cache=(44000, 44000),
77
+ ).queue(api_open=True).launch(show_error=True, show_api=True)
78
+
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ git+https://github.com/doevent/Real-ESRGAN.git