Unverified Commit e384f2f1 authored by Reid's avatar Reid Committed by GitHub
Browse files

[Misc] refactor example - openai_transcription_client (#19851)


Signed-off-by: default avatarreidliu41 <reid201711@gmail.com>
Co-authored-by: default avatarreidliu41 <reid201711@gmail.com>
parent 089a306f
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""
This script demonstrates how to use the vLLM API server to perform audio
transcription with the `openai/whisper-large-v3` model.
Before running this script, you must start the vLLM server with the following command:
vllm serve openai/whisper-large-v3
Requirements:
- vLLM with audio support
- openai Python SDK
- httpx for streaming support
The script performs:
1. Synchronous transcription using OpenAI-compatible API.
2. Streaming transcription using raw HTTP request to the vLLM server.
"""
import asyncio import asyncio
import json import json
...@@ -21,6 +39,9 @@ client = OpenAI( ...@@ -21,6 +39,9 @@ client = OpenAI(
def sync_openai(): def sync_openai():
"""
Perform synchronous transcription using OpenAI-compatible API.
"""
with open(str(mary_had_lamb), "rb") as f: with open(str(mary_had_lamb), "rb") as f:
transcription = client.audio.transcriptions.create( transcription = client.audio.transcriptions.create(
file=f, file=f,
...@@ -37,11 +58,11 @@ def sync_openai(): ...@@ -37,11 +58,11 @@ def sync_openai():
print("transcription result:", transcription.text) print("transcription result:", transcription.text)
sync_openai()
# OpenAI Transcription API client does not support streaming. # OpenAI Transcription API client does not support streaming.
async def stream_openai_response(): async def stream_openai_response():
"""
Perform streaming transcription using vLLM's raw HTTP streaming API.
"""
data = { data = {
"language": "en", "language": "en",
"stream": True, "stream": True,
...@@ -68,7 +89,15 @@ async def stream_openai_response(): ...@@ -68,7 +89,15 @@ async def stream_openai_response():
# Extract and print the content # Extract and print the content
content = chunk["choices"][0].get("delta", {}).get("content") content = chunk["choices"][0].get("delta", {}).get("content")
print(content, end="") print(content, end="")
print() # Final newline after stream ends
def main():
sync_openai()
# Run the asynchronous function
asyncio.run(stream_openai_response())
# Run the asynchronous function if __name__ == "__main__":
asyncio.run(stream_openai_response()) main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment