Skip to main content

A Python module for text translation and audio generation using OpenAI's API and ElevenLabs

Project description

Polyglot TTS

A Python module for translating text using OpenAI's API with optional audio generation using ElevenLabs.

Features

  • Text translation to any language using OpenAI's GPT-3.5 model
  • Optional audio generation of translated text using ElevenLabs API
  • Simple, easy-to-use interface
  • Supports environment variable configuration

Installation

pip install polyglot-tts

Configuration

Create a .env file in your project root with your API keys:

OPENAI_API_KEY=your-openai-api-key-here
ELEVENLABS_API_KEY=your-elevenlabs-api-key-here  # Optional, for audio generation

Alternatively, set them as environment variables:

export OPENAI_API_KEY='your-openai-api-key-here'
export ELEVENLABS_API_KEY='your-elevenlabs-api-key-here'  # Optional

Usage

Basic Translation

from polyglot-tts import translate

# Basic translation
translated_text, _ = translate("Hello, how are you?", "Spanish", generate_audio=False)
print(translated_text)  # "¡Hola, cómo estás?"

# Translation with audio generation
translated_text, audio_path = translate("Hello, how are you?", "French")
print(translated_text)  # "Bonjour, comment allez-vous?"
print(audio_path)  # Path to the generated audio file

Function Parameters

  • message (str): The text to translate
  • output_lang (str): The target language for translation
  • generate_audio (bool, optional): Whether to generate audio for the translation. Defaults to True.
  • voice_id (str, optional): The ElevenLabs voice ID to use for audio generation. Defaults to "EXAVITQu4vr4xnSDxMaL" (Bella voice).
  • save_audio (bool, optional): Whether to save the generated audio file. Defaults to False. If True, the audio file will be saved in the "output" directory in the current working directory.

Return Value

Returns a tuple containing:

  1. The translated text (str)
  2. The path to the generated audio file (str or None if audio generation is disabled or fails)

Requirements

  • Python 3.7+
  • OpenAI API key (required)
  • ElevenLabs API key (optional, for audio generation)

Error Handling

The module will raise:

  • ValueError if required API keys are not set
  • Exception if translation or audio generation fails

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polyglot_tts-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polyglot_tts-0.1.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file polyglot_tts-0.1.0.tar.gz.

File metadata

  • Download URL: polyglot_tts-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for polyglot_tts-0.1.0.tar.gz
Algorithm Hash digest
SHA256 856f71c320f9647e8efe343dbc9a8ee9b9ceb9ad3a8cdafd9eb482f103ab9f20
MD5 e398552f7cb3066ddc079b05373b8114
BLAKE2b-256 4fc3ea911db69874faa4606d129e2a038b68fc44f4ad25d6f1e9f70acab096f3

See more details on using hashes here.

File details

Details for the file polyglot_tts-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: polyglot_tts-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for polyglot_tts-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 177dcd9718688636af6e89b2fd5bf80bd7caa72654d3cecd9470658e46e68b92
MD5 6cd978f680c98147bce852192dd354a6
BLAKE2b-256 ae09f4f066a4db0aac4661cd0d59227a48c4364fd4690738e0870c4e5e490a71

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page