stardate69 commited on
Commit
fb63790
·
verified ·
1 Parent(s): ff2026f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import os
3
+ import soundfile as sf
4
+ from diffusers import StableAudioPipeline
5
+ from huggingface_hub import login
6
+
7
+ # Retrieve the token securely from environment variables
8
+ HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
9
+ if HUGGINGFACE_TOKEN is None:
10
+ raise ValueError("Missing Hugging Face API token. Set 'HF_TOKEN' in Secrets.")
11
+
12
+ # Authenticate with Hugging Face Hub
13
+ login(HUGGINGFACE_TOKEN)
14
+
15
+ # Set up the device
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ torch_dtype = torch.float16 if device == "cuda" else torch.float32
18
+
19
+ # Load the model
20
+ pipe = StableAudioPipeline.from_pretrained(
21
+ "stabilityai/stable-audio-open-1.0",
22
+ torch_dtype=torch_dtype,
23
+ use_auth_token=True # Token provided via 'login' earlier
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"