Spaces:
Running
Running
InferenceLab
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,105 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import asyncio
|
| 3 |
+
import wave
|
| 4 |
+
import os
|
| 5 |
+
from google import genai
|
| 6 |
+
from google.genai import types
|
| 7 |
+
|
| 8 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 9 |
+
client = genai.Client(http_options={'api_version': 'v1alpha'}, api_key=GOOGLE_API_KEY)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Save PCM audio to WAV file
|
| 13 |
+
def save_wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
| 14 |
+
with wave.open(filename, "wb") as wf:
|
| 15 |
+
wf.setnchannels(channels)
|
| 16 |
+
wf.setsampwidth(sample_width)
|
| 17 |
+
wf.setframerate(rate)
|
| 18 |
+
wf.writeframes(pcm)
|
| 19 |
+
|
| 20 |
+
# Async music generation
|
| 21 |
+
async def generate_music(prompt, bpm, temperature):
|
| 22 |
+
audio_chunks = []
|
| 23 |
+
|
| 24 |
+
async def receive_audio(session):
|
| 25 |
+
async for message in session.receive():
|
| 26 |
+
chunk = message.server_content.audio_chunks[0].data
|
| 27 |
+
audio_chunks.append(chunk)
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
async with (
|
| 31 |
+
client.aio.live.music.connect(model='models/lyria-realtime-exp') as session,
|
| 32 |
+
asyncio.TaskGroup() as tg,
|
| 33 |
+
):
|
| 34 |
+
tg.create_task(receive_audio(session))
|
| 35 |
+
|
| 36 |
+
await session.set_weighted_prompts([
|
| 37 |
+
types.WeightedPrompt(text=prompt, weight=1.0)
|
| 38 |
+
])
|
| 39 |
+
await session.set_music_generation_config(
|
| 40 |
+
types.LiveMusicGenerationConfig(bpm=int(bpm), temperature=float(temperature))
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
await session.play()
|
| 44 |
+
await asyncio.sleep(5) # Streaming duration
|
| 45 |
+
await session.pause()
|
| 46 |
+
|
| 47 |
+
all_pcm = b"".join(audio_chunks)
|
| 48 |
+
output_path = "generated_music.wav"
|
| 49 |
+
save_wave_file(output_path, all_pcm)
|
| 50 |
+
return output_path, output_path, "Music generated successfully."
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return None, None, f"Error: {str(e)}"
|
| 54 |
+
|
| 55 |
+
# Wrapper for Gradio to run async function
|
| 56 |
+
def generate_music_gradio(prompt, bpm, temperature):
|
| 57 |
+
return asyncio.run(generate_music(prompt, bpm, temperature))
|
| 58 |
+
|
| 59 |
+
# Gradio UI
|
| 60 |
+
with gr.Blocks(title="Gemini Lyria Music Generator") as demo:
|
| 61 |
+
gr.Markdown("## Lyria Music Generator")
|
| 62 |
+
|
| 63 |
+
# Section 1: Input
|
| 64 |
+
with gr.Group():
|
| 65 |
+
gr.Markdown("### Input")
|
| 66 |
+
with gr.Row():
|
| 67 |
+
prompt_input = gr.Textbox(
|
| 68 |
+
label="Music Style / Prompt",
|
| 69 |
+
placeholder="e.g., ambient synth, minimal techno, classical piano"
|
| 70 |
+
)
|
| 71 |
+
with gr.Row():
|
| 72 |
+
bpm_input = gr.Slider(label="BPM", minimum=60, maximum=180, value=90)
|
| 73 |
+
temp_input = gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=1.0)
|
| 74 |
+
generate_btn = gr.Button("Generate Music")
|
| 75 |
+
|
| 76 |
+
# Section 2: Output
|
| 77 |
+
with gr.Group():
|
| 78 |
+
gr.Markdown("### Output")
|
| 79 |
+
with gr.Row():
|
| 80 |
+
output_audio = gr.Audio(label="Generated Audio")
|
| 81 |
+
download_file = gr.File(label="Download WAV")
|
| 82 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 83 |
+
|
| 84 |
+
# Section 3: Examples
|
| 85 |
+
with gr.Group():
|
| 86 |
+
gr.Markdown("### Examples")
|
| 87 |
+
examples = gr.Examples(
|
| 88 |
+
examples=[
|
| 89 |
+
["minimal techno", 125, 1.0],
|
| 90 |
+
["classical piano in a rainy mood", 70, 0.9],
|
| 91 |
+
["ambient space drone", 90, 1.5],
|
| 92 |
+
["lo-fi chill beats", 80, 1.0],
|
| 93 |
+
["orchestral epic music", 110, 1.2],
|
| 94 |
+
],
|
| 95 |
+
inputs=[prompt_input, bpm_input, temp_input]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Event binding
|
| 99 |
+
generate_btn.click(
|
| 100 |
+
fn=generate_music_gradio,
|
| 101 |
+
inputs=[prompt_input, bpm_input, temp_input],
|
| 102 |
+
outputs=[output_audio, download_file, status_output]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
demo.launch()
|