File size: 3,775 Bytes
0f02095
d88c00f
0f6f40a
2b93622
d88c00f
0f6f40a
 
 
 
0f02095
5f75033
 
 
0f02095
 
 
 
 
 
d88c00f
0f02095
 
5f75033
0f02095
d88c00f
0f02095
 
0f6f40a
 
 
0f02095
0f6f40a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f02095
146c590
0f02095
 
 
0f6f40a
 
0f02095
 
 
5f75033
0f6f40a
5f75033
0f02095
0f6f40a
5f75033
146c590
0f6f40a
 
5f75033
0f6f40a
 
5f75033
0f6f40a
 
5f75033
0f02095
0f6f40a
5f75033
 
 
 
 
146c590
0f02095
 
146c590
0f02095
 
 
 
 
 
 
 
5f75033
 
 
 
0f02095
 
 
0f6f40a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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()