推理提供商文件
使用推理提供商構建您的第一個 AI 應用
並獲得增強的文件體驗
開始使用
使用推理提供商構建您的第一個 AI 應用
您已經掌握了基礎知識,並瞭解了提供商生態系統。現在,讓我們構建一個實用的應用程式:一個**AI 會議筆記**應用程式,它可以轉錄音訊檔案並生成包含行動項的摘要。
本專案演示了在單個應用程式中如何使用多個專業提供商進行實際的 AI 編排。
專案概覽
我們的應用程式將:
- 透過網頁介面接受音訊作為麥克風輸入
- 使用快速語音轉文字模型轉錄語音
- 使用強大的語言模型生成摘要
- 部署到網路以便輕鬆共享
技術棧:Gradio(用於UI)+ 推理提供商(用於AI)
步驟 1:設定身份驗證
在開始編碼之前,使用 CLI 進行 Hugging Face 身份驗證
pip install huggingface_hub hf auth login
當提示時,貼上您的 Hugging Face token。這將自動處理所有推理呼叫的身份驗證。您可以從您的設定頁面生成一個。
步驟 2:構建使用者介面
現在讓我們使用 Gradio 建立一個簡單的網頁介面
import gradio as gr
from huggingface_hub import InferenceClient
def process_meeting_audio(audio_file):
"""Process uploaded audio file and return transcript + summary"""
if audio_file is None:
return "Please upload an audio file.", ""
# We'll implement the AI logic next
return "Transcript will appear here...", "Summary will appear here..."
# Create the Gradio interface
app = gr.Interface(
fn=process_meeting_audio,
inputs=gr.Audio(label="Upload Meeting Audio", type="filepath"),
outputs=[
gr.Textbox(label="Transcript", lines=10),
gr.Textbox(label="Summary & Action Items", lines=8)
],
title="🎤 AI Meeting Notes",
description="Upload an audio file to get an instant transcript and summary with action items."
)
if __name__ == "__main__":
app.launch()
在這裡,我們使用 Gradio 的 `gr.Audio` 元件來上傳音訊檔案或使用麥克風輸入。我們簡化了輸出:一個是轉錄文字,另一個是包含行動項的摘要。
步驟 3:新增語音轉錄
現在,讓我們使用 OpenAI 的 `whisper-large-v3` 模型來實現快速可靠的語音處理。
我們將使用 `auto` 提供商自動為模型選擇第一個可用的提供商。您可以在推理提供商頁面中定義自己的提供商優先順序列表。
def transcribe_audio(audio_file_path):
"""Transcribe audio using fal.ai for speed"""
client = InferenceClient(provider="auto")
# Pass the file path directly - the client handles file reading
transcript = client.automatic_speech_recognition(
audio=audio_file_path,
model="openai/whisper-large-v3"
)
return transcript.text
步驟 4:新增 AI 摘要
接下來,我們將透過推理提供商使用一個強大的語言模型,例如 DeepSeek 的 `deepseek-ai/DeepSeek-R1-0528`,就像上一步一樣,我們將使用 `auto` 提供商自動為模型選擇第一個可用的提供商。我們將定義一個自定義提示,以確保輸出格式為包含行動項和已做決定的摘要。
def generate_summary(transcript):
"""Generate summary using an Inference Provider"""
client = InferenceClient(provider="auto")
prompt = f"""
Analyze this meeting transcript and provide:
1. A concise summary of key points
2. Action items with responsible parties
3. Important decisions made
Transcript: {transcript}
Format with clear sections:
## Summary
## Action Items
## Decisions Made
"""
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-0528",
messages=[{"role": "user", "content": prompt}],
max_tokens=1000
)
return response.choices[0].message.content
步驟 5:部署到 Hugging Face Spaces
要部署,我們需要建立一個 `app.py` 檔案並將其上傳到 Hugging Face Spaces。
📋 點選檢視完整的 app.py 檔案
import gradio as gr
from huggingface_hub import InferenceClient
def transcribe_audio(audio_file_path):
"""Transcribe audio using an Inference Provider"""
client = InferenceClient(provider="auto")
# Pass the file path directly - the client handles file reading
transcript = client.automatic_speech_recognition(
audio=audio_file_path, model="openai/whisper-large-v3"
)
return transcript.text
def generate_summary(transcript):
"""Generate summary using an Inference Provider"""
client = InferenceClient(provider="auto")
prompt = f"""
Analyze this meeting transcript and provide:
1. A concise summary of key points
2. Action items with responsible parties
3. Important decisions made
Transcript: {transcript}
Format with clear sections:
## Summary
## Action Items
## Decisions Made
"""
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1-0528",
messages=[{"role": "user", "content": prompt}],
max_tokens=1000,
)
return response.choices[0].message.content
def process_meeting_audio(audio_file):
"""Main processing function"""
if audio_file is None:
return "Please upload an audio file.", ""
try:
# Step 1: Transcribe
transcript = transcribe_audio(audio_file)
# Step 2: Summarize
summary = generate_summary(transcript)
return transcript, summary
except Exception as e:
return f"Error processing audio: {str(e)}", ""
# Create Gradio interface
app = gr.Interface(
fn=process_meeting_audio,
inputs=gr.Audio(label="Upload Meeting Audio", type="filepath"),
outputs=[
gr.Textbox(label="Transcript", lines=10),
gr.Textbox(label="Summary & Action Items", lines=8),
],
title="🎤 AI Meeting Notes",
description="Upload audio to get instant transcripts and summaries.",
)
if __name__ == "__main__":
app.launch()
我們的應用程式將在埠 7860 上執行,並且看起來像這樣
要部署,我們需要建立一個新的 Space 並上傳我們的檔案。
- 建立一個新空間:前往 huggingface.co/new-space
- 選擇 Gradio SDK 並將其設定為公開
- 上傳檔案:上傳 `app.py`
- 新增您的 token:在 Space 設定中,將 `HF_TOKEN` 新增為秘密(從您的設定中獲取)
- 啟動:您的應用程式將在
https://huggingface.co/spaces/your-username/your-space-name
上上線
注意:雖然我們在本地使用了 CLI 身份驗證,但 Spaces 要求將 token 作為秘密用於部署環境。
後續步驟
恭喜!您已經建立了一個生產就緒的 AI 應用程式,它能夠:處理實際任務,提供專業介面,自動擴充套件,並有效地控制成本。如果您想探索更多提供商,可以檢視推理提供商頁面。或者,這裡有一些後續步驟的建議:
- 改進您的提示:嘗試不同的提示以提高用例的質量
- 嘗試不同的模型:試驗各種語音和文字模型
- 比較效能:在不同提供商之間比較速度與準確性