File size: 646 Bytes
a4ef7e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from transformers import pipeline
import scipy.io.wavfile
import asyncio

# Use a valid text-to-audio model (example: bark)
synthesiser = pipeline("text-to-audio", model="../music")

async def generate_audio(text):
    """Generate audio asynchronously"""
    loop = asyncio.get_event_loop()
    music = await loop.run_in_executor(
        None,
        lambda: synthesiser(text, forward_params={"do_sample": True})
    )
    
    output_file = "melody.wav"
    scipy.io.wavfile.write(output_file, rate=music["sampling_rate"], data=music["audio"])
    print(f"Saved: {output_file}")

# Run async
asyncio.run(generate_audio("pop romance melody"))