Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 6 |
+
|
| 7 |
+
def tts_elevenlabs(text, voice="Rachel"):
|
| 8 |
+
url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice}"
|
| 9 |
+
|
| 10 |
+
headers = {
|
| 11 |
+
"Accept": "audio/mpeg",
|
| 12 |
+
"xi-api-key": API_KEY,
|
| 13 |
+
"Content-Type": "application/json"
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
data = {
|
| 17 |
+
"text": text,
|
| 18 |
+
"voice_settings": {
|
| 19 |
+
"stability": 0.75,
|
| 20 |
+
"similarity_boost": 0.75
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
response = requests.post(url, json=data, headers=headers)
|
| 25 |
+
|
| 26 |
+
if response.status_code == 200:
|
| 27 |
+
with open("output.mp3", "wb") as f:
|
| 28 |
+
f.write(response.content)
|
| 29 |
+
return "output.mp3"
|
| 30 |
+
else:
|
| 31 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 32 |
+
|
| 33 |
+
demo = gr.Interface(fn=tts_elevenlabs, inputs="text", outputs="audio", title="Natural TTS with ElevenLabs")
|
| 34 |
+
|
| 35 |
+
demo.launch()
|