mfumanelli commited on
Commit
bea4577
β€’
1 Parent(s): d3634e7

Add app.py

Browse files
Files changed (2) hide show
  1. app.py +189 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ from datasets import load_dataset
4
+ import transformers
5
+ from diffusers import StableDiffusionPipeline
6
+
7
+ import gradio as gr
8
+
9
+ from random import randrange
10
+ import os
11
+
12
+ MY_SECRET_TOKEN = os.environ.get('stable-diffusion')
13
+
14
+ data = load_dataset("mfumanelli/movies-small")
15
+ data = data['train'].to_pandas()
16
+
17
+ model_id = 'CompVis/stable-diffusion-v1-4'
18
+ device = torch.device('cpu' if not torch.cuda.is_available() else 'cuda')
19
+
20
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=MY_SECRET_TOKEN, revision='fp16')
21
+ pipe = pipe.to(device)
22
+
23
+
24
+ def infer(prompt, samples, steps, scale):
25
+ generator = torch.Generator(device=device)
26
+
27
+ if device.type == 'cuda':
28
+ with torch.autocast(device.type):
29
+ images_list = pipe(
30
+ [prompt] * samples,
31
+ num_inference_steps=steps,
32
+ guidance_scale=scale,
33
+ generator=generator,
34
+ )
35
+ else:
36
+ images_list = pipe(
37
+ [prompt] * samples,
38
+ num_inference_steps=steps,
39
+ guidance_scale=scale,
40
+ generator=generator,
41
+ )
42
+
43
+ return images_list
44
+
45
+
46
+ def generate_movie(state):
47
+ state["title"] = None
48
+ random_number = randrange(data.shape[0])
49
+ plot = data.iloc[random_number]["plot_synopsis_sum"]
50
+ image = infer(plot, 1, 50, 7.5)
51
+ state["number"] = random_number
52
+ state["title"] = data.iloc[int(random_number)]["title"]
53
+
54
+ return image["sample"][0], state
55
+
56
+
57
+ def movie_title(state):
58
+ return state["title"]
59
+
60
+
61
+ css = """
62
+ .gradio-container {
63
+ font-family: 'IBM Plex Sans', sans-serif;
64
+ }
65
+ .gr-button {
66
+ color: white;
67
+ border-color: black;
68
+ background: black;
69
+ }
70
+ input[type='range'] {
71
+ accent-color: black;
72
+ }
73
+ .dark input[type='range'] {
74
+ accent-color: #dfdfdf;
75
+ }
76
+ .container {
77
+ max-width: 730px;
78
+ margin: auto;
79
+ padding-top: 1.5rem;
80
+ }
81
+ #iamge {
82
+ min-height: 22rem;
83
+ margin-bottom: 15px;
84
+ margin-left: auto;
85
+ margin-right: auto;
86
+ border-bottom-right-radius: .5rem !important;
87
+ border-bottom-left-radius: .5rem !important;
88
+ }
89
+ #iamge>div>.h-full {
90
+ min-height: 20rem;
91
+ }
92
+ .details:hover {
93
+ text-decoration: underline;
94
+ }
95
+ .gr-button {
96
+ white-space: nowrap;
97
+ }
98
+ .gr-button:focus {
99
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
100
+ outline: none;
101
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
102
+ --tw-border-opacity: 1;
103
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
104
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
105
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
106
+ --tw-ring-opacity: .5;
107
+ }
108
+ .footer {
109
+ margin-bottom: 45px;
110
+ margin-top: 35px;
111
+ text-align: center;
112
+ border-bottom: 1px solid #e5e5e5;
113
+ }
114
+ .footer>p {
115
+ font-size: .8rem;
116
+ display: inline-block;
117
+ padding: 0 10px;
118
+ transform: translateY(10px);
119
+ background: white;
120
+ }
121
+ .dark .footer {
122
+ border-color: #303030;
123
+ }
124
+ .dark .footer>p {
125
+ background: #0b0f19;
126
+ }
127
+ .acknowledgments h4{
128
+ margin: 1.25em 0 .25em 0;
129
+ font-weight: bold;
130
+ font-size: 115%;
131
+ }
132
+ """
133
+
134
+ with gr.Blocks(css=css) as demo:
135
+ gr.HTML(
136
+ """
137
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
138
+ <div
139
+ style="
140
+ display: inline-flex;
141
+ align-items: center;
142
+ gap: 0.8rem;
143
+ font-size: 1.75rem;
144
+ "
145
+ >
146
+
147
+ <svg style="color: red" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256", height="0.85em" width="0.85em">
148
+ <rect width="18em" height="18em" fill="none"></rect>
149
+ <path d="M128,216S28,160,28,92A52,52,0,0,1,128,72h0A52,52,0,0,1,228,92C228,160,128,216,128,216Z" fill="#d63e25" stroke="#d63e25" stroke-linecap="round"
150
+ stroke-linejoin="round" stroke-width="12"></path></svg>
151
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
152
+ Stable Diffusion Loves Cinema
153
+ </h1>
154
+ </div>
155
+ <p style="margin-bottom: 20px; font-size: 94%">
156
+ Stable Diffusion is a state-of-the-art text-to-image model that generates images from text,
157
+ in this demo it is used to generate movie scenes from their storyline. <br></p>
158
+ <hr style="height:2px;border-width:0;color:gray;background-color:gray">
159
+ <br>
160
+ <p align="left" style="margin-bottom: 10px; font-size: 94%">
161
+ <b>Instructions</b>: press the "Generate a movie scene!" button to generate an image and try to see if you can guess the movie.
162
+ You can see if you guessed right by pressing the "Tell me the title" button.
163
+
164
+ </p>
165
+ </div>
166
+ """
167
+ )
168
+ with gr.Group():
169
+ with gr.Box():
170
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
171
+ b1 = gr.Button("Generate a movie scene!").style(
172
+ margin=False,
173
+ rounded=(False, True, True, False),
174
+ )
175
+ b2 = gr.Button("Tell me the title").style(
176
+ margin=False,
177
+ rounded=(False, True, True, False),
178
+ )
179
+ text = gr.Textbox(label="Title:")
180
+ image = gr.Image(
181
+ label="Generated images", show_label=False, elem_id="image"
182
+ ).style(height="auto")
183
+
184
+ state = gr.State({"number": None, "title": None})
185
+
186
+ b1.click(generate_movie, inputs=state, outputs=[image, state])
187
+ b2.click(movie_title, inputs=state, outputs=text)
188
+
189
+ demo.launch()
requirements.txt ADDED
File without changes