mrm8488 commited on
Commit
e5ba529
1 Parent(s): 46924fe

First commit

Browse files
Files changed (4) hide show
  1. app.py +134 -0
  2. examples.py +37 -0
  3. requirements.txt +6 -0
  4. styles.py +159 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import torch
4
+ from torch import autocast
5
+ from diffusers import StableDiffusionPipeline
6
+ from datasets import load_dataset
7
+ from PIL import Image
8
+ import re
9
+ from styles import css, header_html, footer_html
10
+ from examples import examples
11
+ from transformers import pipeline
12
+
13
+ ars_model = pipeline("automatic-speech-recognition")
14
+
15
+
16
+ model_id = "CompVis/stable-diffusion-v1-4"
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+
19
+ # If you are running this code locally, you need to either do a 'huggingface-cli login` or paste your User Access Token from here https://huggingface.co/settings/tokens into the use_auth_token field below.
20
+ pipe = StableDiffusionPipeline.from_pretrained(
21
+ model_id, use_auth_token=True, revision="fp16", torch_dtype=torch.float16)
22
+ pipe = pipe.to(device)
23
+ # When running locally, you won`t have access to this, so you can remove this part
24
+ word_list_dataset = load_dataset(
25
+ "stabilityai/word-list", data_files="list.txt", use_auth_token=True)
26
+ word_list = word_list_dataset["train"]['text']
27
+
28
+
29
+ def transcribe(audio):
30
+ text = ars_model(audio)["text"]
31
+ return text
32
+
33
+
34
+ def infer(audio, samples, steps, scale, seed):
35
+ prompt = transcribe(audio)
36
+ # When running locally you can also remove this filter
37
+ for filter in word_list:
38
+ if re.search(rf"\b{filter}\b", prompt):
39
+ raise gr.Error(
40
+ "Unsafe content found. Please try again with different prompts.")
41
+
42
+ generator = torch.Generator(device=device).manual_seed(seed)
43
+
44
+ # If you are running locally with CPU, you can remove the `with autocast("cuda")`
45
+ if device == "cuda":
46
+ with autocast("cuda"):
47
+ images_list = pipe(
48
+ [prompt] * samples,
49
+ num_inference_steps=steps,
50
+ guidance_scale=scale,
51
+ generator=generator,
52
+ )
53
+ else:
54
+ images_list = pipe(
55
+ [prompt] * samples,
56
+ num_inference_steps=steps,
57
+ guidance_scale=scale,
58
+ generator=generator,
59
+ )
60
+ images = []
61
+ safe_image = Image.open(r"unsafe.png")
62
+ for i, image in enumerate(images_list["sample"]):
63
+ if(images_list["nsfw_content_detected"][i]):
64
+ images.append(safe_image)
65
+ else:
66
+ images.append(image)
67
+ return images
68
+
69
+
70
+ block = gr.Blocks(css=css)
71
+
72
+
73
+ with block:
74
+ gr.HTML(header_html)
75
+ with gr.Group():
76
+ with gr.Box():
77
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
78
+ audio = gr.Audio(
79
+ label="Describe a prompt",
80
+ source="microphone",
81
+ type="filepath"
82
+ ).style(
83
+ border=(True, False, True, True),
84
+ rounded=(True, False, False, True),
85
+ container=False,
86
+ )
87
+ btn = gr.Button("Generate image").style(
88
+ margin=False,
89
+ rounded=(False, True, True, False),
90
+ )
91
+
92
+ gallery = gr.Gallery(
93
+ label="Generated images", show_label=False, elem_id="gallery"
94
+ ).style(grid=[2], height="auto")
95
+
96
+ advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
97
+
98
+ with gr.Row(elem_id="advanced-options"):
99
+ samples = gr.Slider(label="Images", minimum=1,
100
+ maximum=4, value=4, step=1)
101
+ steps = gr.Slider(label="Steps", minimum=1,
102
+ maximum=50, value=45, step=1)
103
+ scale = gr.Slider(
104
+ label="Guidance Scale", minimum=0, maximum=50, value=7.5, step=0.1
105
+ )
106
+ seed = gr.Slider(
107
+ label="Seed",
108
+ minimum=0,
109
+ maximum=2147483647,
110
+ step=1,
111
+ randomize=True,
112
+ )
113
+
114
+ ex = gr.Examples(fn=infer, inputs=[
115
+ audio, samples, steps, scale, seed], outputs=gallery)
116
+ ex.dataset.headers = [""]
117
+
118
+ audio.submit(infer, inputs=[audio, samples,
119
+ steps, scale, seed], outputs=gallery)
120
+ btn.click(infer, inputs=[audio, samples, steps,
121
+ scale, seed], outputs=gallery)
122
+ advanced_button.click(
123
+ None,
124
+ [],
125
+ audio,
126
+ _js="""
127
+ () => {
128
+ const options = document.querySelector("body > gradio-app").querySelector("#advanced-options");
129
+ options.style.display = ["none", ""].includes(options.style.display) ? "flex" : "none";
130
+ }""",
131
+ )
132
+ gr.HTML(footer_html)
133
+
134
+ block.queue(max_size=25).launch()
examples.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ examples = [
2
+ [
3
+ 'A high tech solarpunk utopia in the Amazon rainforest',
4
+ 4,
5
+ 45,
6
+ 7.5,
7
+ 1024,
8
+ ],
9
+ [
10
+ 'A pikachu fine dining with a view to the Eiffel Tower',
11
+ 4,
12
+ 45,
13
+ 7,
14
+ 1024,
15
+ ],
16
+ [
17
+ 'A mecha robot in a favela in expressionist style',
18
+ 4,
19
+ 45,
20
+ 7,
21
+ 1024,
22
+ ],
23
+ [
24
+ 'an insect robot preparing a delicious meal',
25
+ 4,
26
+ 45,
27
+ 7,
28
+ 1024,
29
+ ],
30
+ [
31
+ "A small cabin on top of a snowy mountain in the style of Disney, artstation",
32
+ 4,
33
+ 45,
34
+ 7,
35
+ 1024,
36
+ ],
37
+ ]
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ datasets
4
+ nvidia-ml-py3
5
+ ftfy
6
+ --extra-index-url https://download.pytorch.org/whl/cu113 torch
styles.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ header_html = """
2
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
3
+ <div
4
+ style="
5
+ display: inline-flex;
6
+ align-items: center;
7
+ gap: 0.8rem;
8
+ font-size: 1.75rem;
9
+ "
10
+ >
11
+ <svg
12
+ width="0.65em"
13
+ height="0.65em"
14
+ viewBox="0 0 115 115"
15
+ fill="none"
16
+ xmlns="http://www.w3.org/2000/svg"
17
+ >
18
+ <rect width="23" height="23" fill="white"></rect>
19
+ <rect y="69" width="23" height="23" fill="white"></rect>
20
+ <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
21
+ <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
22
+ <rect x="46" width="23" height="23" fill="white"></rect>
23
+ <rect x="46" y="69" width="23" height="23" fill="white"></rect>
24
+ <rect x="69" width="23" height="23" fill="black"></rect>
25
+ <rect x="69" y="69" width="23" height="23" fill="black"></rect>
26
+ <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
27
+ <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
28
+ <rect x="115" y="46" width="23" height="23" fill="white"></rect>
29
+ <rect x="115" y="115" width="23" height="23" fill="white"></rect>
30
+ <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
31
+ <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
32
+ <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
33
+ <rect x="92" y="69" width="23" height="23" fill="white"></rect>
34
+ <rect x="69" y="46" width="23" height="23" fill="white"></rect>
35
+ <rect x="69" y="115" width="23" height="23" fill="white"></rect>
36
+ <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
37
+ <rect x="46" y="46" width="23" height="23" fill="black"></rect>
38
+ <rect x="46" y="115" width="23" height="23" fill="black"></rect>
39
+ <rect x="46" y="69" width="23" height="23" fill="black"></rect>
40
+ <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
41
+ <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
42
+ <rect x="23" y="69" width="23" height="23" fill="black"></rect>
43
+ </svg>
44
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
45
+ Stable Diffusion Demo
46
+ </h1>
47
+ </div>
48
+ <p style="margin-bottom: 10px; font-size: 94%">
49
+ Stable Diffusion is a state of the art text-to-image model that generates
50
+ images from text.<br>For faster generation and forthcoming API
51
+ access you can try
52
+ <a
53
+ href="http://beta.dreamstudio.ai/"
54
+ style="text-decoration: underline;"
55
+ target="_blank"
56
+ >DreamStudio Beta</a
57
+ >
58
+ </p>
59
+ </div>
60
+ """
61
+
62
+
63
+ footer_html = """
64
+ <div class="footer">
65
+ <p>Model by <a href="https://huggingface.co/CompVis" style="text-decoration: underline;" target="_blank">CompVis</a> and <a href="https://huggingface.co/stabilityai" style="text-decoration: underline;" target="_blank">Stability AI</a> - Gradio Demo by 🤗 Hugging Face
66
+ </p>
67
+ </div>
68
+ <div class="acknowledgments">
69
+ <p><h4>LICENSE</h4>
70
+ The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
71
+ <p><h4>Biases and content acknowledgment</h4>
72
+ Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
73
+ </div>
74
+ """
75
+
76
+ css = """
77
+ .gradio-container {
78
+ font-family: 'IBM Plex Sans', sans-serif;
79
+ }
80
+ .gr-button {
81
+ color: white;
82
+ border-color: black;
83
+ background: black;
84
+ }
85
+ input[type='range'] {
86
+ accent-color: black;
87
+ }
88
+ .dark input[type='range'] {
89
+ accent-color: #dfdfdf;
90
+ }
91
+ .container {
92
+ max-width: 730px;
93
+ margin: auto;
94
+ padding-top: 1.5rem;
95
+ }
96
+ #gallery {
97
+ min-height: 22rem;
98
+ margin-bottom: 15px;
99
+ margin-left: auto;
100
+ margin-right: auto;
101
+ border-bottom-right-radius: .5rem !important;
102
+ border-bottom-left-radius: .5rem !important;
103
+ }
104
+ #gallery>div>.h-full {
105
+ min-height: 20rem;
106
+ }
107
+ .details:hover {
108
+ text-decoration: underline;
109
+ }
110
+ .gr-button {
111
+ white-space: nowrap;
112
+ }
113
+ .gr-button:focus {
114
+ border-color: rgb(147 197 253 / var(--tw-border-opacity));
115
+ outline: none;
116
+ box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
117
+ --tw-border-opacity: 1;
118
+ --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
119
+ --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
120
+ --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
121
+ --tw-ring-opacity: .5;
122
+ }
123
+ #advanced-btn {
124
+ font-size: .7rem !important;
125
+ line-height: 19px;
126
+ margin-top: 12px;
127
+ margin-bottom: 12px;
128
+ padding: 2px 8px;
129
+ border-radius: 14px !important;
130
+ }
131
+ #advanced-options {
132
+ display: none;
133
+ margin-bottom: 20px;
134
+ }
135
+ .footer {
136
+ margin-bottom: 45px;
137
+ margin-top: 35px;
138
+ text-align: center;
139
+ border-bottom: 1px solid #e5e5e5;
140
+ }
141
+ .footer>p {
142
+ font-size: .8rem;
143
+ display: inline-block;
144
+ padding: 0 10px;
145
+ transform: translateY(10px);
146
+ background: white;
147
+ }
148
+ .dark .footer {
149
+ border-color: #303030;
150
+ }
151
+ .dark .footer>p {
152
+ background: #0b0f19;
153
+ }
154
+ .acknowledgments h4{
155
+ margin: 1.25em 0 .25em 0;
156
+ font-weight: bold;
157
+ font-size: 115%;
158
+ }
159
+ """