elevenlabs-haystack


Nameelevenlabs-haystack JSON
Version 0.4.5 PyPI version JSON
download
home_pageNone
SummaryElevenLabs Text-to-Speech components for Haystack.
upload_time2024-10-17 19:00:37
maintainerNone
docs_urlNone
authorAndy
requires_python<4.0,>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install elevenlabs_haystack
```

## Setting Up API Keys

#### **ElevenLabs API Key**

To access the ElevenLabs API, you need to create an account and obtain an API key.

1. Go to the [ElevenLabs](https://elevenlabs.ai/) website and sign up for an account.
2. Once logged in, navigate to the **Profile** section.
3. In the **API** section, generate a new API key.
4. Copy the API key.

#### **AWS Credentials**

To store generated audio files on AWS S3, you need AWS credentials (Access Key ID, Secret Access Key) and specify a region.

1. If you don’t have an AWS account, sign up at [AWS](https://aws.amazon.com/).
2. Create a new IAM user and assign the necessary permissions to allow the user to upload files to S3. The `AmazonS3FullAccess` policy is sufficient for this example.
3. Once the IAM user is created, download or note the **AWS Access Key ID** and **Secret Access Key**.
4. Identify the **AWS Region** where your S3 bucket resides (e.g., `us-east-1`). This information can be found in the AWS Management Console.
5. Finally, create or identify the S3 bucket where the generated audio files will be saved.

Create a `.env` file in the root directory with the following content (replace with your actual credentials):

```bash
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.

