傳遞關鍵字參數
在這個練習中,你會學到如何在 pipeline 中呼叫模型函式時,傳遞關鍵字參數。
你將使用 Meta 推出的 MusicGen 小型模型,它能根據文字描述或音訊提示產生音樂片段。
pipeline 模組已載入,且 soundfile 函式庫可用並命名為 sf。
本練習屬於課程
使用 Hugging Face 的多模態模型
練習說明
- 使用 PyTorch 架構載入一個
text-to-audiopipeline,模型為facebook/musicgen-small。 - 建立名為
generate_kwargs的字典,將產生溫度設為0.8,並將max_new_tokens設為1。 - 使用
"Classic rock riff"提示字串生成音訊陣列,並以你的generate_kwargs字典調整生成參數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load a text-to-audio pipeline
musicgen = pipeline(task="____", model="____", framework="pt")
# Make a dictionary to set the generation temperature to 0.8 and max_new_tokens to 1
generate_kwargs = {"____": ____, "____": ____}
# Generate an audio array passing the arguments
outputs = ____("____", ____=____)
sf.write("output.wav", outputs["audio"][0][0], outputs["sampling_rate"])