import gradio as gr import requests import json import os # Replace with your actual Play.ht API key and User ID API_KEY = "your_play_ht_api_key" USER_ID = "your_play_ht_user_id" # Function to interact with Play.ht API def text_to_audio(text): url = "https://play.ht/api/v2/tts" headers = { "Authorization": API_KEY, "X-User-ID": USER_ID, "Content-Type": "application/json" } # Customize options here (e.g., voice, language, etc.) data = { "voice": "en_us-male-1", # Specify a voice of choice "content": [text], "speed": 1.0, "sample_rate": 24000, } response = requests.post(url, headers=headers, json=data) if response.status_code == 201: response_data = response.json() audio_url = response_data.get("audio_url") # Fetch the audio file audio_response = requests.get(audio_url) audio_path = "output_audio.mp3" with open(audio_path, "wb") as audio_file: audio_file.write(audio_response.content) return audio_path else: return f"Error: {response.status_code}, {response.text}" # Gradio app interface iface = gr.Interface( fn=text_to_audio, inputs="text", outputs="audio", title="Text to Speech with Play.ht", description="Convert text into realistic speech using Play.ht." ) iface.launch()