Pyxilab._.Vocify / vocify.py
PyxiLabs's picture
Update vocify.py
d4cb7a1 verified
raw
history blame
3.49 kB
import requests
from pydub import AudioSegment
import io
# Voice mapping dictionary
VOICE_MAPPING = {
"charlottee": "XB0fDUnXU5powFXDhCwa",
"daniel": "onwK4e9ZLuTAKqWW03F9",
"callum": "N2lVS1w4EtoT3dr4eOWO",
"charlie": "IKne3meq5aSn9XLyUdCD",
"clyde": "2EiwWnXFnvU5JabPnv8n",
"dave": "CYw3kZ02Hs0563khs1Fj",
"emily": "LcfcDJNUP1GQjkzn1xUU",
"ethan": "g5CIjZEefAph4nQFvHAz",
"fin": "D38z5RcWu1voky8WS1ja",
"freya": "jsCqWAovK2LkecY7zXl4",
"gigi": "jBpfuIE2acCO8z3wKNLl",
"giovanni": "zcAOhNBS3c14rBihAFp1",
"glinda": "z9fAnlkpzviPz146aGWa",
"grace": "oWAxZDx7w5VEj9dCyTzz",
"harry": "SOYHLrjzK2X1ezoPC6cr",
"james": "ZQe5CZNOzWyzPSCn5a3c",
"jeremy": "bVMeCyTHy58xNoL34h3p"
}
def split_text_into_chunks(text, max_length=490):
"""Split text into chunks of max_length, ensuring chunks end with a full stop, comma, or word."""
chunks = []
while len(text) > max_length:
chunk = text[:max_length]
# Find the last occurrence of a full stop, comma, or space within the chunk
last_punctuation = max(chunk.rfind("."), chunk.rfind(","), chunk.rfind(" "))
if last_punctuation == -1:
# If no punctuation is found, force split at max_length
last_punctuation = max_length
chunks.append(chunk[:last_punctuation + 1].strip())
text = text[last_punctuation + 1:].strip()
chunks.append(text)
return chunks
def generate_speech(voice, input_text, model="eleven_multilingual_v2"):
# Convert voice name to voice ID if necessary
voice_id = VOICE_MAPPING.get(voice.lower(), voice)
# Split the input text into chunks if it exceeds 500 characters
if len(input_text) > 500:
chunks = split_text_into_chunks(input_text)
combined_audio = AudioSegment.empty()
for chunk in chunks:
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}?allow_unauthenticated=1"
headers = {
"Content-Type": "application/json"
}
data = {
"text": chunk,
"model_id": model,
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
audio_segment = AudioSegment.from_file(io.BytesIO(response.content), format="mp3")
combined_audio += audio_segment
else:
print(f"Failed to generate speech for chunk: {response.status_code}, {response.text}")
return [response.status_code, response.text]
# Export the combined audio to a BytesIO object
combined_audio_bytes = io.BytesIO()
combined_audio.export(combined_audio_bytes, format="mp3")
combined_audio_bytes.seek(0)
return combined_audio_bytes.read()
else:
# If the input text is less than or equal to 500 characters, process it directly
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}?allow_unauthenticated=1"
headers = {
"Content-Type": "application/json"
}
data = {
"text": input_text,
"model_id": model,
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.content
else:
print(f"Failed to generate speech: {response.status_code}, {response.text}")
return [response.status_code, response.text]