開始使用免費開始

傳遞關鍵字參數

在這個練習中,你會學到如何在 pipeline 中呼叫模型函式時,傳遞關鍵字參數。

你將使用 Meta 推出的 MusicGen 小型模型,它能根據文字描述或音訊提示產生音樂片段。

pipeline 模組已載入,且 soundfile 函式庫可用並命名為 sf

本練習屬於課程

使用 Hugging Face 的多模態模型

檢視課程

練習說明

  • 使用 PyTorch 架構載入一個 text-to-audio pipeline,模型為 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"])
編輯並執行程式碼