0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure OpenAI Whisper API を Python から2通りの方法で叩く

Last updated at Posted at 2024-08-01

はじめに

どうしてこんなチュートリアルみたい記事を...!?
↓ 公式の curl コマンドが動かないから...!!

事前準備

.env に以下のようなファイルを作ってください

AZURE_OPENAI_ENDPOINT="https://*****.openai.azure.com/"
AZURE_OPENAI_API_KEY="*****"

requests を使う方法

import requests
import os
from dotenv import load_dotenv

load_dotenv()

deployment_id = os.getenv("AZURE_DEPLOYMENT_ID", "whisper")

url = os.getenv("AZURE_OPENAI_ENDPOINT") + f"/openai/deployments/{deployment_id}/audio/transcriptions?api-version=2023-09-01-preview"

FILE="*****.mp3"
with open(FILE, 'rb') as f:
    file = {'file': (FILE, f)}
    response = requests.post(
      url, 
      headers={
        "api-key": os.getenv("AZURE_OPENAI_API_KEY"),
      }, 
      files=file
    )

print(response.json())

openai を使う方法

import openai
import os
from dotenv import load_dotenv

load_dotenv()

deployment_id = os.getenv("AZURE_DEPLOYMENT_ID", "whisper")

client = openai.AzureOpenAI(
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    api_version=os.getenv("OPENAI_API_VERSION", "2023-09-01-preview")
)

FILE="*****.m4a"
with open(FILE, "rb") as audio_file:
    transcript = client.audio.transcriptions.create(
        model=deployment_id,
        file=audio_file
    )
    print(transcript)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?