darcksky commited on
Commit
4f0b1cf
1 Parent(s): 044dc7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py CHANGED
@@ -3,3 +3,46 @@ from PIL import Image
3
  from RealESRGAN import RealESRGAN
4
  import gradio as gr
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: Image, size: str) -> Image:
16
+ try:
17
+ if size == '2x':
18
+ result = model2.predict(image.convert('RGB'))
19
+ elif size == '4x':
20
+ result = model4.predict(image.convert('RGB'))
21
+ else:
22
+ result = model8.predict(image.convert('RGB'))
23
+ except Exception as e:
24
+ print(e)
25
+ raise gr.Error(str(e))
26
+ return result
27
+
28
+
29
+ title = "Face Real ESRGAN: 2x 4x 8x"
30
+ 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"
31
+ 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> <center><img src='https://visitor-badge.glitch.me/badge?page_id=max_skobeev_face_esrgan' alt='visitor badge'></center></div>"
32
+
33
+
34
+ gr.Interface(inference,
35
+ [gr.Image(type="pil"),
36
+ gr.Radio(['2x', '4x', '8x'],
37
+ type="value",
38
+ value='2x',
39
+ label='Resolution model')],
40
+ gr.Image(type="pil", label="Output"),
41
+ title=title,
42
+ description=description,
43
+ article=article,
44
+ examples=[['groot.jpeg', "2x"]],
45
+ allow_flagging='never',
46
+ theme="default",
47
+ cache_examples=False,
48
+ ).queue().launch(show_error=True)