openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e9c26d1e253ff54ea50ea1b11d87c35405b24dda

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/audio.py

34lines · modecode

1#!/usr/bin/env python
2
3from pathlib import Path
4
5from openai import OpenAI
6
7# gets OPENAI_API_KEY from your environment variables
8openai = OpenAI()
9
10speech_file_path = Path(__file__).parent / "speech.mp3"
11
12
13def main() -> None:
14 # Create text-to-speech audio file
15 response = openai.audio.speech.create(
16 model="tts-1", voice="alloy", input="the quick brown fox jumped over the lazy dogs"
17 )
18
19 response.stream_to_file(speech_file_path)
20
21 # Create transcription from audio file
22 transcription = openai.audio.transcriptions.create(model="whisper-1", file=speech_file_path)
23 print(transcription.text)
24
25 # Create translation from audio file
26 translation = openai.audio.translations.create(
27 model="whisper-1",
28 file=speech_file_path,
29 )
30 print(translation.text)
31
32
33if __name__ == "__main__":
34 main()