vismaya2939 commited on
Commit
137f837
Β·
verified Β·
1 Parent(s): 6cecadb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -3,6 +3,7 @@ import streamlit as st
3
  from dotenv import load_dotenv
4
  import base64
5
  import requests
 
6
 
7
  # Load environment variables from the .env file
8
  load_dotenv()
@@ -46,13 +47,26 @@ def translate_text(text, target_language):
46
  return text
47
  return text # If English, return the same text
48
 
49
- # Initialize Streamlit UI
50
- st.set_page_config(page_title="Multilingual Text-to-Speech", page_icon="πŸ”Š")
51
- st.header("Multilingual Text-to-Speech Demo")
 
 
52
 
53
- # Get user input and language selection
54
- user_input = get_text()
55
- selected_language = select_language()
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Function to play audio in Streamlit
58
  def play_audio(audio_bytes):
@@ -65,6 +79,14 @@ def play_audio(audio_bytes):
65
  """
66
  st.markdown(audio_html, unsafe_allow_html=True)
67
 
 
 
 
 
 
 
 
 
68
  # Create a button for generating speech
69
  submit = st.button('Generate Speech')
70
 
@@ -79,19 +101,10 @@ if submit and user_input:
79
  st.write(f"Original Text: {user_input}")
80
  st.write(f"Translated Text: {translated_text}")
81
 
82
- # Make a direct request to the API for speech synthesis
83
- url = f"https://api-inference.huggingface.co/models/myshell-ai/MeloTTS-English"
84
- headers = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
85
- payload = {"inputs": translated_text}
86
-
87
- # Requesting raw response with the option to manually handle the audio
88
- response = requests.post(url, headers=headers, json=payload, stream=True)
89
 
90
- if response.status_code == 200:
91
- audio_data = response.content
92
- # Assuming the response is in audio/wav or another format
93
  play_audio(audio_data)
94
- else:
95
- st.error(f"Error: {response.status_code} - {response.text}")
96
  elif submit:
97
  st.warning("Please enter some text.") # Warning for empty input
 
3
  from dotenv import load_dotenv
4
  import base64
5
  import requests
6
+ import time
7
 
8
  # Load environment variables from the .env file
9
  load_dotenv()
 
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}"}
54
+ payload = {"inputs": text}
55
 
56
+ while True:
57
+ response = requests.post(url, headers=headers, json=payload, stream=True)
58
+
59
+ if response.status_code == 200:
60
+ return response.content # Return the audio data if successful
61
+ elif response.status_code == 503:
62
+ # Parse the estimated wait time from the response and wait
63
+ response_data = response.json()
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
70
 
71
  # Function to play audio in Streamlit
72
  def play_audio(audio_bytes):
 
79
  """
80
  st.markdown(audio_html, unsafe_allow_html=True)
81
 
82
+ # Initialize Streamlit UI
83
+ st.set_page_config(page_title="Multilingual Text-to-Speech", page_icon="πŸ”Š")
84
+ st.header("Multilingual Text-to-Speech Demo")
85
+
86
+ # Get user input and language selection
87
+ user_input = get_text()
88
+ selected_language = select_language()
89
+
90
  # Create a button for generating speech
91
  submit = st.button('Generate Speech')
92
 
 
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:
 
 
108
  play_audio(audio_data)
 
 
109
  elif submit:
110
  st.warning("Please enter some text.") # Warning for empty input