ElevenLabs Text-to-Speech components for Haystack.
Project description
ElevenLabs Haystack Integration
This repository contains an integration of ElevenLabs' Text-to-Speech API with Haystack pipelines. This package allows you to convert text to speech using ElevenLabs' API and optionally save the generated audio to AWS S3.
Installation
-
Install:
pip install elevenlabs_haystack
-
Set up environment variables for sensitive credentials.
Create a
.env
file in the root directory with the following content (replace with your actual credentials):ELEVENLABS_API_KEY=sk_your_elevenlabs_api_key_here AWS_ACCESS_KEY_ID=your_aws_access_key_id AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key AWS_REGION_NAME=us-east-1 AWS_S3_BUCKET_NAME=your_s3_bucket_name
These variables will be automatically loaded using
dotenv
and used to access ElevenLabs and AWS services securely.
Usage
Basic Text-to-Speech Example
This example shows how to use the ElevenLabsTextToSpeech
component to convert text to speech and save the generated audio file locally or in an AWS S3 bucket. It uses environment variables to access sensitive credentials.
from haystack.utils import Secret
from elevenlabs_haystack import ElevenLabsTextToSpeech
# Initialize the ElevenLabsTextToSpeech component using environment variables for sensitive data
tts = ElevenLabsTextToSpeech(
elevenlabs_api_key=Secret.from_env_var("ELEVENLABS_API_KEY"),
output_folder="audio_files", # Save the generated audio locally
voice_id="K8lgMMdmFr7QoEooafEf", # ElevenLabs voice ID
aws_s3_bucket_name=Secret.from_env_var("AWS_S3_BUCKET_NAME"), # S3 bucket for optional upload
aws_s3_output_folder="s3_files", # Save the generated audio to AWS S3
aws_access_key_id=Secret.from_env_var("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=Secret.from_env_var("AWS_SECRET_ACCESS_KEY"),
aws_region_name=Secret.from_env_var("AWS_REGION_NAME"), # AWS region
voice_settings={
"stability": 0.75,
"similarity_boost": 0.75,
"style": 0.5,
"use_speaker_boost": True, # Optional voice settings
},
)
# Run the text-to-speech conversion
result = tts.run("Hello, world!")
# Print the result
print(result)
"""
{
"id": "elevenlabs-id",
"file_name": "audio_files/elevenlabs-id.mp3",
"s3_file_name": "s3_files/elevenlabs-id.mp3",
"s3_bucket_name": "test-bucket",
"s3_presigned_url": "https://test-bucket.s3.amazonaws.com/s3_files/elevenlabs-id.mp3"
}
"""
Example Using Haystack Pipeline
This example demonstrates how to integrate the ElevenLabsTextToSpeech
component into a Haystack pipeline. Additionally, we define a WelcomeTextGenerator
component that generates a personalized welcome message.
from haystack import component, Pipeline
from haystack.utils import Secret
from elevenlabs_haystack import ElevenLabsTextToSpeech
# Define a simple component to generate a welcome message
@component
class WelcomeTextGenerator:
"""
A component generating a personal welcome message and making it upper case.
"""
@component.output_types(welcome_text=str, note=str)
def run(self, name: str):
return {
"welcome_text": f'Hello {name}, welcome to Haystack!'.upper(),
"note": "welcome message is ready"
}
# Create a Pipeline
text_pipeline = Pipeline()
# Add WelcomeTextGenerator to the Pipeline
text_pipeline.add_component(
name="welcome_text_generator",
instance=WelcomeTextGenerator()
)
# Add ElevenLabsTextToSpeech to the Pipeline using environment variables
text_pipeline.add_component(
name="tts",
instance=ElevenLabsTextToSpeech(
elevenlabs_api_key=Secret.from_env_var("ELEVENLABS_API_KEY"),
output_folder="audio_files", # Save the generated audio locally
voice_id="K8lgMMdmFr7QoEooafEf", # ElevenLabs voice ID
aws_s3_bucket_name=Secret.from_env_var("AWS_S3_BUCKET_NAME"), # S3 bucket for optional upload
aws_s3_output_folder="s3_files", # Save the generated audio to AWS S3
aws_access_key_id=Secret.from_env_var("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=Secret.from_env_var("AWS_SECRET_ACCESS_KEY"),
aws_region_name=Secret.from_env_var("AWS_REGION_NAME"), # Load region from env
voice_settings={
"stability": 0.75,
"similarity_boost": 0.75,
"style": 0.5,
"use_speaker_boost": True, # Optional voice settings
},
),
)
# Connect the output of WelcomeTextGenerator to the input of ElevenLabsTextToSpeech
text_pipeline.connect(sender="welcome_text_generator.welcome_text", receiver="tts")
# Run the pipeline with a sample name
result = text_pipeline.run({"welcome_text_generator": {"name": "Bilge"}})
# Print the result
print(result)
"""
{
"id": "elevenlabs-id",
"file_name": "audio_files/elevenlabs-id.mp3",
"s3_file_name": "s3_files/elevenlabs-id.mp3",
"s3_bucket_name": "test-bucket",
"s3_presigned_url": "https://test-bucket.s3.amazonaws.com/s3_files/elevenlabs-id.mp3"
}
"""
Example .env
File
Here's an example .env
file containing environment variables:
ELEVENLABS_API_KEY=sk_your_elevenlabs_api_key_here
AWS_ACCESS_KEY_ID=your_aws_access_key_id
AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
AWS_REGION_NAME=us-east-1
AWS_S3_BUCKET_NAME=your_s3_bucket_name
Running the Examples
To run the examples:
-
Ensure you have set up your environment variables in the
.env
file as shown above. -
Run your Python script:
poetry run python your_script.py
License
This project is licensed under the 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
Built Distribution
Hashes for elevenlabs_haystack-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffe4b95dafc5f7b26e8bd281ed41a05a5c2a229f733fe66b00b02b4fdc3c3c6f |
|
MD5 | b74b96d62901326f9fcf5c29543f0063 |
|
BLAKE2b-256 | 87c14cc9b9958a25357ed31078945e769363d1c0f972ac6c26169e7164efa010 |
Hashes for elevenlabs_haystack-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aa4438c3ec0ede7c81ef0a702d61f54e00f991b6e20ecf044d5949e20c01101 |
|
MD5 | 71788c19a804ec103305fdba9ae1c0cc |
|
BLAKE2b-256 | 73a664d34f89f8b37d737ad8a5b2e5acf3cbec6677f1a59ae5e754db858304a3 |