openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
examples/audio.py
34lines · modecode
| 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( |
| 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 | |
| 33 | if __name__ == "__main__": |
| 34 | main() |