```python
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="Xb7hH8MSUJpSbSDYk0k2",  # ElevenLabs voice ID (Alice)
    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.

```python
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="Xb7hH8MSUJpSbSDYk0k2",  # ElevenLabs voice ID (Alice)
        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"
}
"""
```

## License

This project is licensed under the MIT License.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "elevenlabs-haystack",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Andy",
    "author_email": "andychert@protonmail.com",
    "download_url": "https://files.pythonhosted.org/packages/3d/6e/b23630610777f3ae490d0d066d937d5831abd263f68aa9a5ba8906e245c5/elevenlabs_haystack-0.4.5.tar.gz",
    "platform": null,
    "description": "# ElevenLabs Haystack Integration\n\nThis 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.\n\n## Installation\n\n```bash\npip install elevenlabs_haystack\n```\n\n## Setting Up API Keys\n\n#### **ElevenLabs API Key**\n\nTo access the ElevenLabs API, you need to create an account and obtain an API key.\n\n1. Go to the [ElevenLabs](https://elevenlabs.ai/) website and sign up for an account.\n2. Once logged in, navigate to the **Profile** section.\n3. In the **API** section, generate a new API key.\n4. Copy the API key.\n\n#### **AWS Credentials**\n\nTo store generated audio files on AWS S3, you need AWS credentials (Access Key ID, Secret Access Key) and specify a region.\n\n1. If you don\u2019t have an AWS account, sign up at [AWS](https://aws.amazon.com/).\n2. Create a new IAM user and assign the necessary permissions to allow the user to upload files to S3. The `AmazonS3FullAccess` policy is sufficient for this example.\n3. Once the IAM user is created, download or note the **AWS Access Key ID** and **Secret Access Key**.\n4. Identify the **AWS Region** where your S3 bucket resides (e.g., `us-east-1`). This information can be found in the AWS Management Console.\n5. Finally, create or identify the S3 bucket where the generated audio files will be saved.\n\nCreate a `.env` file in the root directory with the following content (replace with your actual credentials):\n\n```bash\nELEVENLABS_API_KEY=sk_your_elevenlabs_api_key_here\nAWS_ACCESS_KEY_ID=your_aws_access_key_id\nAWS_SECRET_ACCESS_KEY=your_aws_secret_access_key\nAWS_REGION_NAME=us-east-1\nAWS_S3_BUCKET_NAME=your_s3_bucket_name\n```\n\nThese variables will be automatically loaded using `dotenv` and used to access ElevenLabs and AWS services securely.\n\n## Usage\n\n### Basic Text-to-Speech Example\n\nThis 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.\n\n```python\nfrom haystack.utils import Secret\nfrom elevenlabs_haystack import ElevenLabsTextToSpeech\n\n# Initialize the ElevenLabsTextToSpeech component using environment variables for sensitive data\ntts = ElevenLabsTextToSpeech(\n    elevenlabs_api_key=Secret.from_env_var(\"ELEVENLABS_API_KEY\"),\n    output_folder=\"audio_files\",  # Save the generated audio locally\n    voice_id=\"Xb7hH8MSUJpSbSDYk0k2\",  # ElevenLabs voice ID (Alice)\n    aws_s3_bucket_name=Secret.from_env_var(\"AWS_S3_BUCKET_NAME\"),  # S3 bucket for optional upload\n    aws_s3_output_folder=\"s3_files\",  # Save the generated audio to AWS S3\n    aws_access_key_id=Secret.from_env_var(\"AWS_ACCESS_KEY_ID\"),\n    aws_secret_access_key=Secret.from_env_var(\"AWS_SECRET_ACCESS_KEY\"),\n    aws_region_name=Secret.from_env_var(\"AWS_REGION_NAME\"),  # AWS region\n    voice_settings={\n        \"stability\": 0.75,\n        \"similarity_boost\": 0.75,\n        \"style\": 0.5,\n        \"use_speaker_boost\": True,  # Optional voice settings\n    },\n)\n\n# Run the text-to-speech conversion\nresult = tts.run(\"Hello, world!\")\n\n# Print the result\nprint(result)\n\n\"\"\"\n{\n    \"id\": \"elevenlabs-id\",\n    \"file_name\": \"audio_files/elevenlabs-id.mp3\",\n    \"s3_file_name\": \"s3_files/elevenlabs-id.mp3\",\n    \"s3_bucket_name\": \"test-bucket\",\n    \"s3_presigned_url\": \"https://test-bucket.s3.amazonaws.com/s3_files/elevenlabs-id.mp3\"\n}\n\"\"\"\n```\n\n### Example Using Haystack Pipeline\n\nThis example demonstrates how to integrate the `ElevenLabsTextToSpeech` component into a Haystack pipeline. Additionally, we define a `WelcomeTextGenerator` component that generates a personalized welcome message.\n\n```python\nfrom haystack import component, Pipeline\nfrom haystack.utils import Secret\nfrom elevenlabs_haystack import ElevenLabsTextToSpeech\n\n# Define a simple component to generate a welcome message\n@component\nclass WelcomeTextGenerator:\n    \"\"\"\n    A component generating a personal welcome message and making it upper case.\n    \"\"\"\n    @component.output_types(welcome_text=str, note=str)\n    def run(self, name: str):\n        return {\n            \"welcome_text\": f'Hello {name}, welcome to Haystack!'.upper(),\n            \"note\": \"welcome message is ready\"\n        }\n\n# Create a Pipeline\ntext_pipeline = Pipeline()\n\n# Add WelcomeTextGenerator to the Pipeline\ntext_pipeline.add_component(\n    name=\"welcome_text_generator\",\n    instance=WelcomeTextGenerator()\n)\n\n# Add ElevenLabsTextToSpeech to the Pipeline using environment variables\ntext_pipeline.add_component(\n    name=\"tts\",\n    instance=ElevenLabsTextToSpeech(\n        elevenlabs_api_key=Secret.from_env_var(\"ELEVENLABS_API_KEY\"),\n        output_folder=\"audio_files\",  # Save the generated audio locally\n        voice_id=\"Xb7hH8MSUJpSbSDYk0k2\",  # ElevenLabs voice ID (Alice)\n        aws_s3_bucket_name=Secret.from_env_var(\"AWS_S3_BUCKET_NAME\"),  # S3 bucket for optional upload\n        aws_s3_output_folder=\"s3_files\",  # Save the generated audio to AWS S3\n        aws_access_key_id=Secret.from_env_var(\"AWS_ACCESS_KEY_ID\"),\n        aws_secret_access_key=Secret.from_env_var(\"AWS_SECRET_ACCESS_KEY\"),\n        aws_region_name=Secret.from_env_var(\"AWS_REGION_NAME\"),  # Load region from env\n        voice_settings={\n            \"stability\": 0.75,\n            \"similarity_boost\": 0.75,\n            \"style\": 0.5,\n            \"use_speaker_boost\": True,  # Optional voice settings\n        },\n    ),\n)\n\n# Connect the output of WelcomeTextGenerator to the input of ElevenLabsTextToSpeech\ntext_pipeline.connect(sender=\"welcome_text_generator.welcome_text\", receiver=\"tts\")\n\n# Run the pipeline with a sample name\nresult = text_pipeline.run({\"welcome_text_generator\": {\"name\": \"Bilge\"}})\n\n# Print the result\nprint(result)\n\n\"\"\"\n{\n    \"id\": \"elevenlabs-id\",\n    \"file_name\": \"audio_files/elevenlabs-id.mp3\",\n    \"s3_file_name\": \"s3_files/elevenlabs-id.mp3\",\n    \"s3_bucket_name\": \"test-bucket\",\n    \"s3_presigned_url\": \"https://test-bucket.s3.amazonaws.com/s3_files/elevenlabs-id.mp3\"\n}\n\"\"\"\n```\n\n## License\n\nThis project is licensed under the MIT License.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "ElevenLabs Text-to-Speech components for Haystack.",
    "version": "0.4.5",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "91c20050b7faaadd0700c04e5b8f99e75f79bdfbe83ccff3f06c91d2f4092e5c",
                "md5": "8743ebbd73a93c2ca20d5d59ee9b30c6",
                "sha256": "7571330bd983a207ac48abae95dd5c8ef76eb6eeed6f7a8137c2293b7b00f9d1"
            },
            "downloads": -1,
            "filename": "elevenlabs_haystack-0.4.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8743ebbd73a93c2ca20d5d59ee9b30c6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 4876,
            "upload_time": "2024-10-17T19:00:36",
            "upload_time_iso_8601": "2024-10-17T19:00:36.595473Z",
            "url": "https://files.pythonhosted.org/packages/91/c2/0050b7faaadd0700c04e5b8f99e75f79bdfbe83ccff3f06c91d2f4092e5c/elevenlabs_haystack-0.4.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3d6eb23630610777f3ae490d0d066d937d5831abd263f68aa9a5ba8906e245c5",
                "md5": "aca6bee80dc8448bbf9134183f2d9cbd",
                "sha256": "bb02727bd9af1151f639dc4cbc843ef50faa30ee7cf738b54a36b425e1bb84eb"
            },
            "downloads": -1,
            "filename": "elevenlabs_haystack-0.4.5.tar.gz",
            "has_sig": false,
            "md5_digest": "aca6bee80dc8448bbf9134183f2d9cbd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 4051,
            "upload_time": "2024-10-17T19:00:37",
            "upload_time_iso_8601": "2024-10-17T19:00:37.516150Z",
            "url": "https://files.pythonhosted.org/packages/3d/6e/b23630610777f3ae490d0d066d937d5831abd263f68aa9a5ba8906e245c5/elevenlabs_haystack-0.4.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-17 19:00:37",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "elevenlabs-haystack"
}
        
Elapsed time: 1.63061s