Create vocify.py
Browse files
vocify.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
# Voice mapping dictionary
|
| 4 |
+
VOICE_MAPPING = {
|
| 5 |
+
"charlottee": "XB0fDUnXU5powFXDhCwa",
|
| 6 |
+
"daniel": "onwK4e9ZLuTAKqWW03F9",
|
| 7 |
+
"callum": "N2lVS1w4EtoT3dr4eOWO",
|
| 8 |
+
"charlie": "IKne3meq5aSn9XLyUdCD",
|
| 9 |
+
"clyde": "2EiwWnXFnvU5JabPnv8n",
|
| 10 |
+
"dave": "CYw3kZ02Hs0563khs1Fj",
|
| 11 |
+
"emily": "LcfcDJNUP1GQjkzn1xUU",
|
| 12 |
+
"ethan": "g5CIjZEefAph4nQFvHAz",
|
| 13 |
+
"fin": "D38z5RcWu1voky8WS1ja",
|
| 14 |
+
"freya": "jsCqWAovK2LkecY7zXl4",
|
| 15 |
+
"gigi": "jBpfuIE2acCO8z3wKNLl",
|
| 16 |
+
"giovanni": "zcAOhNBS3c14rBihAFp1",
|
| 17 |
+
"glinda": "z9fAnlkpzviPz146aGWa",
|
| 18 |
+
"grace": "oWAxZDx7w5VEj9dCyTzz",
|
| 19 |
+
"harry": "SOYHLrjzK2X1ezoPC6cr",
|
| 20 |
+
"james": "ZQe5CZNOzWyzPSCn5a3c",
|
| 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 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}?allow_unauthenticated=1"
|
| 29 |
+
headers = {
|
| 30 |
+
"Content-Type": "application/json"
|
| 31 |
+
}
|
| 32 |
+
data = {
|
| 33 |
+
"text": input_text,
|
| 34 |
+
"model_id": model,
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
response = requests.post(url, json=data, headers=headers)
|
| 38 |
+
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
return response.content
|
| 41 |
+
else:
|
| 42 |
+
print(f"Failed to generate speech: {response.status_code}, {response.text}")
|
| 43 |
+
return [response.status_code, response.text]
|