poemsforaphrodite commited on
Commit
5f75033
·
verified ·
1 Parent(s): 3fb7274

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
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
- # Save the audio response to a temporary file
36
  return response.content
37
  else:
38
- try:
39
- return str(response.json())
40
- except ValueError:
41
- return response.text
42
  else:
43
- return f"API request failed with status code {response.status_code}"
44
 
45
  except ConnectionError:
46
- return "Connection error. Please check your internet connection and try again."
47
  except SSLError:
48
- return "SSL Error occurred. Please try again later."
49
  except Timeout:
50
- return "Request timed out. Please try again later."
51
  except Exception as e:
52
- return f"An unexpected error occurred: {str(e)}"
 
 
 
 
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__":