InferenceLab commited on
Commit
31fce29
Β·
verified Β·
1 Parent(s): a634dfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
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 asyncio (required for Gradio + asyncio)
11
  nest_asyncio.apply()
12
 
13
- # Configure Gemini API
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
- # Save PCM audio to WAV file
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 function
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) # Streaming duration
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
- # Convert PCM to numpy array for audio playback
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
- # Wrapper for Gradio
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("### πŸŽ› Input")
76
  with gr.Row():
77
  prompt_input = gr.Textbox(
78
  label="Music Style / Prompt",
79
- placeholder="e.g., ambient synth, minimal techno, classical piano"
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("### 🎡 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("### πŸ” Examples")
95
  examples = gr.Examples(
96
  examples=[
97
- ["minimal techno", 125, 1.0],
98
- ["classical piano in a rainy mood", 70, 0.9],
99
- ["ambient space drone", 90, 1.5],
100
- ["lo-fi chill beats", 80, 1.0],
101
- ["orchestral epic music", 110, 1.2],
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
  )