audio.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """An example showing how to use aphrodite to serve VLMs.
  2. Launch the aphrodite server with the following command:
  3. aphrodite serve fixie-ai/ultravox-v0_3
  4. """
  5. import base64
  6. import os
  7. from openai import OpenAI
  8. # Get path to the audio file in ../audio directory
  9. audio_path = os.path.join(
  10. os.path.dirname(os.path.realpath(__file__)),
  11. "..",
  12. "audio",
  13. "mary_had_lamb.ogg",
  14. )
  15. # Modify OpenAI's API key and API base to use aphrodite's API server.
  16. openai_api_key = "EMPTY"
  17. openai_api_base = "http://localhost:2242/v1"
  18. client = OpenAI(
  19. # defaults to os.environ.get("OPENAI_API_KEY")
  20. api_key=openai_api_key,
  21. base_url=openai_api_base,
  22. )
  23. models = client.models.list()
  24. model = models.data[0].id
  25. def encode_audio_base64_from_file(file_path: str) -> str:
  26. """Encode an audio file to base64 format."""
  27. with open(file_path, "rb") as f:
  28. return base64.b64encode(f.read()).decode("utf-8")
  29. # Use base64 encoded audio in the payload
  30. audio_base64 = encode_audio_base64_from_file(audio_path)
  31. chat_completion = client.chat.completions.create(
  32. messages=[
  33. {
  34. "role": "user",
  35. "content": [
  36. {"type": "text", "text": "What's in this audio?"},
  37. {
  38. "type": "audio_url",
  39. "audio_url": {
  40. "url": f"data:audio/ogg;base64,{audio_base64}"
  41. },
  42. },
  43. ],
  44. } # type: ignore
  45. ],
  46. model=model,
  47. max_tokens=128,
  48. )
  49. result = chat_completion.choices[0].message.content
  50. print(f"Chat completion output: {result}")