File size: 694 Bytes
c7fc3b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
from config import HUGGINGFACE_API_KEY

# Bark model
BARK_MODEL_ID = "suno/bark"

def text_to_speech(text: str) -> bytes:
    """
    Convert text to speech (audio bytes) using Hugging Face Bark model.
    """
    url = f"https://api-inference.huggingface.co/models/{BARK_MODEL_ID}"

    headers = {
        "Authorization": f"Bearer {HUGGINGFACE_API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "inputs": text
    }

    response = requests.post(url, headers=headers, json=payload)

    # Bark returns raw WAV bytes
    if response.status_code != 200:
        raise Exception(f"Bark API error: {response.text}")

    return response.content