poemsforaphrodite commited on
Commit
0f02095
·
verified ·
1 Parent(s): d4829ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -87
app.py CHANGED
@@ -1,98 +1,69 @@
1
- import streamlit as st
2
  import requests
3
- from io import BytesIO
4
  from requests.exceptions import ConnectionError, SSLError, Timeout
5
 
6
- # Set the page configuration
7
- st.set_page_config(
8
- page_title="Voice Cloning App",
9
- layout="centered",
10
- initial_sidebar_state="auto",
11
- )
12
-
13
- st.title("📢 Voice Cloning Application")
14
- st.write("Enter the details below and upload an audio file to clone the voice.")
15
-
16
- # Create a form for input
17
- with st.form("voice_clone_form"):
18
- # Text input
19
- text = st.text_input("Text", value="مرحباً بكم في تطبيق استنساخ الصوت. يمكنك استخدام هذا التطبيق لإنشاء نسخة من صوتك باللغة العربية.")
20
-
21
- # Language selection
22
- language = st.selectbox("Language", options=["ar"], index=0)
23
 
24
- # File uploader for audio file
25
- audio_file = st.file_uploader("Upload Audio File", type=["wav", "mp3", "ogg"])
 
 
26
 
27
- # Submit button
28
- submit_button = st.form_submit_button(label="Clone Voice")
29
 
30
- if submit_button:
31
- if not audio_file:
32
- st.error("Please upload an audio file.")
33
- else:
34
- try:
35
- # Prepare the payload
36
- payload = {
37
- 'text': text,
38
- 'language': language
39
- }
40
 
41
- # Prepare the files
42
- files = {
43
- 'audio_file': (audio_file.name, audio_file.read(), audio_file.type)
44
- }
45
-
46
- # **Corrected API endpoint**
47
- api_url = "https://tellergen.com/api/clone-voice"
48
-
49
- with st.spinner("Sending request to the API..."):
50
  try:
51
- response = requests.post(
52
- api_url,
53
- data=payload,
54
- files=files,
55
- verify=False, # Disable SSL verification
56
- timeout=30 # Set timeout to 30 seconds
57
- )
58
- response.raise_for_status()
59
- except ConnectionError as e:
60
- st.error("Connection error. Please check your internet connection and try again.")
61
- st.stop()
62
- except SSLError as e:
63
- st.error("SSL Error occurred. Please try again later.")
64
- st.stop()
65
- except Timeout as e:
66
- st.error("Request timed out. Please try again later.")
67
- st.stop()
68
- except Exception as e:
69
- st.error(f"An unexpected error occurred: {str(e)}")
70
- st.stop()
71
 
72
- # Check if the request was successful
73
- if response.status_code == 200:
74
- st.success("Voice cloned successfully!")
 
 
 
 
 
75
 
76
- # Assuming the API returns a URL to the cloned audio or the audio data itself
77
- # Adjust the following based on the actual API response
78
- content_type = response.headers.get('Content-Type')
79
 
80
- if 'audio' in content_type:
81
- # If the response is audio data, allow the user to play it
82
- st.audio(response.content, format=content_type)
83
- else:
84
- # If the response is JSON, display it
85
- try:
86
- response_data = response.json()
87
- st.json(response_data)
88
- except ValueError:
89
- st.text(response.text)
90
- else:
91
- st.error(f"API request failed with status code {response.status_code}")
92
- try:
93
- error_data = response.json()
94
- st.error(error_data)
95
- except ValueError:
96
- st.error(response.text)
97
- except Exception as e:
98
- st.error(f"An error occurred: {e}")
 
1
+ import gradio as gr
2
  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 = {
9
+ 'text': text,
10
+ 'language': 'ar' # Fixed to Arabic as per original app
11
+ }
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Prepare the files
14
+ files = {
15
+ 'audio_file': ('audio.wav', audio_file, 'audio/wav')
16
+ }
17
 
18
+ # API endpoint
19
+ api_url = "https://tellergen.com/api/clone-voice"
20
 
21
+ # Make the request
22
+ response = requests.post(
23
+ api_url,
24
+ data=payload,
25
+ files=files,
26
+ verify=False, # Disable SSL verification
27
+ timeout=30 # Set timeout to 30 seconds
28
+ )
29
+ response.raise_for_status()
 
30
 
31
+ # Check if the request was successful
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 = "مرحباً بكم في تطبيق استنساخ الصوت. يمكنك استخدام هذا التطبيق لإنشاء نسخة من صوتك باللغة العربية."
 
56
 
57
+ demo = gr.Interface(
58
+ fn=clone_voice,
59
+ inputs=[
60
+ gr.Textbox(label="Text", value=default_text),
61
+ gr.Audio(label="Upload Audio File", type="filepath")
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__":
69
+ demo.launch(share=True)