Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ import requests
|
|
3 |
from requests.exceptions import ConnectionError, SSLError, Timeout
|
4 |
|
5 |
def clone_voice(text, audio_file):
|
|
|
|
|
|
|
6 |
try:
|
7 |
# Prepare the payload
|
8 |
payload = {
|
@@ -12,7 +15,7 @@ def clone_voice(text, audio_file):
|
|
12 |
|
13 |
# Prepare the files
|
14 |
files = {
|
15 |
-
'audio_file': ('audio.wav', audio_file, 'audio/wav')
|
16 |
}
|
17 |
|
18 |
# API endpoint
|
@@ -32,24 +35,27 @@ def clone_voice(text, audio_file):
|
|
32 |
if response.status_code == 200:
|
33 |
content_type = response.headers.get('Content-Type')
|
34 |
if 'audio' in content_type:
|
35 |
-
#
|
36 |
return response.content
|
37 |
else:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
return response.text
|
42 |
else:
|
43 |
-
|
44 |
|
45 |
except ConnectionError:
|
46 |
-
|
47 |
except SSLError:
|
48 |
-
|
49 |
except Timeout:
|
50 |
-
|
51 |
except Exception as e:
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Create Gradio interface
|
55 |
default_text = "مرحباً بكم في تطبيق استنساخ الصوت. يمكنك استخدام هذا التطبيق لإنشاء نسخة من صوتك باللغة العربية."
|
@@ -62,7 +68,10 @@ demo = gr.Interface(
|
|
62 |
],
|
63 |
outputs=gr.Audio(label="Cloned Voice"),
|
64 |
title="📢 Voice Cloning Application",
|
65 |
-
description="Enter the details below and upload an audio file to clone the voice."
|
|
|
|
|
|
|
66 |
)
|
67 |
|
68 |
if __name__ == "__main__":
|
|
|
3 |
from requests.exceptions import ConnectionError, SSLError, Timeout
|
4 |
|
5 |
def clone_voice(text, audio_file):
|
6 |
+
if audio_file is None:
|
7 |
+
raise gr.Error("Please upload an audio file.")
|
8 |
+
|
9 |
try:
|
10 |
# Prepare the payload
|
11 |
payload = {
|
|
|
15 |
|
16 |
# Prepare the files
|
17 |
files = {
|
18 |
+
'audio_file': ('audio.wav', open(audio_file, 'rb'), 'audio/wav')
|
19 |
}
|
20 |
|
21 |
# API endpoint
|
|
|
35 |
if response.status_code == 200:
|
36 |
content_type = response.headers.get('Content-Type')
|
37 |
if 'audio' in content_type:
|
38 |
+
# Return the audio response
|
39 |
return response.content
|
40 |
else:
|
41 |
+
# If not audio response, raise an error
|
42 |
+
response_text = response.json() if response.headers.get('Content-Type') == 'application/json' else response.text
|
43 |
+
raise gr.Error(f"Unexpected response: {response_text}")
|
|
|
44 |
else:
|
45 |
+
raise gr.Error(f"API request failed with status code {response.status_code}")
|
46 |
|
47 |
except ConnectionError:
|
48 |
+
raise gr.Error("Connection error. Please check your internet connection and try again.")
|
49 |
except SSLError:
|
50 |
+
raise gr.Error("SSL Error occurred. Please try again later.")
|
51 |
except Timeout:
|
52 |
+
raise gr.Error("Request timed out. Please try again later.")
|
53 |
except Exception as e:
|
54 |
+
raise gr.Error(f"An unexpected error occurred: {str(e)}")
|
55 |
+
finally:
|
56 |
+
# Close the file if it was opened
|
57 |
+
if 'files' in locals() and 'audio_file' in files:
|
58 |
+
files['audio_file'][1].close()
|
59 |
|
60 |
# Create Gradio interface
|
61 |
default_text = "مرحباً بكم في تطبيق استنساخ الصوت. يمكنك استخدام هذا التطبيق لإنشاء نسخة من صوتك باللغة العربية."
|
|
|
68 |
],
|
69 |
outputs=gr.Audio(label="Cloned Voice"),
|
70 |
title="📢 Voice Cloning Application",
|
71 |
+
description="Enter the details below and upload an audio file to clone the voice.",
|
72 |
+
examples=[
|
73 |
+
[default_text, None]
|
74 |
+
]
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|