justina-tran
commited on
Commit
·
bf5a03f
1
Parent(s):
8efdec0
add files from colab
Browse files- app.py +52 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import StableDiffusionPipeline
|
2 |
+
import torch
|
3 |
+
import requests
|
4 |
+
import base64
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
from gradio.components import Textbox
|
8 |
+
|
9 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
10 |
+
pipe = pipe.to("cuda")
|
11 |
+
|
12 |
+
device = "cuda"
|
13 |
+
generator = torch.Generator(device=device)
|
14 |
+
seed = generator.seed()
|
15 |
+
|
16 |
+
def improve_image(img):
|
17 |
+
url = "https://hf.space/embed/abidlabs/GFPGAN/+/api/predict"
|
18 |
+
img_to_str = gr.processing_utils.encode_pil_to_base64(img)
|
19 |
+
inputs = {'data': [img_to_str, 2]}
|
20 |
+
resp = requests.post(url, json = inputs)
|
21 |
+
resp_img = gr.processing_utils.decode_base64_to_image(resp.json()['data'][0])
|
22 |
+
return resp_img
|
23 |
+
|
24 |
+
|
25 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
26 |
+
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "movie-img-generator")
|
27 |
+
|
28 |
+
def generate(celebrity, setting):
|
29 |
+
prompt = "A movie poster with title and caption of {} in {}".format(celebrity, setting)
|
30 |
+
image = pipe(prompt,
|
31 |
+
latents=torch.randn((1, 4, 64, 64), generator=torch.manual_seed(12)),
|
32 |
+
guidance_scale=7).images[0]
|
33 |
+
|
34 |
+
enhanced_img = improve_image(image)
|
35 |
+
return enhanced_img
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
title="🎬 Movie Poster Image Generator",
|
39 |
+
description="Generate an image of a celebritiy in a cinematic setting by entering in any celebrity name and selecting a TV show/movie to see what image is generated!🍿",
|
40 |
+
inputs=[gr.Textbox(lines=1, label="Enter a celebrity name:"),
|
41 |
+
gr.Dropdown(["Stranger Things",
|
42 |
+
"Mean Girls",
|
43 |
+
"Titanic",
|
44 |
+
"The Big Bang Theory",
|
45 |
+
"Game of Thrones"], label="Select a setting:"),],
|
46 |
+
outputs="image",
|
47 |
+
fn=generate,
|
48 |
+
allow_flagging="manual",
|
49 |
+
flagging_options=["Wrong Celebrity", "Wrong Setting", "Image Not Clear"],
|
50 |
+
flagging_callback=hf_writer
|
51 |
+
)
|
52 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
gradio
|
3 |
+
diffusers
|