推理提供商文档

使用推理提供商构建您的第一个 AI 应用

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

使用推理提供商构建您的第一个 AI 应用

您已经掌握了基础知识,并了解了提供商生态系统。现在,让我们构建一个实用的应用程序:一个**AI 会议笔记**应用程序,它可以转录音频文件并生成包含行动项的摘要。

本项目演示了在单个应用程序中如何使用多个专业提供商进行实际的 AI 编排。

项目概览

我们的应用程序将:

  1. 通过网页界面接受音频作为麦克风输入
  2. 使用快速语音转文本模型转录语音
  3. 使用强大的语言模型生成摘要
  4. 部署到网络以便轻松共享
python
javascript

技术栈:Gradio(用于UI)+ 推理提供商(用于AI)

步骤 1:设置身份验证

python
javascript

在开始编码之前,使用 CLI 进行 Hugging Face 身份验证

pip install huggingface_hub
hf auth login

当提示时,粘贴您的 Hugging Face token。这将自动处理所有推理调用的身份验证。您可以从您的设置页面生成一个。

步骤 2:构建用户界面

python
javascript

现在让我们使用 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:添加语音转录

python
javascript

现在,让我们使用 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 摘要

python
javascript

接下来,我们将通过推理提供商使用一个强大的语言模型,例如 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

python
javascript

要部署,我们需要创建一个 `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 上运行,并且看起来像这样

Gradio app

要部署,我们需要创建一个新的 Space 并上传我们的文件。

  1. 创建一个新空间:前往 huggingface.co/new-space
  2. 选择 Gradio SDK 并将其设置为公开
  3. 上传文件:上传 `app.py`
  4. 添加您的 token:在 Space 设置中,将 `HF_TOKEN` 添加为秘密(从您的设置中获取)
  5. 启动:您的应用程序将在 https://huggingface.co/spaces/your-username/your-space-name 上上线

注意:虽然我们在本地使用了 CLI 身份验证,但 Spaces 要求将 token 作为秘密用于部署环境。

后续步骤

恭喜!您已经创建了一个生产就绪的 AI 应用程序,它能够:处理实际任务,提供专业界面,自动扩展,并有效地控制成本。如果您想探索更多提供商,可以查看推理提供商页面。或者,这里有一些后续步骤的建议:

  • 改进您的提示:尝试不同的提示以提高用例的质量
  • 尝试不同的模型:试验各种语音和文本模型
  • 比较性能:在不同提供商之间比较速度与准确性
< > 在 GitHub 上更新