stardate69 commited on
Commit
38967a3
·
verified ·
1 Parent(s): a0627ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -1,19 +1,17 @@
1
- import torch
2
  import os
 
3
  import soundfile as sf
4
- from diffusers import StableAudioPipeline
5
  from huggingface_hub import login
 
6
  import gradio as gr
7
 
8
- # Retrieve the token securely from environment variables
9
  HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
10
  if HUGGINGFACE_TOKEN is None:
11
- raise ValueError("Missing Hugging Face API token. Set 'HF_TOKEN' in Secrets.")
12
-
13
- # Authenticate with Hugging Face Hub
14
  login(HUGGINGFACE_TOKEN)
15
 
16
- # Set up the device
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
19
 
@@ -24,22 +22,21 @@ pipe = StableAudioPipeline.from_pretrained(
24
  )
25
  pipe = pipe.to(device)
26
 
27
- # Audio generation function
28
- def generate_audio(prompt, negative_prompt, duration, seed):
29
  generator = torch.Generator(device).manual_seed(seed)
30
  audio_output = pipe(
31
  prompt=prompt,
32
  negative_prompt=negative_prompt,
33
- num_inference_steps=50,
34
  audio_end_in_s=duration,
35
  num_waveforms_per_prompt=1,
36
  generator=generator
37
  ).audios
38
-
39
- # Save the generated audio
40
  output_audio = audio_output[0].T.float().cpu().numpy()
41
- sf.write("output.wav", output_audio, pipe.vae.sampling_rate)
42
- return "output.wav"
 
43
 
44
  # Gradio UI
45
  with gr.Blocks() as demo:
@@ -69,4 +66,3 @@ with gr.Blocks() as demo:
69
 
70
  # Launch the app
71
  demo.launch()
72
-
 
 
1
  import os
2
+ import torch
3
  import soundfile as sf
 
4
  from huggingface_hub import login
5
+ from diffusers import StableAudioPipeline
6
  import gradio as gr
7
 
8
+ # Load Hugging Face token securely
9
  HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
10
  if HUGGINGFACE_TOKEN is None:
11
+ raise ValueError("Missing Hugging Face token. Please set it in Spaces Secrets.")
 
 
12
  login(HUGGINGFACE_TOKEN)
13
 
14
+ # Set device for PyTorch
15
  device = "cuda" if torch.cuda.is_available() else "cpu"
16
  torch_dtype = torch.float16 if device == "cuda" else torch.float32
17
 
 
22
  )
23
  pipe = pipe.to(device)
24
 
25
+ # Function to generate audio
26
+ def generate_audio(prompt, negative_prompt, duration, diffusion_steps, seed):
27
  generator = torch.Generator(device).manual_seed(seed)
28
  audio_output = pipe(
29
  prompt=prompt,
30
  negative_prompt=negative_prompt,
31
+ num_inference_steps=int(diffusion_steps), # Number of diffusion steps
32
  audio_end_in_s=duration,
33
  num_waveforms_per_prompt=1,
34
  generator=generator
35
  ).audios
 
 
36
  output_audio = audio_output[0].T.float().cpu().numpy()
37
+ output_file = "output.wav"
38
+ sf.write(output_file, output_audio, pipe.vae.sampling_rate)
39
+ return output_file
40
 
41
  # Gradio UI
42
  with gr.Blocks() as demo:
 
66
 
67
  # Launch the app
68
  demo.launch()