#How do I handle unicode characters when calling the api?

1 messages · Page 1 of 1 (latest)

livid sapphireBOT
#
AI-generated Answer for tractorjuice

To handle Unicode characters when calling the API, ensure that your text is properly encoded in UTF-8. This will allow the API to correctly interpret and process the characters. Here's an example of how to set the headers and body in a request:

import requests

url = "https://api.elevenlabs.io/your-endpoint"
headers = {
    "Content-Type": "application/json",
    "xi-api-key": "your_api_key"
}
data = {
    "text": "Your text with Unicode characters like emojis 😊 or accented characters é"
}

response = requests.post(url, headers=headers, json=data)
print(response.json())

Make sure to replace "your_api_key" and "your-endpoint" with your actual API key and endpoint. This will ensure that your Unicode characters are correctly handled by the API.

tribal isle
#

My text has unicode escape characters

livid sapphireBOT
#
AI-generated Answer for tractorjuice

When handling Unicode characters in your text for the API, ensure that your text is properly encoded in UTF-8. Most programming languages and libraries handle this automatically, but you can explicitly encode your text if needed.

Here's an example in Python:

import requests

api_key = 'your_api_key'
url = 'https://api.elevenlabs.io/v1/text-to-speech'
headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_key}'
}
data = {
    'text': 'Your text with unicode characters like emojis 😊',
    'voice': 'your_voice_id',
    'voice_settings': {}
}

response = requests.post(url, json=data, headers=headers)
audio_content = response.content

with open('output.mp3', 'wb') as audio_file:
    audio_file.write(audio_content)

Make sure your text string is properly encoded and passed to the API. This will ensure that Unicode characters are correctly interpreted and processed.

tribal isle
#

How do i make sure they are utf-8

livid sapphireBOT
#
AI-generated Answer for tractorjuice

To ensure your text is UTF-8 encoded when calling the API, you can use the following approach in Python:

import requests

text = "Your text with unicode escape characters"
utf8_text = text.encode('utf-8')

response = requests.post(
    'https://api.elevenlabs.io/your-endpoint',
    data=utf8_text,
    headers={'Content-Type': 'application/json'}
)

print(response.json())

This will encode your text in UTF-8 before sending it to the API. Make sure to replace 'https://api.elevenlabs.io/your-endpoint' with the actual API endpoint you are using.

tribal isle
#

How would I handle this text: Imagine if your body temperature rose by 1\u00b0C permanently.

livid sapphireBOT
#
AI-generated Answer for tractorjuice

To handle the text with Unicode escape characters and ensure it is in UTF-8, you can decode the Unicode escape sequences. Here's an example in Python:

text = "Imagine if your body temperature rose by 1\u00b0C permanently."
decoded_text = text.encode('utf-8').decode('unicode_escape')
print(decoded_text)

This will convert the Unicode escape sequence \u00b0 to the actual degree symbol °. The output will be:

Imagine if your body temperature rose by 1°C permanently.

You can then use this decoded text in your API calls.

tribal isle
#

how would i pass it to the api

livid sapphireBOT
#
AI-generated Answer for tractorjuice

To handle Unicode escape characters and ensure they are in UTF-8 format, you can decode the text before sending it to the API. Here's an example in Python:

import json
import websockets
import asyncio

text = "Imagine if your body temperature rose by 1\u00b0C permanently."
decoded_text = text.encode('utf-8').decode('unicode_escape')

async def text_to_speech_ws_streaming(voice_id, model_id):
    uri = f"wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input?model_id={model_id}"
    async with websockets.connect(uri) as websocket:
        await websocket.send(json.dumps({
            "text": decoded_text,
            "voice_settings": {"stability": 0.5, "similarity_boost": 0.8, "use_speaker_boost": False},
            "generation_config": {
                "chunk_length_schedule": [120, 160, 250, 290]
            },
            "xi_api_key": "your_elevenlabs_api_key_here",
        }))
        await websocket.send(json.dumps({"text": ""}))  # Close the connection

voice_id = 'kmSVBPu7loj4ayNinwWM'
model_id = 'eleven_turbo_v2'
asyncio.run(text_to_speech_ws_streaming(voice_id, model_id))

This code snippet decodes the Unicode escape characters and ensures the text is in UTF-8 format before sending it to the API.

tribal isle
#

How would I do this with the python client

livid sapphireBOT
#
AI-generated Answer for tractorjuice

To handle Unicode characters and ensure they are UTF-8 encoded when calling the API with the Python client, you can use the encode method. Here's how you can handle the text and pass it to the API:

  1. Ensure the text is properly encoded:

    text = "Imagine if your body temperature rose by 1\u00b0C permanently."
    utf8_text = text.encode('utf-8').decode('utf-8')
    
  2. Pass the encoded text to the API:

    import os
    from dotenv import load_dotenv
    import websockets
    import json
    import asyncio
    
    # Load the API key from the .env file
    load_dotenv()
    ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
    
    voice_id = 'kmSVBPu7loj4ayNinwWM'
    model_id = 'eleven_turbo_v2'
    
    async def text_to_speech_ws_streaming(voice_id, model_id, text):
        uri = f"wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input?model_id={model_id}"
    
        async with websockets.connect(uri) as websocket:
            await websocket.send(json.dumps({
                "text": " ",
                "voice_settings": {"stability": 0.5, "similarity_boost": 0.8, "use_speaker_boost": False},
                "generation_config": {
                    "chunk_length_schedule": [120, 160, 250, 290]
                },
                "xi_api_key": ELEVENLABS_API_KEY,
            }))
    
            await websocket.send(json.dumps({"text": text}))
    
            # Send empty string to indicate the end of the text sequence which will close the websocket connection
            await websocket.send(json.dumps({"text": ""}))
    
    asyncio.run(text_to_speech_ws_streaming(voice_id, model_id, utf8_text))
    

This script ensures that the text is properly encoded in UTF-8 before sending it to the API.

tribal isle
#

Do i need to send it as utf-8