openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.7.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/audio.py

34lines · modeblame

e9c26d1eStainless Bot2 years ago1#!/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
15response = openai.audio.speech.create(
2d85e1e7Stainless Bot2 years ago16model="tts-1", voice="alloy", input="the quick brown fox jumped over the lazy dogs"
e9c26d1eStainless Bot2 years ago17)
18
19response.stream_to_file(speech_file_path)
20
21# Create transcription from audio file
22transcription = openai.audio.transcriptions.create(model="whisper-1", file=speech_file_path)
23print(transcription.text)
24
25# Create translation from audio file
26translation = openai.audio.translations.create(
27model="whisper-1",
28file=speech_file_path,
29)
30print(translation.text)
31
32
33if __name__ == "__main__":
34main()