File size: 1,679 Bytes
d812b13
 
 
 
 
 
 
a022650
d812b13
 
 
699dbc9
d109fe0
d812b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699dbc9
d812b13
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from diffusers import StableDiffusionPipeline
import requests
import os
import gradio as gr
import torch

SEED = 42
AUTH_TOKEN = os.environ.get("auth_token")
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=AUTH_TOKEN)
pipe = pipe.to(DEVICE)
hf_writer = gr.HuggingFaceDatasetSaver(AUTH_TOKEN, "celebrity-set-dataset")

# Ensure consistently generated images
generator = torch.Generator(device=DEVICE).manual_seed(SEED)
latent = torch.randn(
    (1, 4, 64, 64),
    generator = generator,
    device = DEVICE
)

def generate(celebrity, setting):
  prompt = "A movie poster with {} in {}.".format(celebrity, setting)
  return improve_image(pipe(prompt, latents=latent).images[0], 2)

# Use the GANS model of Abubakar
def improve_image(img, rescaling_factor = 1):
  return gr.processing_utils.decode_base64_to_image(
      requests.post(
          url = 'https://hf.space/embed/abidlabs/GFPGAN/+/api/predict', 
          json = { 
              "data": [
                  gr.processing_utils.encode_pil_to_base64(img), 
                  rescaling_factor
                  ]}
                  ).json()['data'][0])
                  
gr.Interface(
    inputs = [
        gr.Textbox(label = 'Celebrity'), 
        gr.Dropdown(
            choices = ['Star Trek', 'Star Wars', 'The Wire', 'Breaking Bad', 'a rainforest', 'a skyscraper.'], 
            label = 'Movie / Show / Setting')
        ],
    fn = generate,
    outputs = "image",
    allow_flagging = "manual",
    flagging_options = ["Looks good", "Looks bad"],
    flagging_callback = hf_writer
).launch()