Spaces:
Sleeping
Sleeping
InferenceLab
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,14 +7,14 @@ import nest_asyncio
|
|
7 |
from google import genai
|
8 |
from google.genai import types
|
9 |
|
10 |
-
# Enable nested
|
11 |
nest_asyncio.apply()
|
12 |
|
13 |
-
#
|
14 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
15 |
client = genai.Client(http_options={'api_version': 'v1alpha'}, api_key=GOOGLE_API_KEY)
|
16 |
|
17 |
-
#
|
18 |
def save_wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
19 |
with wave.open(filename, "wb") as wf:
|
20 |
wf.setnchannels(channels)
|
@@ -22,7 +22,7 @@ def save_wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
|
22 |
wf.setframerate(rate)
|
23 |
wf.writeframes(pcm)
|
24 |
|
25 |
-
# Async music generation
|
26 |
async def generate_music(prompt, bpm, temperature):
|
27 |
audio_chunks = []
|
28 |
|
@@ -46,37 +46,37 @@ async def generate_music(prompt, bpm, temperature):
|
|
46 |
)
|
47 |
|
48 |
await session.play()
|
49 |
-
await asyncio.sleep(5) #
|
50 |
await session.pause()
|
51 |
|
52 |
all_pcm = b"".join(audio_chunks)
|
53 |
|
54 |
-
# Save WAV file
|
55 |
output_path = "generated_music.wav"
|
56 |
save_wave_file(output_path, all_pcm)
|
57 |
|
58 |
-
#
|
59 |
audio_np = np.frombuffer(all_pcm, dtype=np.int16)
|
60 |
-
return (24000, audio_np), output_path, "Music generated successfully!"
|
61 |
|
62 |
except Exception as e:
|
63 |
return None, None, f"β Error: {str(e)}"
|
64 |
|
65 |
-
#
|
66 |
def generate_music_gradio(prompt, bpm, temperature):
|
67 |
loop = asyncio.get_event_loop()
|
68 |
return loop.run_until_complete(generate_music(prompt, bpm, temperature))
|
69 |
|
70 |
# Gradio UI
|
71 |
-
with gr.Blocks(title="Gemini Lyria Music Generator") as demo:
|
72 |
gr.Markdown("## πΆ Gemini Lyria Music Generator")
|
73 |
|
74 |
with gr.Group():
|
75 |
-
gr.Markdown("### π
|
76 |
with gr.Row():
|
77 |
prompt_input = gr.Textbox(
|
78 |
label="Music Style / Prompt",
|
79 |
-
placeholder="e.g., ambient synth,
|
80 |
)
|
81 |
with gr.Row():
|
82 |
bpm_input = gr.Slider(label="BPM", minimum=60, maximum=180, value=90)
|
@@ -84,21 +84,21 @@ with gr.Blocks(title="Gemini Lyria Music Generator") as demo:
|
|
84 |
generate_btn = gr.Button("π§ Generate Music")
|
85 |
|
86 |
with gr.Group():
|
87 |
-
gr.Markdown("###
|
88 |
with gr.Row():
|
89 |
output_audio = gr.Audio(label="Generated Audio", type="numpy")
|
90 |
download_file = gr.File(label="Download WAV")
|
91 |
status_output = gr.Textbox(label="Status", interactive=False)
|
92 |
|
93 |
with gr.Group():
|
94 |
-
gr.Markdown("### π Examples")
|
95 |
examples = gr.Examples(
|
96 |
examples=[
|
97 |
-
["
|
98 |
-
["classical piano
|
99 |
-
["
|
100 |
-
["
|
101 |
-
["
|
102 |
],
|
103 |
inputs=[prompt_input, bpm_input, temp_input]
|
104 |
)
|
|
|
7 |
from google import genai
|
8 |
from google.genai import types
|
9 |
|
10 |
+
# Enable nested async for environments like Gradio/Jupyter
|
11 |
nest_asyncio.apply()
|
12 |
|
13 |
+
# Load Google API key from environment
|
14 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
15 |
client = genai.Client(http_options={'api_version': 'v1alpha'}, api_key=GOOGLE_API_KEY)
|
16 |
|
17 |
+
# Helper to save PCM as .wav
|
18 |
def save_wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
|
19 |
with wave.open(filename, "wb") as wf:
|
20 |
wf.setnchannels(channels)
|
|
|
22 |
wf.setframerate(rate)
|
23 |
wf.writeframes(pcm)
|
24 |
|
25 |
+
# Async music generation logic
|
26 |
async def generate_music(prompt, bpm, temperature):
|
27 |
audio_chunks = []
|
28 |
|
|
|
46 |
)
|
47 |
|
48 |
await session.play()
|
49 |
+
await asyncio.sleep(5) # Generation duration
|
50 |
await session.pause()
|
51 |
|
52 |
all_pcm = b"".join(audio_chunks)
|
53 |
|
54 |
+
# Save to WAV file for download
|
55 |
output_path = "generated_music.wav"
|
56 |
save_wave_file(output_path, all_pcm)
|
57 |
|
58 |
+
# Return numpy array for playback (gr.Audio)
|
59 |
audio_np = np.frombuffer(all_pcm, dtype=np.int16)
|
60 |
+
return (24000, audio_np), output_path, "β
Music generated successfully!"
|
61 |
|
62 |
except Exception as e:
|
63 |
return None, None, f"β Error: {str(e)}"
|
64 |
|
65 |
+
# Sync wrapper for Gradio (uses event loop)
|
66 |
def generate_music_gradio(prompt, bpm, temperature):
|
67 |
loop = asyncio.get_event_loop()
|
68 |
return loop.run_until_complete(generate_music(prompt, bpm, temperature))
|
69 |
|
70 |
# Gradio UI
|
71 |
+
with gr.Blocks(title="π΅ Gemini Lyria Music Generator") as demo:
|
72 |
gr.Markdown("## πΆ Gemini Lyria Music Generator")
|
73 |
|
74 |
with gr.Group():
|
75 |
+
gr.Markdown("### π Prompt & Controls")
|
76 |
with gr.Row():
|
77 |
prompt_input = gr.Textbox(
|
78 |
label="Music Style / Prompt",
|
79 |
+
placeholder="e.g., ambient synth, classical piano, lo-fi chill"
|
80 |
)
|
81 |
with gr.Row():
|
82 |
bpm_input = gr.Slider(label="BPM", minimum=60, maximum=180, value=90)
|
|
|
84 |
generate_btn = gr.Button("π§ Generate Music")
|
85 |
|
86 |
with gr.Group():
|
87 |
+
gr.Markdown("### πΌ Output")
|
88 |
with gr.Row():
|
89 |
output_audio = gr.Audio(label="Generated Audio", type="numpy")
|
90 |
download_file = gr.File(label="Download WAV")
|
91 |
status_output = gr.Textbox(label="Status", interactive=False)
|
92 |
|
93 |
with gr.Group():
|
94 |
+
gr.Markdown("### π Try These Examples")
|
95 |
examples = gr.Examples(
|
96 |
examples=[
|
97 |
+
["ambient synth in a forest", 100, 1.2],
|
98 |
+
["classical piano with raindrops", 70, 0.8],
|
99 |
+
["lo-fi chillhop vibe", 85, 1.0],
|
100 |
+
["orchestral film music", 110, 1.5],
|
101 |
+
["spacey techno beats", 125, 1.3],
|
102 |
],
|
103 |
inputs=[prompt_input, bpm_input, temp_input]
|
104 |
)
|