Add audio example usage

This commit is contained in:
hlohaus 2025-03-21 05:13:59 +01:00
parent c97ba0c88e
commit 705ad02954
5 changed files with 46 additions and 5 deletions

28
etc/examples/audio.py Normal file
View file

@ -0,0 +1,28 @@
import asyncio
from g4f.client import AsyncClient
import g4f.Provider
import g4f.models
async def main():
client = AsyncClient(provider=g4f.Provider.PollinationsAI)
# Generate audio with PollinationsAI
response = await client.chat.completions.create(
model="openai-audio",
messages=[{"role": "user", "content": "Say good day to the world"}],
audio={ "voice": "alloy", "format": "mp3" },
)
response.choices[0].message.save("alloy.mp3")
# Transcribe a audio file
with open("audio.wav", "rb") as audio_file:
response = await client.chat.completions.create(
messages="Transcribe this audio",
provider=g4f.Provider.Microsoft_Phi_4,
media=[[audio_file, "audio.wav"]],
modalities=["text"],
)
print(response.choices[0].message.content)
if __name__ == "__main__":
asyncio.run(main())