Update vocify.py
Browse files
vocify.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import requests
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Voice mapping dictionary
|
| 4 |
VOICE_MAPPING = {
|
|
@@ -21,23 +23,63 @@ VOICE_MAPPING = {
|
|
| 21 |
"jeremy": "bVMeCyTHy58xNoL34h3p"
|
| 22 |
}
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def generate_speech(voice, input_text, model="eleven_multilingual_v2"):
|
| 25 |
# Convert voice name to voice ID if necessary
|
| 26 |
voice_id = VOICE_MAPPING.get(voice.lower(), voice)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
else:
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
from pydub import AudioSegment
|
| 3 |
+
import io
|
| 4 |
|
| 5 |
# Voice mapping dictionary
|
| 6 |
VOICE_MAPPING = {
|
|
|
|
| 23 |
"jeremy": "bVMeCyTHy58xNoL34h3p"
|
| 24 |
}
|
| 25 |
|
| 26 |
+
def split_text_into_chunks(text, max_length=490):
|
| 27 |
+
"""Split text into chunks of max_length, ensuring chunks end with a full stop, comma, or word."""
|
| 28 |
+
chunks = []
|
| 29 |
+
while len(text) > max_length:
|
| 30 |
+
chunk = text[:max_length]
|
| 31 |
+
# Find the last occurrence of a full stop, comma, or space within the chunk
|
| 32 |
+
last_punctuation = max(chunk.rfind("."), chunk.rfind(","), chunk.rfind(" "))
|
| 33 |
+
if last_punctuation == -1:
|
| 34 |
+
# If no punctuation is found, force split at max_length
|
| 35 |
+
last_punctuation = max_length
|
| 36 |
+
chunks.append(chunk[:last_punctuation + 1].strip())
|
| 37 |
+
text = text[last_punctuation + 1:].strip()
|
| 38 |
+
chunks.append(text)
|
| 39 |
+
return chunks
|
| 40 |
+
|
| 41 |
def generate_speech(voice, input_text, model="eleven_multilingual_v2"):
|
| 42 |
# Convert voice name to voice ID if necessary
|
| 43 |
voice_id = VOICE_MAPPING.get(voice.lower(), voice)
|
| 44 |
|
| 45 |
+
# Split the input text into chunks if it exceeds 500 characters
|
| 46 |
+
if len(input_text) > 500:
|
| 47 |
+
chunks = split_text_into_chunks(input_text)
|
| 48 |
+
combined_audio = AudioSegment.empty()
|
| 49 |
+
for chunk in chunks:
|
| 50 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}?allow_unauthenticated=1"
|
| 51 |
+
headers = {
|
| 52 |
+
"Content-Type": "application/json"
|
| 53 |
+
}
|
| 54 |
+
data = {
|
| 55 |
+
"text": chunk,
|
| 56 |
+
"model_id": model,
|
| 57 |
+
}
|
| 58 |
+
response = requests.post(url, json=data, headers=headers)
|
| 59 |
+
if response.status_code == 200:
|
| 60 |
+
audio_segment = AudioSegment.from_file(io.BytesIO(response.content), format="mp3")
|
| 61 |
+
combined_audio += audio_segment
|
| 62 |
+
else:
|
| 63 |
+
print(f"Failed to generate speech for chunk: {response.status_code}, {response.text}")
|
| 64 |
+
return [response.status_code, response.text]
|
| 65 |
+
# Export the combined audio to a BytesIO object
|
| 66 |
+
combined_audio_bytes = io.BytesIO()
|
| 67 |
+
combined_audio.export(combined_audio_bytes, format="mp3")
|
| 68 |
+
combined_audio_bytes.seek(0)
|
| 69 |
+
return combined_audio_bytes.read()
|
| 70 |
else:
|
| 71 |
+
# If the input text is less than or equal to 500 characters, process it directly
|
| 72 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}?allow_unauthenticated=1"
|
| 73 |
+
headers = {
|
| 74 |
+
"Content-Type": "application/json"
|
| 75 |
+
}
|
| 76 |
+
data = {
|
| 77 |
+
"text": input_text,
|
| 78 |
+
"model_id": model,
|
| 79 |
+
}
|
| 80 |
+
response = requests.post(url, json=data, headers=headers)
|
| 81 |
+
if response.status_code == 200:
|
| 82 |
+
return response.content
|
| 83 |
+
else:
|
| 84 |
+
print(f"Failed to generate speech: {response.status_code}, {response.text}")
|
| 85 |
+
return [response.status_code, response.text]
|