openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
examples/audio.py
34lines · modeblame
e9c26d1eStainless Bot2 years ago | 1 | #!/usr/bin/env python |
| 2 | | |
| 3 | from pathlib import Path | |
| 4 | | |
| 5 | from openai import OpenAI | |
| 6 | | |
| 7 | # gets OPENAI_API_KEY from your environment variables | |
| 8 | openai = OpenAI() | |
| 9 | | |
| 10 | speech_file_path = Path(__file__).parent / "speech.mp3" | |
| 11 | | |
| 12 | | |
| 13 | def main() -> None: | |
| 14 | # Create text-to-speech audio file | |
| 15 | response = openai.audio.speech.create( | |
2d85e1e7Stainless Bot2 years ago | 16 | model="tts-1", voice="alloy", input="the quick brown fox jumped over the lazy dogs" |
e9c26d1eStainless Bot2 years ago | 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 | | |
| 33 | if __name__ == "__main__": | |
| 34 | main() |