Spaces:
Running
Running
vismaya2939
commited on
Commit
•
d138cc3
1
Parent(s):
137f837
Update app.py
Browse files
app.py
CHANGED
@@ -47,7 +47,7 @@ def translate_text(text, target_language):
|
|
47 |
return text
|
48 |
return text # If English, return the same text
|
49 |
|
50 |
-
# Function to send a request to the Hugging Face API with retry on 503
|
51 |
def generate_speech(model_id, text):
|
52 |
url = f"https://api-inference.huggingface.co/models/{model_id}"
|
53 |
headers = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
|
@@ -64,6 +64,9 @@ def generate_speech(model_id, text):
|
|
64 |
estimated_time = response_data.get("estimated_time", 10)
|
65 |
st.info(f"Model is loading, please wait {estimated_time} seconds...")
|
66 |
time.sleep(estimated_time) # Wait before retrying
|
|
|
|
|
|
|
67 |
else:
|
68 |
st.error(f"Error: {response.status_code} - {response.text}")
|
69 |
return None
|
@@ -101,7 +104,7 @@ if submit and user_input:
|
|
101 |
st.write(f"Original Text: {user_input}")
|
102 |
st.write(f"Translated Text: {translated_text}")
|
103 |
|
104 |
-
# Generate speech with retry if the model is loading
|
105 |
audio_data = generate_speech("myshell-ai/MeloTTS-English", translated_text)
|
106 |
|
107 |
if audio_data:
|
|
|
47 |
return text
|
48 |
return text # If English, return the same text
|
49 |
|
50 |
+
# Function to send a request to the Hugging Face API with retry on 503 and error handling for 500
|
51 |
def generate_speech(model_id, text):
|
52 |
url = f"https://api-inference.huggingface.co/models/{model_id}"
|
53 |
headers = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
|
|
|
64 |
estimated_time = response_data.get("estimated_time", 10)
|
65 |
st.info(f"Model is loading, please wait {estimated_time} seconds...")
|
66 |
time.sleep(estimated_time) # Wait before retrying
|
67 |
+
elif response.status_code == 500:
|
68 |
+
st.error("Internal Server Error: The model encountered an issue. Please try again later.")
|
69 |
+
return None
|
70 |
else:
|
71 |
st.error(f"Error: {response.status_code} - {response.text}")
|
72 |
return None
|
|
|
104 |
st.write(f"Original Text: {user_input}")
|
105 |
st.write(f"Translated Text: {translated_text}")
|
106 |
|
107 |
+
# Generate speech with retry if the model is loading and handle 500 errors
|
108 |
audio_data = generate_speech("myshell-ai/MeloTTS-English", translated_text)
|
109 |
|
110 |
if audio_data:
|