Back to Chattts

OpenAI API test (non-streaming)

openai_api.ipynb

0.2.54.4 KB
Original Source
python
# OpenAI API test (non-streaming)
from openai import OpenAI
from IPython.display import Audio, display

# Initialize the client
client = OpenAI(api_key="dummy-key", base_url="http://localhost:8000/v1")

# Generate audio
response = client.audio.speech.create(
    model="tts-1",
    voice="echo",
    input="""
    以下是一些中英文对照的话语。 
    1. 早上好!希望你有美好的一天。Good morning! Wish you a wonderful day. 
    2. 你好呀,最近怎么样?Hello there, how have you been recently? 
    3. 别放弃,你能做到的!Don't give up, you can do it! 
    4. 继续努力,你的付出会有回报的。Keep up the good work, your efforts will pay off.
    """,
    response_format="wav",
)

# Get audio binary data
audio_data = response.content  # response.content is of type bytes

# Display and play in the Notebook
display(Audio(audio_data, autoplay=False))
python
# Test using the requests module, streaming mode
import requests
from IPython.display import Audio, display
import io

payload = {
    "model": "tts-1",
    "input": """
    以下是一些中英文对照的话语。 
    1. 早上好!希望你有美好的一天。Good morning! Wish you a wonderful day. 
    2. 你好呀,最近怎么样?Hello there, how have you been recently? 
    3. 别放弃,你能做到的!Don't give up, you can do it! 
    4. 继续努力,你的付出会有回报的。Keep up the good work, your efforts will pay off.
    """,
    "voice": "echo",
    "response_format": "wav",
    "stream": True,
}

try:
    response = requests.post(
        "http://localhost:8000/v1/audio/speech", json=payload, stream=True
    )
    response.raise_for_status()  # Check the status code

    audio_buffer = io.BytesIO()
    for chunk in response.iter_content(chunk_size=8192):
        if chunk:
            audio_buffer.write(chunk)

    audio_buffer.seek(0)
    display(Audio(audio_buffer.getvalue(), autoplay=False))
    print("Audio has been loaded into the Notebook and can be played manually")
except requests.exceptions.RequestException as e:
    print(f"Request failed: {str(e)}")
    if hasattr(e.response, "text"):
        print(f"Error details: {e.response.text}")
python
import subprocess

# Use pipeline to implement streaming playback, WAV format
cmd = (
    'curl -X POST "http://localhost:8000/v1/audio/speech" '
    '-H "Content-Type: application/json" '
    '-d \'{"model": "tts-1", "input": "以下是一些中英文对照的话语。 1. 早上好!希望你有美好的一天。Good morning! Wish you a wonderful day. 2. 你好呀,最近怎么样?Hello there, how have you been recently? 3. 别放弃,你能做到的!Dont give up, you can do it! 4. 继续努力,你的付出会有回报的。Keep up the good work, your efforts will pay off.", "voice": "echo", "response_format": "wav", "stream": true}\' '
    "-s | mpv --no-video -"
)
subprocess.run(cmd, shell=True, check=True)
python
import subprocess

# Use pipeline to implement streaming playback, MP3 format
cmd = (
    'curl -X POST "http://localhost:8000/v1/audio/speech" '
    '-H "Content-Type: application/json" '
    '-d \'{"model": "tts-1", "input": "以下是一些中英文对照的话语。 1. 早上好!希望你有美好的一天。Good morning! Wish you a wonderful day. 2. 你好呀,最近怎么样?Hello there, how have you been recently? 3. 别放弃,你能做到的!Dont give up, you can do it! 4. 继续努力,你的付出会有回报的。Keep up the good work, your efforts will pay off.", "voice": "echo", "response_format": "mp3", "stream": true}\' '
    "-s | mpv --no-video -"
)
subprocess.run(cmd, shell=True, check=True)
python
import subprocess

# Use pipeline to implement streaming playback, OGG format
cmd = (
    'curl -X POST "http://localhost:8000/v1/audio/speech" '
    '-H "Content-Type: application/json" '
    '-d \'{"model": "tts-1", "input": "以下是一些中英文对照的话语。 1. 早上好!希望你有美好的一天。Good morning! Wish you a wonderful day. 2. 你好呀,最近怎么样?Hello there, how have you been recently? 3. 别放弃,你能做到的!Dont give up, you can do it! 4. 继续努力,你的付出会有回报的。Keep up the good work, your efforts will pay off.", "voice": "echo", "response_format": "ogg", "stream": true}\' '
    "-s | mpv --no-video -"
)
subprocess.run(cmd, shell=True, check=True)