InferenceLab commited on
Commit
aaabe15
·
verified ·
1 Parent(s): cf1ec2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -63
app.py CHANGED
@@ -1,64 +1,105 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()