JAM / app.py
hungchiayu's picture
Update app.py
c68d858
raw
history blame
2.52 kB
import spaces
import gradio as gr
import os
from model import Jamify
# Initialize the Jamify model once
print("Initializing Jamify model...")
jamify_model = Jamify()
print("Jamify model ready.")
@spaces.GPU(duration=100)
def generate_song(reference_audio, lyrics_file, style_prompt, duration):
# We need to save the uploaded files to temporary paths to pass to the model
reference_audio = reference_audio if reference_audio else None
# The model expects paths, so we write the prompt to a temp file if needed
# (This part of the model could be improved to accept the string directly)
output_path = jamify_model.predict(
reference_audio_path=ref_audio_path,
lyrics_json_path=lyrics_path,
style_prompt=style_prompt,
duration_sec=duration
)
return output_path
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Jamify: Music Generation from Lyrics and Style")
gr.Markdown("Provide your lyrics, a style reference (either an audio file or a text prompt), and a desired duration to generate a song.")
with gr.Row():
with gr.Column():
gr.Markdown("### Inputs")
lyrics_file = gr.File(label="Lyrics File (.json)", type="filepath")
duration_slider = gr.Slider(minimum=5, maximum=180, value=30, step=1, label="Duration (seconds)")
with gr.Tab("Style from Audio"):
reference_audio = gr.File(label="Reference Audio (.mp3, .wav)", type="filepath")
with gr.Tab("Style from Text"):
style_prompt = gr.Textbox(label="Style Prompt", lines=3, placeholder="e.g., A high-energy electronic dance track with a strong bassline and euphoric synths.")
generate_button = gr.Button("Generate Song", variant="primary")
with gr.Column():
gr.Markdown("### Output")
output_audio = gr.Audio(label="Generated Song")
generate_button.click(
fn=generate_song,
inputs=[reference_audio, lyrics_file, style_prompt, duration_slider],
outputs=output_audio,
api_name="generate_song"
)
gr.Markdown("### Example Usage")
gr.Examples(
examples=[
[None, "gt0.json", "A sad, slow, acoustic country song", 30],
],
inputs=[reference_audio, lyrics_file, style_prompt, duration_slider],
outputs=output_audio,
fn=generate_song,
cache_examples=True
)
demo.queue().launch()