Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Replace with your actual Play.ht API key and User ID
|
| 7 |
+
API_KEY = "your_play_ht_api_key"
|
| 8 |
+
USER_ID = "your_play_ht_user_id"
|
| 9 |
+
|
| 10 |
+
# Function to interact with Play.ht API
|
| 11 |
+
def text_to_audio(text):
|
| 12 |
+
url = "https://play.ht/api/v2/tts"
|
| 13 |
+
|
| 14 |
+
headers = {
|
| 15 |
+
"Authorization": API_KEY,
|
| 16 |
+
"X-User-ID": USER_ID,
|
| 17 |
+
"Content-Type": "application/json"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
# Customize options here (e.g., voice, language, etc.)
|
| 21 |
+
data = {
|
| 22 |
+
"voice": "en_us-male-1", # Specify a voice of choice
|
| 23 |
+
"content": [text],
|
| 24 |
+
"speed": 1.0,
|
| 25 |
+
"sample_rate": 24000,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
response = requests.post(url, headers=headers, json=data)
|
| 29 |
+
|
| 30 |
+
if response.status_code == 201:
|
| 31 |
+
response_data = response.json()
|
| 32 |
+
audio_url = response_data.get("audio_url")
|
| 33 |
+
|
| 34 |
+
# Fetch the audio file
|
| 35 |
+
audio_response = requests.get(audio_url)
|
| 36 |
+
audio_path = "output_audio.mp3"
|
| 37 |
+
with open(audio_path, "wb") as audio_file:
|
| 38 |
+
audio_file.write(audio_response.content)
|
| 39 |
+
|
| 40 |
+
return audio_path
|
| 41 |
+
else:
|
| 42 |
+
return f"Error: {response.status_code}, {response.text}"
|
| 43 |
+
|
| 44 |
+
# Gradio app interface
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=text_to_audio,
|
| 47 |
+
inputs="text",
|
| 48 |
+
outputs="audio",
|
| 49 |
+
title="Text to Speech with Play.ht",
|
| 50 |
+
description="Convert text into realistic speech using Play.ht."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
iface.launch()
|