voice-finetune / app.py
poemsforaphrodite's picture
Update app.py
0f6f40a verified
import gradio as gr
import requests
import logging
from requests.exceptions import ConnectionError, SSLError, Timeout
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def clone_voice(text, audio_file):
if audio_file is None:
raise gr.Error("Please upload an audio file.")
try:
# Prepare the payload
payload = {
'text': text,
'language': 'ar' # Fixed to Arabic as per original app
}
# Prepare the files
files = {
'audio_file': ('audio.wav', open(audio_file, 'rb'), 'audio/wav')
}
# API endpoint
api_url = "https://tellergen.com/api/clone-voice"
logger.info(f"Making request to {api_url}")
# Make the request
try:
# First try with SSL verification
response = requests.post(
api_url,
data=payload,
files=files,
timeout=30
)
except SSLError:
logger.warning("SSL verification failed, retrying without verification")
# If SSL fails, retry without verification
response = requests.post(
api_url,
data=payload,
files=files,
verify=False,
timeout=30
)
logger.info(f"Response status code: {response.status_code}")
response.raise_for_status()
# Check if the request was successful
if response.status_code == 200:
content_type = response.headers.get('Content-Type')
logger.info(f"Response content type: {content_type}")
if 'audio' in content_type:
return response.content
else:
response_text = response.json() if response.headers.get('Content-Type') == 'application/json' else response.text
logger.error(f"Unexpected response content type: {content_type}, response: {response_text}")
raise gr.Error(f"Unexpected response: {response_text}")
else:
logger.error(f"API request failed with status code {response.status_code}")
raise gr.Error(f"API request failed with status code {response.status_code}")
except ConnectionError as e:
logger.error(f"Connection error: {str(e)}")
raise gr.Error("Connection error. Please check your internet connection and try again.")
except SSLError as e:
logger.error(f"SSL Error: {str(e)}")
raise gr.Error("SSL Error occurred. Please try again later.")
except Timeout as e:
logger.error(f"Timeout error: {str(e)}")
raise gr.Error("Request timed out. Please try again later.")
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
raise gr.Error(f"An unexpected error occurred: {str(e)}")
finally:
# Close the file if it was opened
if 'files' in locals() and 'audio_file' in files:
files['audio_file'][1].close()
# Create Gradio interface
default_text = "مرحباً بكم في تطبيق استنساخ الصوت. يمكنك استخدام هذا التطبيق لإنشاء نسخة من صوتك باللغة العربية."
demo = gr.Interface(
fn=clone_voice,
inputs=[
gr.Textbox(label="Text", value=default_text),
gr.Audio(label="Upload Audio File", type="filepath")
],
outputs=gr.Audio(label="Cloned Voice"),
title="📢 Voice Cloning Application",
description="Enter the details below and upload an audio file to clone the voice.",
examples=[
[default_text, None]
]
)
if __name__ == "__main__":
demo.launch()