openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.107.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/audio.py

38lines · modeblame

9375d2ccStainless Bot2 years ago1#!/usr/bin/env rye run python
e9c26d1eStainless Bot2 years ago2
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
86379b44Stainless Bot2 years ago15with openai.audio.speech.with_streaming_response.create(
16model="tts-1",
17voice="alloy",
18input="the quick brown fox jumped over the lazy dogs",
19) as response:
20response.stream_to_file(speech_file_path)
e9c26d1eStainless Bot2 years ago21
22# Create transcription from audio file
86379b44Stainless Bot2 years ago23transcription = openai.audio.transcriptions.create(
24model="whisper-1",
25file=speech_file_path,
26)
e9c26d1eStainless Bot2 years ago27print(transcription.text)
28
29# Create translation from audio file
30translation = openai.audio.translations.create(
31model="whisper-1",
32file=speech_file_path,
33)
34print(translation.text)
35
36
37if __name__ == "__main__":
38main()