import gradio as gr import requests from requests.exceptions import ConnectionError, SSLError, Timeout 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" # Make the request response = requests.post( api_url, data=payload, files=files, verify=False, # Disable SSL verification timeout=30 # Set timeout to 30 seconds ) response.raise_for_status() # Check if the request was successful if response.status_code == 200: content_type = response.headers.get('Content-Type') if 'audio' in content_type: # Return the audio response return response.content else: # If not audio response, raise an error response_text = response.json() if response.headers.get('Content-Type') == 'application/json' else response.text raise gr.Error(f"Unexpected response: {response_text}") else: raise gr.Error(f"API request failed with status code {response.status_code}") except ConnectionError: raise gr.Error("Connection error. Please check your internet connection and try again.") except SSLError: raise gr.Error("SSL Error occurred. Please try again later.") except Timeout: raise gr.Error("Request timed out. Please try again later.") except Exception as 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(share=True)