po5302006 commited on
Commit
201e368
Β·
verified Β·
1 Parent(s): 6751a64

add upscaler with x4

Browse files
Files changed (2) hide show
  1. app.py +60 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from diffusers import StableDiffusionUpscalePipeline
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ import torch
7
+
8
+
9
+
10
+ model_id = "stabilityai/stable-diffusion-x4-upscaler"
11
+ pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
12
+ pipeline = pipeline.to("cuda")
13
+
14
+
15
+ # def predict(input_img):
16
+ # predictions = predictor(input_img)
17
+ # return input_img, {p["label"]: p["score"] for p in predictions}
18
+
19
+
20
+ def upscalex4(input_img):
21
+ low_res_img = resize_on_scale(input_img)
22
+ prompt = "highly detailed"
23
+ upscaled_img = pipeline(prompt=prompt, image=low_res_img).images[0]
24
+ print("Low resolution image size = ", low_res_img.size)
25
+ print("Upscaled image size = ", upscaled_img.size)
26
+ return upscaled_img
27
+
28
+
29
+ def resize_on_scale(input_img):
30
+ low_res_img = input_img.convert("RGB")
31
+ wsize = 300
32
+ wpercent = (wsize / float(input_img.size[0]))
33
+ hsize = int((float(input_img.size[1]) * float(wpercent)))
34
+ low_res_img = low_res_img.resize((wsize, hsize))
35
+ return low_res_img
36
+
37
+
38
+ gradio_app = gr.Interface(
39
+ upscalex4,
40
+ inputs=gr.Image(label="Select a blurry image", sources=['upload', 'webcam', 'clipboard'], type="pil"),
41
+ outputs=gr.Image(label="Processed Image"),
42
+ title="Image Upscaler",
43
+ )
44
+
45
+ if __name__ == "__main__":
46
+ gradio_app.launch()
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+ # img = Image.open(requests.get("https://pbs.twimg.com/profile_images/1600764211378139137/HirERJI5_400x400.jpg", stream=True).raw)
56
+ # img = img.resize((64, 64))
57
+
58
+ # print("Original image size = ", img.size)
59
+ # print("Upscaled image size = ", upscaled_img.size)
60
+ # upscaled_img.show()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ diffusers==0.26.3
2
+ gradio
3
+ Pillow==10.2.0
4
+ torch