hshetty commited on
Commit
f488959
·
1 Parent(s): ee83f35

Initial app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from diffusers import StableDiffusionPipeline
3
+ import requests
4
+ import base64
5
+ import torch
6
+ import os
7
+
8
+
9
+ device = "cuda"
10
+ generator = torch.Generator(device=device)
11
+
12
+ HF_TOKEN = os.getenv('HF_TOKEN')
13
+ hf_writer =gr.HuggingFaceDatasetSaver(HF_TOKEN, "dst-movie-poster-demo")
14
+
15
+
16
+ def improve_image(img):
17
+ # ANSWER HERE
18
+ img_in_base64 = gr.processing_utils.encode_pil_to_base64(img)
19
+ scale=3
20
+ resp_obj = requests.post('https://hf.space/embed/abidlabs/GFPGAN/+/api/predict',json={'data':[img_in_base64,scale]})
21
+ resp_img = gr.processing_utils.decode_base64_to_image((resp_obj.json())['data'][0])
22
+ return resp_img
23
+
24
+ pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
25
+ pipe = pipe.to("cuda")
26
+
27
+ def generate(celebrity, setting):
28
+ # ANSWER HERE
29
+ prompt = 'A movie poster of {} in the movie{}'.format(celebrity,setting)
30
+ latent_sample = torch.randn((1,4,64,64),generator = generator,device=device)
31
+ gen_img = pipe(prompt,latents=latent_sample,num_inference_steps=100,guidance_scale=g_scale[i]).images[0]
32
+ image = improve_image(gen_img)
33
+ return image
34
+
35
+ gr.Interface(
36
+ # ANSWER HERE
37
+ fn=generate,
38
+ inputs=[gr.Textbox(label='Celebrity'), gr.Dropdown(['The Godfather', 'Titanic', 'Fast and Furious'], label='Movie')],
39
+ outputs = gr.Image(type='pill'),
40
+ allow_flagging="manual",
41
+ flagging_options = ['Incorrect movie poster','Incorrect Actor','Other Problem'],
42
+ flagging_callback=hf_writer,
43
+ flagging_dir='/flagged_data'
44
+ ).launch()