Drew commited on
Commit
cda11a5
·
1 Parent(s): 4c23b00
Files changed (1) hide show
  1. app.py +117 -2
app.py CHANGED
@@ -6,6 +6,13 @@ import spaces
6
  import os
7
  import uuid
8
  from pydub import AudioSegment
 
 
 
 
 
 
 
9
 
10
  # Importing the model-related functions
11
  from stable_audio_tools import get_pretrained_model
@@ -90,6 +97,37 @@ def generate_audio(prompt, seconds_total=30, steps=100, cfg_scale=7):
90
  # Return the path to the generated audio file
91
  return full_path_mp3
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  # Setting up the Gradio Interface
94
  interface = gr.Interface(
95
  fn=generate_audio,
@@ -102,9 +140,80 @@ interface = gr.Interface(
102
  outputs=gr.Audio(type="filepath", label="Generated Audio"),
103
  title="Stable Audio Generator",
104
  description="Generate variable-length stereo audio at 44.1kHz from text prompts using Stable Audio Open 1.0.",
105
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
- with gr.Blocks() as demo:
108
  with gr.Tab("Audio"):
109
  audio_prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt here")
110
  audio_duration = gr.Slider(0, 47, value=30, label="Duration in Seconds")
@@ -114,6 +223,12 @@ with gr.Blocks() as demo:
114
  audio_output = gr.Audio(type="filepath", label="Generated Audio")
115
  audio_process_button.click(generate_audio, [audio_prompt, audio_duration, audio_steps, audio_cfg], [audio_output])
116
 
 
 
 
 
 
 
117
  # Pre-load the model to avoid multiprocessing issues
118
  model, model_config = load_model()
119
 
 
6
  import os
7
  import uuid
8
  from pydub import AudioSegment
9
+ import numpy as np
10
+ import random
11
+ import torch
12
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
13
+
14
+
15
+ '''AUDIO'''
16
 
17
  # Importing the model-related functions
18
  from stable_audio_tools import get_pretrained_model
 
97
  # Return the path to the generated audio file
98
  return full_path_mp3
99
 
100
+ '''DIFFUSION'''
101
+ device = "cuda" if torch.cuda.is_available() else "cpu"
102
+ dtype = torch.float16
103
+
104
+ repo = "stabilityai/stable-diffusion-3-medium-diffusers"
105
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)
106
+
107
+ MAX_SEED = np.iinfo(np.int32).max
108
+ MAX_IMAGE_SIZE = 1344
109
+
110
+ @spaces.GPU
111
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
112
+
113
+ if randomize_seed:
114
+ seed = random.randint(0, MAX_SEED)
115
+
116
+ generator = torch.Generator().manual_seed(seed)
117
+
118
+ image = pipe(
119
+ prompt = prompt,
120
+ negative_prompt = negative_prompt,
121
+ guidance_scale = guidance_scale,
122
+ num_inference_steps = num_inference_steps,
123
+ width = width,
124
+ height = height,
125
+ generator = generator
126
+ ).images[0]
127
+
128
+ return image, seed
129
+
130
+ '''
131
  # Setting up the Gradio Interface
132
  interface = gr.Interface(
133
  fn=generate_audio,
 
140
  outputs=gr.Audio(type="filepath", label="Generated Audio"),
141
  title="Stable Audio Generator",
142
  description="Generate variable-length stereo audio at 44.1kHz from text prompts using Stable Audio Open 1.0.",
143
+ )'''
144
+
145
+ with gr.Blocks() as demo:
146
+ with gr.Tab("SD3"):
147
+ with gr.Column:
148
+ gr.Markdown(f"""
149
+ # Demo [Stable Diffusion 3 Medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium)
150
+ Learn more about the [Stable Diffusion 3 series](https://stability.ai/news/stable-diffusion-3). Try on [Stability AI API](https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post), [Stable Assistant](https://stability.ai/stable-assistant), or on Discord via [Stable Artisan](https://stability.ai/stable-artisan). Run locally with [ComfyUI](https://github.com/comfyanonymous/ComfyUI) or [diffusers](https://github.com/huggingface/diffusers)
151
+ """)
152
+ with gr.Row():
153
+ prompt = gr.Text(
154
+ label="Prompt",
155
+ show_label=False,
156
+ max_lines=1,
157
+ placeholder="Enter your prompt",
158
+ container=False,
159
+ )
160
+ run_button = gr.Button("Run", scale=0)
161
+ result = gr.Image(label="Result", show_label=False)
162
+
163
+ with gr.Accordion("Advanced Settings", open=False):
164
+
165
+ negative_prompt = gr.Text(
166
+ label="Negative prompt",
167
+ max_lines=1,
168
+ placeholder="Enter a negative prompt",
169
+ )
170
+
171
+ seed = gr.Slider(
172
+ label="Seed",
173
+ minimum=0,
174
+ maximum=MAX_SEED,
175
+ step=1,
176
+ value=0,
177
+ )
178
+
179
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
180
+
181
+ with gr.Row():
182
+
183
+ width = gr.Slider(
184
+ label="Width",
185
+ minimum=256,
186
+ maximum=MAX_IMAGE_SIZE,
187
+ step=64,
188
+ value=1024,
189
+ )
190
+
191
+ height = gr.Slider(
192
+ label="Height",
193
+ minimum=256,
194
+ maximum=MAX_IMAGE_SIZE,
195
+ step=64,
196
+ value=1024,
197
+ )
198
+
199
+ with gr.Row():
200
+
201
+ guidance_scale = gr.Slider(
202
+ label="Guidance scale",
203
+ minimum=0.0,
204
+ maximum=10.0,
205
+ step=0.1,
206
+ value=5.0,
207
+ )
208
+
209
+ num_inference_steps = gr.Slider(
210
+ label="Number of inference steps",
211
+ minimum=1,
212
+ maximum=50,
213
+ step=1,
214
+ value=28,
215
+ )
216
 
 
217
  with gr.Tab("Audio"):
218
  audio_prompt = gr.Textbox(label="Prompt", placeholder="Enter your text prompt here")
219
  audio_duration = gr.Slider(0, 47, value=30, label="Duration in Seconds")
 
223
  audio_output = gr.Audio(type="filepath", label="Generated Audio")
224
  audio_process_button.click(generate_audio, [audio_prompt, audio_duration, audio_steps, audio_cfg], [audio_output])
225
 
226
+ gr.on(
227
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
228
+ fn = infer,
229
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
230
+ outputs = [result, seed]
231
+ )
232
  # Pre-load the model to avoid multiprocessing issues
233
  model, model_config = load_model()
234