does anyone know how to use TTS that i've deployed...
# agents
a
does anyone know how to use TTS that i've deployed on hugging face inference endpoint in my livekit agent? More specifically, i've deployed canopylabs/orpheus-3b-0.1-ft
r
To use a Text-to-Speech (TTS) model deployed on Hugging Face Inference endpoint with your LiveKit agent, you will need to implement a custom TTS node. Below is a step-by-step guide along with example code to help you integrate your Hugging Face TTS model into your LiveKit agent. • LiveKit Agents Documentation Step-by-Step Guide 1. Set Up Your Hugging Face Inference Endpoint: Ensure that your TTS model (e.g.,
canopylabs/orpheus-3b-0.1-ft
) is deployed and accessible via the Hugging Face Inference API. You will need the endpoint URL and an API token if required. 2. Create a Custom TTS Node: You will need to create a custom TTS node that will call the Hugging Face API to convert text to speech. 3. Integrate the Custom TTS Node into Your LiveKit Agent: Use the custom TTS node in your LiveKit agent setup. Example Code Here’s an example implementation of a custom TTS node that uses the Hugging Face Inference API: (AI Generated Example here so YMMV)
Copy code
import requests
from livekit.agents import VoicePipelineAgent
from livekit.agents.nodes import TTSNode

class HuggingFaceTTS(TTSNode):
    def __init__(self, model_endpoint: str, api_token: str = None):
        self.model_endpoint = model_endpoint
        self.api_token = api_token

    def synthesize(self, text: str) -> bytes:
        headers = {
            "Authorization": f"Bearer {self.api_token}" if self.api_token else None,
            "Content-Type": "application/json"
        }
        payload = {
            "inputs": text
        }
        response = requests.post(self.model_endpoint, headers=headers, json=payload)
        response.raise_for_status()  # Raise an error for bad responses
        return response.content  # Assuming the response contains audio data

# Replace with your Hugging Face Inference endpoint and token
model_endpoint = "https://api-inference.huggingface.co/models/canopylabs/orpheus-3b-0.1-ft"
api_token = "YOUR_HUGGING_FACE_API_TOKEN"  # Optional, if your model requires authentication

# Create the TTS instance
hugging_face_tts = HuggingFaceTTS(model_endpoint=model_endpoint, api_token=api_token)

# Create your LiveKit agent
agent = VoicePipelineAgent(
    chat_ctx=llm.ChatContext(),  # Initialize your chat context
    stt=deepgram.STT(),  # Your STT implementation
    llm=openai.LLM(model="gpt-4o-mini"),  # Your LLM implementation
    tts=hugging_face_tts,  # Use the custom TTS node
    vad=silero_vad,  # Your VAD implementation
    turn_detector=turn_detector.EOUModel()  # Your turn detector
)

# Start your agent
agent.start()
Explanation of the Code • HuggingFaceTTS Class: This class inherits from
TTSNode
and implements the
synthesize
method, which sends a POST request to the Hugging Face Inference API with the text to be synthesized. • Model Endpoint: Replace
model_endpoint
with the actual URL of your deployed model. • API Token: If your model requires authentication, provide your Hugging Face API token. • VoicePipelineAgent: This is where you integrate the custom TTS node into your LiveKit agent. This setup should allow you to use your Hugging Face TTS model within your LiveKit agent effectively. If you have further questions or need additional features, feel free to ask! You could consider making a plugin for it. There are many example here as examples: https://github.com/livekit/agents/tree/main/livekit-plugins