Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from audiocraft.models import MusicGen
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
import torchaudio
|
|
@@ -7,101 +7,70 @@ import numpy as np
|
|
| 7 |
import base64
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
import google.generativeai as genai
|
| 10 |
-
load_dotenv()
|
| 11 |
|
|
|
|
|
|
|
| 12 |
genai.configure(api_key=os.getenv("API_KEY"))
|
| 13 |
llm = genai.GenerativeModel("gemini-pro")
|
| 14 |
|
| 15 |
-
|
| 16 |
def load_model():
|
| 17 |
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 18 |
return model
|
| 19 |
|
| 20 |
-
|
| 21 |
-
print(f"Description: {description}")
|
| 22 |
-
print(f"Duration: {duration}")
|
| 23 |
-
model = load_model()
|
| 24 |
-
|
| 25 |
-
model.set_generation_params(
|
| 26 |
-
use_sampling=True,
|
| 27 |
-
top_k=250,
|
| 28 |
-
duration=duration
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
sample_rate = 32000
|
| 41 |
-
save_path = "
|
| 42 |
-
|
| 43 |
-
assert samples.dim() == 2 or samples.dim() == 3
|
| 44 |
samples = samples.detach().cpu()
|
| 45 |
-
|
| 46 |
if samples.dim() == 2:
|
| 47 |
samples = samples[None, ...]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
ORIGINAL PROMPT : {text_area}
|
| 78 |
-
YOUR OUTPUT PROMPT :
|
| 79 |
-
"""
|
| 80 |
-
llm_result = llm.generate_content(context)
|
| 81 |
-
prompt = llm_result.text
|
| 82 |
-
print("")
|
| 83 |
-
|
| 84 |
-
if text_area and time_slider:
|
| 85 |
-
st.json(
|
| 86 |
-
{
|
| 87 |
-
"Description": prompt,
|
| 88 |
-
"Duration": time_slider
|
| 89 |
-
}
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
st.subheader("Generated Music")
|
| 93 |
-
|
| 94 |
-
music_tensors = generate_music_tensors(prompt, time_slider)
|
| 95 |
-
print(f"Music Tensors: {music_tensors}")
|
| 96 |
-
|
| 97 |
-
save_music_file = save_audio(music_tensors)
|
| 98 |
-
|
| 99 |
-
audio_filepath = "saved_audio/audio_0.wav"
|
| 100 |
-
audio_file = open(audio_filepath, 'rb')
|
| 101 |
-
audio_bytes = audio_file.read()
|
| 102 |
-
|
| 103 |
-
st.audio(audio_bytes)
|
| 104 |
-
st.markdown(download_music(audio_filepath, 'Audio'), unsafe_allow_html=True)
|
| 105 |
|
| 106 |
-
|
| 107 |
-
main()
|
|
|
|
| 1 |
from audiocraft.models import MusicGen
|
| 2 |
+
import gradio as gr
|
| 3 |
import os
|
| 4 |
import torch
|
| 5 |
import torchaudio
|
|
|
|
| 7 |
import base64
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
import google.generativeai as genai
|
|
|
|
| 10 |
|
| 11 |
+
# Load API key from environment
|
| 12 |
+
load_dotenv()
|
| 13 |
genai.configure(api_key=os.getenv("API_KEY"))
|
| 14 |
llm = genai.GenerativeModel("gemini-pro")
|
| 15 |
|
| 16 |
+
# Load MusicGen Model
|
| 17 |
def load_model():
|
| 18 |
model = MusicGen.get_pretrained("facebook/musicgen-small")
|
| 19 |
return model
|
| 20 |
|
| 21 |
+
model = load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Function to generate music
|
| 24 |
+
def generate_music(description, duration):
|
| 25 |
+
context = f"""Enhance the following music prompt by adding relevant musical terms, structure, and flow.
|
| 26 |
+
Ensure it's concise but descriptive:
|
| 27 |
+
ORIGINAL PROMPT: {description}
|
| 28 |
+
YOUR OUTPUT PROMPT:"""
|
| 29 |
+
|
| 30 |
+
llm_result = llm.generate_content(context)
|
| 31 |
+
enhanced_prompt = llm_result.text.strip()
|
| 32 |
|
| 33 |
+
model.set_generation_params(use_sampling=True, top_k=250, duration=duration)
|
| 34 |
+
output = model.generate(descriptions=[enhanced_prompt], progress=True, return_tokens=True)
|
| 35 |
+
|
| 36 |
+
return output[0], enhanced_prompt
|
| 37 |
|
| 38 |
+
# Save and return music file path
|
| 39 |
+
def save_audio(samples):
|
| 40 |
sample_rate = 32000
|
| 41 |
+
save_path = "generated_audio.wav"
|
| 42 |
+
|
|
|
|
| 43 |
samples = samples.detach().cpu()
|
|
|
|
| 44 |
if samples.dim() == 2:
|
| 45 |
samples = samples[None, ...]
|
| 46 |
|
| 47 |
+
torchaudio.save(save_path, samples[0], sample_rate)
|
| 48 |
+
return save_path
|
| 49 |
+
|
| 50 |
+
# Function to integrate with Gradio
|
| 51 |
+
def generate_music_and_return(description, duration):
|
| 52 |
+
music_tensors, enhanced_prompt = generate_music(description, duration)
|
| 53 |
+
audio_file_path = save_audio(music_tensors)
|
| 54 |
+
|
| 55 |
+
return enhanced_prompt, audio_file_path
|
| 56 |
+
|
| 57 |
+
# Gradio UI
|
| 58 |
+
with gr.Blocks() as app:
|
| 59 |
+
gr.Markdown("# 🎵 Text-to-Music Generator")
|
| 60 |
+
gr.Markdown("Enter a music description, and our AI will generate a unique audio clip.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
description_input = gr.Textbox(label="Enter music description")
|
| 64 |
+
duration_input = gr.Slider(2, 20, value=5, step=1, label="Select duration (seconds)")
|
| 65 |
+
|
| 66 |
+
generate_button = gr.Button("🎼 Generate Music")
|
| 67 |
+
enhanced_description_output = gr.Textbox(label="Enhanced Description", interactive=False)
|
| 68 |
+
audio_output = gr.Audio(label="Generated Audio")
|
| 69 |
+
|
| 70 |
+
generate_button.click(
|
| 71 |
+
generate_music_and_return,
|
| 72 |
+
inputs=[description_input, duration_input],
|
| 73 |
+
outputs=[enhanced_description_output, audio_output]
|
| 74 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
app.launch()
|
|
|