sanchit-gandhi HF staff commited on
Commit
529049e
1 Parent(s): d925450

remove gr.Box (deprecated)

Browse files
Files changed (4) hide show
  1. MANIFEST.in +0 -2
  2. app.py +98 -245
  3. bg.png +0 -0
  4. share_btn.py +0 -74
MANIFEST.in DELETED
@@ -1,2 +0,0 @@
1
- include *.py LICENSE README.md
2
- recursive-include audioldm2 *.txt *.py *.gz *.npy *.json
 
 
 
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import torch
3
  from diffusers import AudioLDM2Pipeline
4
- from share_btn import community_icon_html, loading_icon_html, share_js
5
 
6
 
7
  # make Space compatible with CPU duplicates
@@ -38,143 +37,7 @@ def text2audio(text, negative_prompt, duration, guidance_scale, random_seed, n_c
38
  return gr.make_waveform((16000, waveforms[0]), bg_image="bg.png")
39
 
40
 
41
- css = """
42
- a {
43
- color: inherit;
44
- text-decoration: underline;
45
- }
46
- .gradio-container {
47
- font-family: 'IBM Plex Sans', sans-serif;
48
- max-width: 730px !important;
49
- }
50
- .gr-button {
51
- color: white;
52
- border-color: #000000;
53
- background: #000000;
54
- }
55
- input[type='range'] {
56
- accent-color: #000000;
57
- }
58
- .dark input[type='range'] {
59
- accent-color: #dfdfdf;
60
- }
61
- .container {
62
- margin: auto;
63
- padding-top: 1.5rem;
64
- }
65
- #gallery {
66
- min-height: 22rem;
67
- margin-bottom: 15px;
68
- margin-left: auto;
69
- margin-right: auto;
70
- border-bottom-right-radius: .5rem !important;
71
- border-bottom-left-radius: .5rem !important;
72
- }
73
- #gallery>div>.h-full {
74
- min-height: 20rem;
75
- }
76
- .details:hover {
77
- text-decoration: underline;
78
- }
79
- .gr-button {
80
- white-space: nowrap;
81
- }
82
- .gr-button:focus {
83
- border-color: rgb(147 197 253 / var(--tw-border-opacity));
84
- outline: none;
85
- box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
86
- --tw-border-opacity: 1;
87
- --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
88
- --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
89
- --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
90
- --tw-ring-opacity: .5;
91
- }
92
- #advanced-btn {
93
- font-size: .7rem !important;
94
- line-height: 19px;
95
- margin-top: 12px;
96
- margin-bottom: 12px;
97
- padding: 2px 8px;
98
- border-radius: 14px !important;
99
- }
100
- #advanced-options {
101
- margin-bottom: 20px;
102
- }
103
- .footer {
104
- margin-bottom: 45px;
105
- margin-top: 35px;
106
- text-align: center;
107
- border-bottom: 1px solid #e5e5e5;
108
- }
109
- .footer>p {
110
- font-size: .8rem;
111
- display: inline-block;
112
- padding: 0 10px;
113
- transform: translateY(10px);
114
- background: white;
115
- }
116
- .dark .footer {
117
- border-color: #303030;
118
- }
119
- .dark .footer>p {
120
- background: #0b0f19;
121
- }
122
- .acknowledgments h4{
123
- margin: 1.25em 0 .25em 0;
124
- font-weight: bold;
125
- font-size: 115%;
126
- }
127
- #container-advanced-btns{
128
- display: flex;
129
- flex-wrap: wrap;
130
- justify-content: space-between;
131
- align-items: center;
132
- }
133
- .animate-spin {
134
- animation: spin 1s linear infinite;
135
- }
136
- @keyframes spin {
137
- from {
138
- transform: rotate(0deg);
139
- }
140
- to {
141
- transform: rotate(360deg);
142
- }
143
- }
144
- #share-btn-container {
145
- display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
146
- margin-top: 10px;
147
- margin-left: auto;
148
- }
149
- #share-btn {
150
- all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;right:0;
151
- }
152
- #share-btn * {
153
- all: unset;
154
- }
155
- #share-btn-container div:nth-child(-n+2){
156
- width: auto !important;
157
- min-height: 0px !important;
158
- }
159
- #share-btn-container .wrap {
160
- display: none !important;
161
- }
162
- .gr-form{
163
- flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
164
- }
165
- #prompt-container{
166
- gap: 0;
167
- }
168
- #generated_id{
169
- min-height: 700px
170
- }
171
- #setting_id{
172
- margin-bottom: 12px;
173
- text-align: center;
174
- font-weight: 900;
175
- }
176
- """
177
- iface = gr.Blocks(css=css)
178
 
179
  with iface:
180
  gr.HTML(
@@ -196,124 +59,114 @@ with iface:
196
  </div>
197
  """
198
  )
199
- gr.HTML(
200
- """
201
- <p>This is the demo for AudioLDM 2, powered by 🧨 Diffusers. Demo uses the checkpoint <a
202
  href="https://huggingface.co/cvssp/audioldm2"> AudioLDM 2 base</a>. For faster inference without waiting in
203
- queue, you may duplicate the space and upgrade to a GPU in the settings. <br/> <a
204
- href="https://huggingface.co/spaces/haoheliu/audioldm2-text2audio-text2music?duplicate=true"> <img
205
- style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a> <p/>
206
- """
207
- )
208
 
209
  with gr.Group():
210
- with gr.Box():
211
- textbox = gr.Textbox(
212
- value="The vibrant beat of Brazilian samba drums.",
213
- max_lines=1,
214
- label="Input text",
215
- info="Your text is important for the audio quality. Please ensure it is descriptive by using more adjectives.",
216
- elem_id="prompt-in",
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  )
218
- negative_textbox = gr.Textbox(
219
- value="Low quality.",
220
- max_lines=1,
221
- label="Negative prompt",
222
- info="Enter a negative prompt not to guide the audio generation. Selecting appropriate negative prompts can improve the audio quality significantly.",
223
- elem_id="prompt-in",
 
 
 
 
 
 
 
 
 
 
224
  )
225
 
226
- with gr.Accordion("Click to modify detailed configurations", open=False):
227
- seed = gr.Number(
228
- value=45,
229
- label="Seed",
230
- info="Change this value (any integer number) will lead to a different generation result.",
231
- )
232
- duration = gr.Slider(5, 15, value=10, step=2.5, label="Duration (seconds)")
233
- guidance_scale = gr.Slider(
234
- 0,
235
- 7,
236
- value=3.5,
237
- step=0.5,
238
- label="Guidance scale",
239
- info="Larger => better quality and relevancy to text; Smaller => better diversity",
240
- )
241
- n_candidates = gr.Slider(
242
- 1,
243
- 5,
244
- value=3,
245
- step=1,
246
- label="Number waveforms to generate",
247
- info="Automatic quality control. This number control the number of candidates (e.g., generate three audios and choose the best to show you). A larger value usually lead to better quality with heavier computation",
248
- )
249
-
250
- outputs = gr.Video(label="Output", elem_id="output-video")
251
- btn = gr.Button("Submit").style(full_width=True)
252
-
253
- with gr.Group(elem_id="share-btn-container", visible=False):
254
- community_icon = gr.HTML(community_icon_html)
255
- loading_icon = gr.HTML(loading_icon_html)
256
- share_button = gr.Button("Share to community", elem_id="share-btn")
257
 
258
- btn.click(
259
- text2audio,
260
- inputs=[textbox, negative_textbox, duration, guidance_scale, seed, n_candidates],
261
- outputs=[outputs],
262
- )
263
 
264
- share_button.click(None, [], [], _js=share_js)
265
- gr.HTML(
266
- """
267
- <div class="footer" style="text-align: center; max-width: 700px; margin: 0 auto;">
268
- <p>Follow the latest update of AudioLDM 2 on our<a href="https://audioldm.github.io/audioldm2"
269
- style="text-decoration: underline;" target="_blank"> Github repo</a> </p> <br> <p>Model by <a
270
- href="https://twitter.com/LiuHaohe" style="text-decoration: underline;" target="_blank">Haohe
271
- Liu</a>. Code and demo by 🤗 Hugging Face.</p> <br>
272
- </div>
273
  """
274
- )
275
- gr.Examples(
276
- [
277
- ["A hammer is hitting a wooden surface.", "Low quality.", 10, 3.5, 45, 3],
278
- ["A cat is meowing for attention.", "Low quality.", 10, 3.5, 45, 3],
279
- ["An excited crowd cheering at a sports game.", "Low quality.", 10, 3.5, 45, 3],
280
- ["Birds singing sweetly in a blooming garden.", "Low quality.", 10, 3.5, 45, 3],
281
- ["A modern synthesizer creating futuristic soundscapes.", "Low quality.", 10, 3.5, 45, 3],
282
- ["The vibrant beat of Brazilian samba drums.", "Low quality.", 10, 3.5, 45, 3],
283
- ],
284
- fn=text2audio,
285
- inputs=[textbox, negative_textbox, duration, guidance_scale, seed, n_candidates],
286
- outputs=[outputs],
287
- cache_examples=True,
288
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  gr.HTML(
290
  """
291
- <div class="acknowledgements"> <p>Essential Tricks for Enhancing the Quality of Your Generated
292
- Audio</p>
293
- <p>1. Try using more adjectives to describe your sound. For example: "A man is speaking
294
- clearly and slowly in a large room" is better than "A man is speaking".</p>
295
- <p>2. Try using different random seeds, which can significantly affect the quality of the generated
296
- output.</p>
297
- <p>3. It's better to use general terms like 'man' or 'woman' instead of specific names for individuals or
298
- abstract objects that humans may not be familiar with.</p>
299
- <p>4. Using a negative prompt to not guide the diffusion process can improve the
300
- audio quality significantly. Try using negative prompts like 'low quality'.</p>
301
- </div>
302
- """
303
  )
304
- with gr.Accordion("Additional information", open=False):
305
- gr.HTML(
306
- """
307
- <div class="acknowledgments">
308
- <p> We build the model with data from <a href="http://research.google.com/audioset/">AudioSet</a>,
309
- <a href="https://freesound.org/">Freesound</a> and <a
310
- href="https://sound-effects.bbcrewind.co.uk/">BBC Sound Effect library</a>. We share this demo
311
- based on the <a
312
- href="https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/375954/Research.pdf">UK
313
- copyright exception</a> of data for academic research.
314
- </p>
315
- </div>
316
- """
317
- )
318
 
319
  iface.queue(max_size=20).launch()
 
1
  import gradio as gr
2
  import torch
3
  from diffusers import AudioLDM2Pipeline
 
4
 
5
 
6
  # make Space compatible with CPU duplicates
 
37
  return gr.make_waveform((16000, waveforms[0]), bg_image="bg.png")
38
 
39
 
40
+ iface = gr.Blocks()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  with iface:
43
  gr.HTML(
 
59
  </div>
60
  """
61
  )
62
+ gr.HTML("""This is the demo for AudioLDM 2, powered by 🧨 Diffusers. Demo uses the checkpoint <a
 
 
63
  href="https://huggingface.co/cvssp/audioldm2"> AudioLDM 2 base</a>. For faster inference without waiting in
64
+ queue, you may duplicate the space and upgrade to a GPU in the settings.""")
65
+ gr.DuplicateButton()
 
 
 
66
 
67
  with gr.Group():
68
+ textbox = gr.Textbox(
69
+ value="The vibrant beat of Brazilian samba drums.",
70
+ max_lines=1,
71
+ label="Input text",
72
+ info="Your text is important for the audio quality. Please ensure it is descriptive by using more adjectives.",
73
+ elem_id="prompt-in",
74
+ )
75
+ negative_textbox = gr.Textbox(
76
+ value="Low quality.",
77
+ max_lines=1,
78
+ label="Negative prompt",
79
+ info="Enter a negative prompt not to guide the audio generation. Selecting appropriate negative prompts can improve the audio quality significantly.",
80
+ elem_id="prompt-in",
81
+ )
82
+
83
+ with gr.Accordion("Click to modify detailed configurations", open=False):
84
+ seed = gr.Number(
85
+ value=45,
86
+ label="Seed",
87
+ info="Change this value (any integer number) will lead to a different generation result.",
88
  )
89
+ duration = gr.Slider(5, 15, value=10, step=2.5, label="Duration (seconds)")
90
+ guidance_scale = gr.Slider(
91
+ 0,
92
+ 7,
93
+ value=3.5,
94
+ step=0.5,
95
+ label="Guidance scale",
96
+ info="Larger => better quality and relevancy to text; Smaller => better diversity",
97
+ )
98
+ n_candidates = gr.Slider(
99
+ 1,
100
+ 5,
101
+ value=3,
102
+ step=1,
103
+ label="Number waveforms to generate",
104
+ info="Automatic quality control. This number control the number of candidates (e.g., generate three audios and choose the best to show you). A larger value usually lead to better quality with heavier computation",
105
  )
106
 
107
+ outputs = gr.Video(label="Output", elem_id="output-video")
108
+ btn = gr.Button("Submit")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ btn.click(
111
+ text2audio,
112
+ inputs=[textbox, negative_textbox, duration, guidance_scale, seed, n_candidates],
113
+ outputs=[outputs],
114
+ )
115
 
116
+ gr.HTML(
 
 
 
 
 
 
 
 
117
  """
118
+ <div class="footer" style="text-align: center">
119
+ <p>Share your generations with the community by clicking the share icon at the top right the generated audio!</p>
120
+ <p>Follow the latest update of AudioLDM 2 on our<a href="https://audioldm.github.io/audioldm2"
121
+ style="text-decoration: underline;" target="_blank"> Github repo</a> </p>
122
+ <p>Model by <a
123
+ href="https://twitter.com/LiuHaohe" style="text-decoration: underline;" target="_blank">Haohe
124
+ Liu</a>. Code and demo by 🤗 Hugging Face.</p>
125
+ </div>
126
+ """
127
+ )
128
+ gr.Examples(
129
+ [
130
+ ["A hammer is hitting a wooden surface.", "Low quality.", 10, 3.5, 45, 3],
131
+ ["A cat is meowing for attention.", "Low quality.", 10, 3.5, 45, 3],
132
+ ["An excited crowd cheering at a sports game.", "Low quality.", 10, 3.5, 45, 3],
133
+ ["Birds singing sweetly in a blooming garden.", "Low quality.", 10, 3.5, 45, 3],
134
+ ["A modern synthesizer creating futuristic soundscapes.", "Low quality.", 10, 3.5, 45, 3],
135
+ ["The vibrant beat of Brazilian samba drums.", "Low quality.", 10, 3.5, 45, 3],
136
+ ],
137
+ fn=text2audio,
138
+ inputs=[textbox, negative_textbox, duration, guidance_scale, seed, n_candidates],
139
+ outputs=[outputs],
140
+ cache_examples=True,
141
+ )
142
+ gr.HTML(
143
+ """
144
+ <div class="acknowledgements"> <p>Essential Tricks for Enhancing the Quality of Your Generated
145
+ Audio</p>
146
+ <p>1. Try using more adjectives to describe your sound. For example: "A man is speaking
147
+ clearly and slowly in a large room" is better than "A man is speaking".</p>
148
+ <p>2. Try using different random seeds, which can significantly affect the quality of the generated
149
+ output.</p>
150
+ <p>3. It's better to use general terms like 'man' or 'woman' instead of specific names for individuals or
151
+ abstract objects that humans may not be familiar with.</p>
152
+ <p>4. Using a negative prompt to not guide the diffusion process can improve the
153
+ audio quality significantly. Try using negative prompts like 'low quality'.</p>
154
+ </div>
155
+ """
156
+ )
157
+ with gr.Accordion("Additional information", open=False):
158
  gr.HTML(
159
  """
160
+ <div class="acknowledgments">
161
+ <p> We build the model with data from <a href="http://research.google.com/audioset/">AudioSet</a>,
162
+ <a href="https://freesound.org/">Freesound</a> and <a
163
+ href="https://sound-effects.bbcrewind.co.uk/">BBC Sound Effect library</a>. We share this demo
164
+ based on the <a
165
+ href="https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/375954/Research.pdf">UK
166
+ copyright exception</a> of data for academic research.
167
+ </p>
168
+ </div>
169
+ """
 
 
170
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  iface.queue(max_size=20).launch()
bg.png DELETED
Binary file (1.28 kB)
 
share_btn.py DELETED
@@ -1,74 +0,0 @@
1
- community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
2
- <path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
3
- <path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
4
- </svg>"""
5
-
6
- loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
7
- style="color: #ffffff;
8
- "
9
- xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
10
-
11
- share_js = """async () => {
12
- async function uploadFile(file){
13
- const UPLOAD_URL = 'https://huggingface.co/uploads';
14
- const response = await fetch(UPLOAD_URL, {
15
- method: 'POST',
16
- headers: {
17
- 'Content-Type': file.type,
18
- 'X-Requested-With': 'XMLHttpRequest',
19
- },
20
- body: file, /// <- File inherits from Blob
21
- });
22
- const url = await response.text();
23
- return url;
24
- }
25
- async function getInputVideoFile(videoEl){
26
- const res = await fetch(videoEl.src);
27
- const blob = await res.blob();
28
- const videoId = Date.now() % 200;
29
- const fileName = `sd-perception-${{videoId}}.mp4`;
30
- return new File([blob], fileName, { type: 'video/mp4' });
31
- }
32
-
33
- async function audioToBase64(audioFile) {
34
- return new Promise((resolve, reject) => {
35
- let reader = new FileReader();
36
- reader.readAsDataURL(audioFile);
37
- reader.onload = () => resolve(reader.result);
38
- reader.onerror = error => reject(error);
39
-
40
- });
41
- }
42
- const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
43
- const inputPromptEl = gradioEl.querySelector('#prompt-in input').value;
44
- const outputVideoEl = gradioEl.querySelector('#output-video video');
45
-
46
- let titleTxt = `Text-to-Audio: ${inputPromptEl}`;
47
-
48
- const shareBtnEl = gradioEl.querySelector('#share-btn');
49
- const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
50
- const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
51
- if(!outputVideoEl){
52
- return;
53
- };
54
- shareBtnEl.style.pointerEvents = 'none';
55
- shareIconEl.style.display = 'none';
56
- loadingIconEl.style.removeProperty('display');
57
- const outputVideo = await getInputVideoFile(outputVideoEl);
58
- const urlOutputVideo = await uploadFile(outputVideo);
59
-
60
- const descriptionMd = `
61
- ##### ${inputPromptEl}
62
-
63
- ${urlOutputVideo}
64
- `;
65
- const params = new URLSearchParams({
66
- title: titleTxt,
67
- description: descriptionMd,
68
- });
69
- const paramsStr = params.toString();
70
- window.open(`https://huggingface.co/spaces/haoheliu/audioldm2-text2audio-text2music/discussions/new?${paramsStr}`, '_blank');
71
- shareBtnEl.style.removeProperty('pointer-events');
72
- shareIconEl.style.removeProperty('display');
73
- loadingIconEl.style.display = 'none';
74
- }"""