Update app.py
Browse files
app.py
CHANGED
@@ -124,64 +124,57 @@ def models(text, model="Llama 3 8B Service", seed=42):
|
|
124 |
|
125 |
return output
|
126 |
|
127 |
-
def
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
"--vocoder_name", "vocoder_pretssel",
|
140 |
-
"--gated-model-dir", "models",
|
141 |
-
"--output_path", output_file
|
142 |
-
]
|
143 |
|
144 |
-
|
|
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
151 |
return None
|
152 |
|
153 |
-
|
154 |
-
async def respond_with_retry(audio, model, seed, target_language, max_retries=3):
|
155 |
-
for attempt in range(max_retries):
|
156 |
-
try:
|
157 |
-
return await respond(audio, model, seed, target_language)
|
158 |
-
except ClientDisconnect:
|
159 |
-
if attempt < max_retries - 1:
|
160 |
-
logger.warning(f"Client disconnected. Retrying... (Attempt {attempt + 1}/{max_retries})")
|
161 |
-
await asyncio.sleep(1) # Wait a bit before retrying
|
162 |
-
else:
|
163 |
-
logger.error("Max retries reached. Client repeatedly disconnected.")
|
164 |
-
return None, None
|
165 |
-
except Exception as e:
|
166 |
-
logger.error(f"An error occurred: {str(e)}")
|
167 |
-
return None, None
|
168 |
-
|
169 |
async def respond(audio, model, seed, target_language):
|
170 |
-
start_time = time.time()
|
171 |
try:
|
172 |
if audio is None:
|
173 |
return None, None
|
174 |
-
|
175 |
user_input = transcribe(audio)
|
176 |
if not user_input:
|
177 |
return None, None
|
178 |
-
|
179 |
-
logger.info(f"Processing input: {user_input[:50]}...") # Log the first 50 characters of input
|
180 |
-
|
181 |
if user_input.lower().startswith("please translate"):
|
182 |
-
|
183 |
-
translated_audio =
|
184 |
-
logger.info(f"Translation completed in {time.time() - start_time:.2f} seconds")
|
185 |
return None, translated_audio
|
186 |
else:
|
187 |
reply = models(user_input, model, seed)
|
@@ -189,10 +182,12 @@ async def respond(audio, model, seed, target_language):
|
|
189 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
190 |
tmp_path = tmp_file.name
|
191 |
await communicate.save(tmp_path)
|
192 |
-
logger.info(f"Voice assistant response generated in {time.time() - start_time:.2f} seconds")
|
193 |
return tmp_path, None
|
|
|
|
|
|
|
194 |
except Exception as e:
|
195 |
-
|
196 |
return None, None
|
197 |
|
198 |
def clear_history():
|
@@ -237,7 +232,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
237 |
translated_audio = gr.Audio(label="Translated Audio", type="filepath", interactive=False, autoplay=True)
|
238 |
|
239 |
input_audio.change(
|
240 |
-
fn=
|
241 |
inputs=[input_audio, select, seed, target_lang],
|
242 |
outputs=[output_audio, translated_audio],
|
243 |
)
|
@@ -245,8 +240,4 @@ with gr.Blocks(css="style.css") as demo:
|
|
245 |
clear_button.click(fn=clear_history, inputs=[], outputs=[output_audio, translated_audio])
|
246 |
|
247 |
if __name__ == "__main__":
|
248 |
-
demo.queue(concurrency_count=5, max_size=20).launch(
|
249 |
-
share=True,
|
250 |
-
debug=True,
|
251 |
-
enable_queue=True,
|
252 |
-
)
|
|
|
124 |
|
125 |
return output
|
126 |
|
127 |
+
async def translate_speech_with_timeout(audio_file, target_language, timeout=30):
|
128 |
+
try:
|
129 |
+
language_code = LANGUAGE_CODES[target_language]
|
130 |
+
output_file = f"translated_audio_{int(time.time())}.wav"
|
131 |
+
|
132 |
+
command = [
|
133 |
+
"expressivity_predict",
|
134 |
+
audio_file,
|
135 |
+
"--tgt_lang", language_code,
|
136 |
+
"--model_name", "seamless_expressivity",
|
137 |
+
"--vocoder_name", "vocoder_pretssel",
|
138 |
+
"--gated-model-dir", "models",
|
139 |
+
"--output_path", output_file
|
140 |
+
]
|
141 |
|
142 |
+
process = await asyncio.create_subprocess_exec(
|
143 |
+
*command,
|
144 |
+
stdout=asyncio.subprocess.PIPE,
|
145 |
+
stderr=asyncio.subprocess.PIPE
|
146 |
+
)
|
147 |
|
148 |
+
try:
|
149 |
+
stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
|
150 |
+
except asyncio.TimeoutError:
|
151 |
+
process.kill()
|
152 |
+
raise Exception("Translation process timed out")
|
|
|
|
|
|
|
|
|
153 |
|
154 |
+
if process.returncode != 0:
|
155 |
+
raise Exception(f"Translation process failed: {stderr.decode()}")
|
156 |
|
157 |
+
if os.path.exists(output_file):
|
158 |
+
print(f"File created successfully: {output_file}")
|
159 |
+
return output_file
|
160 |
+
else:
|
161 |
+
raise Exception(f"File not found: {output_file}")
|
162 |
+
except Exception as e:
|
163 |
+
print(f"Translation error: {str(e)}")
|
164 |
return None
|
165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
async def respond(audio, model, seed, target_language):
|
|
|
167 |
try:
|
168 |
if audio is None:
|
169 |
return None, None
|
170 |
+
|
171 |
user_input = transcribe(audio)
|
172 |
if not user_input:
|
173 |
return None, None
|
174 |
+
|
|
|
|
|
175 |
if user_input.lower().startswith("please translate"):
|
176 |
+
# Use background task for translation
|
177 |
+
translated_audio = await translate_speech_with_timeout(audio, target_language)
|
|
|
178 |
return None, translated_audio
|
179 |
else:
|
180 |
reply = models(user_input, model, seed)
|
|
|
182 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
183 |
tmp_path = tmp_file.name
|
184 |
await communicate.save(tmp_path)
|
|
|
185 |
return tmp_path, None
|
186 |
+
except ClientDisconnect:
|
187 |
+
print("Client disconnected")
|
188 |
+
return None, None
|
189 |
except Exception as e:
|
190 |
+
print(f"An error occurred: {str(e)}")
|
191 |
return None, None
|
192 |
|
193 |
def clear_history():
|
|
|
232 |
translated_audio = gr.Audio(label="Translated Audio", type="filepath", interactive=False, autoplay=True)
|
233 |
|
234 |
input_audio.change(
|
235 |
+
fn=respond,
|
236 |
inputs=[input_audio, select, seed, target_lang],
|
237 |
outputs=[output_audio, translated_audio],
|
238 |
)
|
|
|
240 |
clear_button.click(fn=clear_history, inputs=[], outputs=[output_audio, translated_audio])
|
241 |
|
242 |
if __name__ == "__main__":
|
243 |
+
demo.queue(concurrency_count=5, max_size=20).launch()
|
|
|
|
|
|
|
|