推理提供商文档

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

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

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

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

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

项目概览

我们的应用程序将

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

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

第 1 步:设置身份验证

python
javascript

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

pip install huggingface_hub
hf auth login

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

第 2 步:构建用户界面

python
javascript

现在让我们使用 Gradio 创建一个简单的 Web 界面

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 提供商自动选择该模型的第一个可用提供商。我们还将使用 :fastest 策略,以确保我们为该模型选择性能最佳的提供商。我们将定义一个自定义提示,以确保输出格式为摘要,并包含行动项和已做出的决策。

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:fastest",
        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:fastest",
        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. 添加你的令牌:在 Space 设置中,将 HF_TOKEN 添加为密钥(从你的设置获取)
  5. 启动:您的应用程序将在 https://huggingface.co/spaces/your-username/your-space-name 上上线

注意:虽然我们在本地使用了 CLI 进行身份验证,但 Spaces 需要将令牌作为密钥用于部署环境。

后续步骤

恭喜!你已经创建了一个生产级别的 AI 应用程序,它:处理实际任务,提供专业的界面,自动扩展,并且成本效益高。如果你想探索更多提供商,可以查看推理提供商页面。或者,这里有一些关于下一步的建议

  • 改进你的提示:尝试不同的提示来提高针对你用例的质量
  • 尝试不同的模型:尝试各种语音和文本模型
  • 比较性能:对不同提供商的速度与准确性进行基准测试
在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.