AI 日报

OpenAI Whisper 教程:使用 GPT-3 更新我们的 Whisper API




OpenAI Whisper 教程:使用 GPT-3 更新我们的 Whisper API

什么是耳语?

Whisper 是 OpenAI 的一种自动最先进的语音识别系统,它已经接受了 680,000 小时的多语言和多任务监督数据的培训,这些数据是从网络上收集的。这个庞大而多样化的数据集提高了对口音、背景噪音和技术语言的鲁棒性。此外,它还支持多种语言的转录,以及将这些语言翻译成英语。OpenAI 发布了模型和代码,作为构建利用语音识别的有用应用程序的基础。

什么是 GPT-3?

GPT-3 是 OpenAI 的一种语言模型,可以生成文本。它是在来自网络的大型文本数据集上进行训练的。

如何开始?

本教程是之前教程 Whisper API with Flask and Docker 的升级。

所以请先检查一下 🙂 如果您已经检查过了,您可以在此处继续。

OpenAI API 密钥

如果您还没有,请转到 OpenAI 并创建一个帐户。并创建您的 API 密钥。切勿在公共存储库中共享您的 API 密钥!

更新 requirement.txt

我们正在将 openai 包添加到我们的文件中

为 gpt3 函数创建文件

我们将创建一个名为 gpt3.py 的新文件,并向其中添加以下代码。在提示中我使用了摘要选项来总结文本,但你可以使用任何你想要的。您也可以调整参数。

import openaidef gpt3complete(speech):    # Completion function call engine: text-davinci-002    Platformresponse = openai.Completion.create(        engine="text-davinci-002",        prompt="Write a short summary of this text: {}".format(speech),        temperature=0.7,        max_tokens=1500,        top_p=1,        frequency_penalty=0,        presence_penalty=0,        )    return Platformresponse.choices[0].text

更新 app.py

在顶部,我们将更新我们的导入。请插入您之前创建的 API 密钥,而不是“MY_API_KEY”。

import openaiimport gpt3# GPT-3 API Keyopenai.api_key = "MY_API_KEY"

更新 /whisper 路由

我们会将我们新的 GPT3 功能集成到路由中。因此,当我们从 whisper 获得结果时,我们会将其传递给 gpt3 函数并返回结果。

@app.route('/whisper', methods=['POST'])def handler():    if not request.files:        # If the user didn't submit any files, return a 400 (Bad Request) error.        abort(400)    # For each file, let's store the results in a list of dictionaries.    results = []    # Loop over every file that the user submitted.    for filename, handle in request.files.items():        # Create a temporary file.        # The location of the temporary file is available in `temp.name`.        temp = NamedTemporaryFile()        # Write the user's uploaded file to the temporary file.        # The file will get deleted when it drops out of scope.        handle.save(temp)        # Let's get the transcript of the temporary file.        result = model.transcribe(temp.name)        text = result['text']        # Let's get the summary of the soundfile        summary = gpt3.gpt3complete(text)        # Now we can store the result object for this file.        results.append({            'filename': filename,            'transcript': text.strip(),            'summary': summary.strip(),        })    # This will be automatically converted to JSON.    return {'results': results}

如何运行容器?

打开终端并导航到您创建文件的文件夹。运行以下命令来构建容器:

docker build -t whisper-api .

构建就绪后,运行以下命令来运行容器:

docker run -p 5000:5000 whisper-api

如何测试API?

http://localhost:5000/whisper您可以通过向其中包含文件的路由发送 POST 请求来测试 API 。Body 应为 form-data。您可以使用以下 curl 命令测试 API:

curl -F "file=@/path/to/file" http://localhost:5000/whisper

结果,您应该得到一个 JSON 对象,其中包含成绩单和摘要。

如何部署API?

这个 API 可以部署在任何可以使用 Docker 的地方。请记住,此设置当前使用 CPU 处理音频文件。如果您想使用 GPU,则需要更改 Dockerfile 并共享 GPU。我不会深入介绍,因为这是一个介绍。Docker GPU

你可以在这里找到整个代码

谢谢你!– AI未来百科 ; 探索AI的边界与未来! 懂您的AI未来站