manoskary commited on
Commit
a96e7fc
·
verified ·
1 Parent(s): 51bbb25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -19
app.py CHANGED
@@ -63,7 +63,7 @@ def load_model():
63
 
64
  # Function to set up, generate, and process the audio
65
  @spaces.GPU(duration=120) # Allocate GPU only when this function is called
66
- def generate_audio(prompt, seconds_total=30, steps=100, cfg_scale=7):
67
  """
68
  Generate instrumental music using Stable Audio.
69
  This function runs on GPU via @spaces.GPU decorator.
@@ -128,19 +128,10 @@ def generate_audio(prompt, seconds_total=30, steps=100, cfg_scale=7):
128
  output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
129
  print("Audio normalized and converted.")
130
 
131
- # Generate a unique filename for the output
132
- unique_filename = f"output_{uuid.uuid4().hex}.wav"
133
- print(f"Saving audio to file: {unique_filename}")
134
-
135
- # Save to file
136
- torchaudio.save(unique_filename, output, sample_rate)
137
- print(f"Audio saved: {unique_filename}")
138
-
139
- # Return the path to the generated audio file
140
- return unique_filename
141
-
142
 
143
- def save_audio_to_file(output:np.array) -> str:
144
  # Generate a unique filename for the output
145
  unique_filename = f"output_{uuid.uuid4().hex}.wav"
146
  print(f"Saving audio to file: {unique_filename}")
@@ -152,16 +143,11 @@ def save_audio_to_file(output:np.array) -> str:
152
  # Return the path to the generated audio file
153
  return unique_filename
154
 
155
-
156
- def inf_function(prompt, seconds_total=30, steps=100, cfg_scale=7):
157
- output = generate_audio(prompt, seconds_total, steps, cfg_scale)
158
- filename = save_audio_to_file(output)
159
- return filename
160
 
161
 
162
  # Setting up the Gradio Interface
163
  interface = gr.Interface(
164
- fn=inf_function,
165
  inputs=[
166
  gr.Textbox(
167
  label="Prompt",
 
63
 
64
  # Function to set up, generate, and process the audio
65
  @spaces.GPU(duration=120) # Allocate GPU only when this function is called
66
+ def generate_audio(prompt, seconds_total=30, steps=100, cfg_scale=7, return_raw_audio=False):
67
  """
68
  Generate instrumental music using Stable Audio.
69
  This function runs on GPU via @spaces.GPU decorator.
 
128
  output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
129
  print("Audio normalized and converted.")
130
 
131
+ # only used through the API
132
+ if return_raw_audio:
133
+ return output
 
 
 
 
 
 
 
 
134
 
 
135
  # Generate a unique filename for the output
136
  unique_filename = f"output_{uuid.uuid4().hex}.wav"
137
  print(f"Saving audio to file: {unique_filename}")
 
143
  # Return the path to the generated audio file
144
  return unique_filename
145
 
 
 
 
 
 
146
 
147
 
148
  # Setting up the Gradio Interface
149
  interface = gr.Interface(
150
+ fn=generate_audio,
151
  inputs=[
152
  gr.Textbox(
153
  label="Prompt",