andresgtn commited on
Commit
98ac5fa
·
1 Parent(s): 77cec1f

Create new file

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import StableDiffusionPipeline
2
+ import gradio as gr
3
+ import requests
4
+ import base64
5
+ from PIL import Image, PngImagePlugin
6
+ from io import BytesIO
7
+
8
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
9
+
10
+ def encode_pil_to_base64(pil_image):
11
+ ''' From: https://github.com/gradio-app/gradio/blob/main/gradio/processing_utils.py'''
12
+ with BytesIO() as output_bytes:
13
+
14
+ # Copy any text-only metadata
15
+ use_metadata = False
16
+ metadata = PngImagePlugin.PngInfo()
17
+ for key, value in pil_image.info.items():
18
+ if isinstance(key, str) and isinstance(value, str):
19
+ metadata.add_text(key, value)
20
+ use_metadata = True
21
+
22
+ pil_image.save(
23
+ output_bytes, "PNG", pnginfo=(metadata if use_metadata else None)
24
+ )
25
+ bytes_data = output_bytes.getvalue()
26
+ base64_str = str(base64.b64encode(bytes_data), "utf-8")
27
+ return "data:image/png;base64," + base64_str
28
+
29
+ def decode_base64_to_image(encoding):
30
+ ''' From: https://github.com/gradio-app/gradio/blob/main/gradio/processing_utils.py'''
31
+ content = encoding.split(";")[1]
32
+ image_encoded = content.split(",")[1]
33
+ return Image.open(BytesIO(base64.b64decode(image_encoded)))
34
+
35
+ def improve_image(img, scale=2):
36
+ ''' Improves an input image using GFP-GAN
37
+ Inputs
38
+ img (PIL): image to improve
39
+ scale (int): scale factor for new image
40
+ Output
41
+ Improved image. If the request to GFPGAN is unsuccesful, it returns
42
+ a black image.
43
+ '''
44
+ url = "https://hf.space/embed/NotFungibleIO/GFPGAN/+/api/predict"
45
+ request_objt = {"data":[encode_pil_to_base64(img),'v1.3', scale]}
46
+ try:
47
+ imp_img = decode_base64_to_image(requests.post(url, json=request_objt).json()['data'][0])
48
+ except AttributeError:
49
+ return Image.new('RGB', size=(512, 512))
50
+ return imp_img
51
+
52
+ def generate(celebrity, movie, guidance, improve_flag, scale): # add scale as var
53
+ prompt = f"A movie poster of {celebrity} in {movie}."
54
+ image = pipe(prompt, guidance=guidance).images[0]
55
+ if improve_flag:
56
+ image = improve_image(image, scale=scale)
57
+ return image
58
+
59
+ movie_options = ["James Bond", "Snatch", "Saving Private Ryan", "Scarface", "Avatar", "Top Gun"]
60
+ title = "Movie Poster Celebrity Swap"
61
+ description = "Write the name of a celebrity, and pick a movie from the dropdown menu.\
62
+ It will generate a new movie poster (inspired by your chosen movie)\
63
+ with the chosen celebrity in it. See below for explanation of the\
64
+ input variables."
65
+ article= "Inputs explained: \n Guidance: the lower, the more random the output\
66
+ image. Improve and scale: if selected, the image will be refined\
67
+ using GFP-GAN (google it), and scaled (if scale is >1)."
68
+
69
+ demo = gr.Interface(
70
+ fn=generate,
71
+ inputs=[gr.Textbox(value="Daniel Craig"),
72
+ gr.Dropdown(movie_options, value="Saving Private Ryan"),
73
+ gr.Slider(1, 20, value=7.5, step=0.5),
74
+ gr.Checkbox(label="Improve and scale? (extra time)"),
75
+ gr.Slider(1, 3, value=1, step=0.5)
76
+ ],
77
+ outputs='image',
78
+ title=title,
79
+ description=description,
80
+ article=article
81
+ )
82
+
83
+ demo.launch()