Spaces:
Runtime error
Runtime error
| import requests | |
| # 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 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) | |
| 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] | |
| if __name__ == "__main__": | |
| text = """हमारा भारत एक विविधताओं से भरा देश है। | |
| यहाँ कई भाषाएँ, संस्कृतियाँ और परंपराएँ एक साथ फलती-फूलती हैं। | |
| प्रकृति ने भी इस देश को अपार सुंदरता से सजाया है। | |
| पहाड़, नदियाँ, समुद्र और हरे-भरे जंगल, सभी इसकी शोभा बढ़ाते हैं।""" | |
| voice = "charlottee" # You can now use the voice name instead of the ID | |
| print(generate_speech(voice, text)) |