import gradio as gr import requests import json import os # Load the API key from environment variables XI_API_KEY = os.getenv("XI_API_KEY") # Define the function to perform the Speech-to-Speech transformation def sts_conversion(voice_id, audio_file): CHUNK_SIZE = 1024 OUTPUT_PATH = "output.mp3" # Construct the URL for the Speech-to-Speech API request sts_url = f"https://api.elevenlabs.io/v1/speech-to-speech/{voice_id}/stream" # Set up headers for the API request, including the API key for authentication headers = { "Accept": "application/json", "xi-api-key": XI_API_KEY } # Set up the data payload for the API request, including model ID and voice settings data = { "model_id": "eleven_english_sts_v2", "voice_settings": json.dumps({ "stability": 0.5, "similarity_boost": 0.8, "style": 0.0, "use_speaker_boost": True }) } # Set up the files to send with the request, including the input audio file files = { "audio": audio_file } # Make the POST request to the STS API with headers, data, and files, enabling streaming response response = requests.post(sts_url, headers=headers, data=data, files=files, stream=True) # Check if the request was successful if response.ok: # Open the output file in write-binary mode with open(OUTPUT_PATH, "wb") as f: # Read the response in chunks and write to the file for chunk in response.iter_content(chunk_size=CHUNK_SIZE): f.write(chunk) # Return the output audio file for download return OUTPUT_PATH else: # Return the error message if the request was not successful return response.text # Create the Gradio Blocks UI with gr.Blocks(theme="Hev832/Applio") as demo: gr.Markdown("#