bdc-divya commited on
Commit
0a20992
·
1 Parent(s): 6239fd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -1,38 +1,45 @@
1
- import tempfile
2
- from audiocraft.models import MusicGen
3
- from audiocraft.data.audio import audio_write
4
- import gradio as gr
5
- import torch
6
- import tempfile
7
- import uuid
8
- import os
9
- from scipy.io.wavfile import write
10
 
11
  # Load the model and set parameters
12
- model = MusicGen.get_pretrained("facebook/musicgen-small")
13
- model.set_generation_params(duration=8)
14
 
 
15
  def generate_music(description):
16
- # Generate audio
17
- wav = model.generate([description])
18
- audio_array = wav.cpu().numpy().squeeze()
19
- sample_rate = model.sample_rate
 
 
20
  file_id = uuid.uuid1()
 
 
21
  file_path = os.path.join(
22
- tempfile.gettempdir(),
23
- f'{file_id}.wav'
24
  )
 
 
25
  write(file_path, rate=sample_rate, data=audio_array)
 
 
26
  return file_path
27
 
28
  # Define Gradio interface with temporary file output
29
  iface = gr.Interface(
30
- fn=generate_music,
31
- inputs="text",
32
- outputs=gr.components.Audio(type="filepath", label="Audio"),
33
- title="Text to Audio Generation",
34
- description="Generate audio based on text descriptions.",
35
- live=False,
36
- )
37
-
38
- iface.launch(debug=True)
 
1
+ # Import necessary libraries
2
+ import tempfile # Library for temporary file operations
3
+ from audiocraft.models import MusicGen # Import the MusicGen model for music generation
4
+ from audiocraft.data.audio import audio_write # Import audio_write function for saving audio
5
+ import gradio as gr # Import Gradio for creating a web interface
6
+ import torch # Import PyTorch for deep learning operations
7
+ import uuid # Library for generating unique identifiers (UUIDs)
8
+ import os # Operating system-related functions
9
+ from scipy.io.wavfile import write # Function for writing audio to a WAV file
10
 
11
  # Load the model and set parameters
12
+ model = MusicGen.get_pretrained("facebook/musicgen-small") # Load a pre-trained MusicGen model
13
+ model.set_generation_params(duration=8) # Set the duration for audio generation in seconds
14
 
15
+ # Function to generate music from text descriptions
16
  def generate_music(description):
17
+ # Generate audio based on the provided description
18
+ wav = model.generate([description]) # Generate audio using the model
19
+ audio_array = wav.cpu().numpy().squeeze() # Convert the audio to a NumPy array
20
+ sample_rate = model.sample_rate # Get the audio sample rate (e.g., 44100 Hz)
21
+
22
+ # Generate a unique file identifier (UUID) for the temporary audio file
23
  file_id = uuid.uuid1()
24
+
25
+ # Define the path for the temporary audio file using the identifier
26
  file_path = os.path.join(
27
+ tempfile.gettempdir(), # Get the temporary directory path
28
+ f'{file_id}.wav' # Create a unique file name with the UUID and .wav extension
29
  )
30
+
31
+ # Write the generated audio to the temporary file as a WAV file
32
  write(file_path, rate=sample_rate, data=audio_array)
33
+
34
+ # Return the path to the temporary audio file
35
  return file_path
36
 
37
  # Define Gradio interface with temporary file output
38
  iface = gr.Interface(
39
+ fn=generate_music, # Use the generate_music function for processing input
40
+ inputs="text", # Accept text input from the user
41
+ outputs=gr.components.Audio(type="filepath", label="Audio"), # Display the generated audio as output
42
+ title="Text to Audio Generation", # Set the title of the web interface
43
+ description="Generate audio based on text descriptions.", # Provide a description
44
+ live=False, # Set to False if you don't want real-time updates (for beginner-friendly interaction)
45
+ )