audio.py 907 B

1234567891011121314151617181920212223242526272829
  1. import asyncio
  2. from g4f.client import AsyncClient
  3. import g4f.Provider
  4. import g4f.models
  5. async def main():
  6. client = AsyncClient(provider=g4f.Provider.PollinationsAI)
  7. # Generate audio with PollinationsAI
  8. response = await client.chat.completions.create(
  9. model="openai-audio",
  10. messages=[{"role": "user", "content": "Say good day to the world"}],
  11. audio={ "voice": "alloy", "format": "mp3" },
  12. )
  13. response.choices[0].message.save("alloy.mp3")
  14. # Transcribe a audio file
  15. with open("audio.wav", "rb") as audio_file:
  16. response = await client.chat.completions.create(
  17. messages="Transcribe this audio",
  18. provider=g4f.Provider.Microsoft_Phi_4,
  19. media=[[audio_file, "audio.wav"]],
  20. modalities=["text"],
  21. )
  22. print(response.choices[0].message.content)
  23. if __name__ == "__main__":
  24. asyncio.run(main())