Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,35 +14,40 @@ forced_decoder_ids = processor.get_decoder_prompt_ids(language="es", task="trans
|
|
14 |
|
15 |
# Function to process and translate audio
|
16 |
def translate_audio(filepath):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
|
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
return full_translation.strip()
|
46 |
|
47 |
# Gradio UI components
|
48 |
mic_transcribe = gr.Interface(
|
|
|
14 |
|
15 |
# Function to process and translate audio
|
16 |
def translate_audio(filepath):
|
17 |
+
try:
|
18 |
+
if filepath is None:
|
19 |
+
return "No audio file received."
|
20 |
|
21 |
+
audio = AudioSegment.from_file(filepath)
|
22 |
+
chunk_length_ms = 30 * 1000
|
23 |
+
chunks = [audio[i:i + chunk_length_ms] for i in range(0, len(audio), chunk_length_ms)]
|
24 |
|
25 |
+
full_translation = ""
|
26 |
|
27 |
+
for i, chunk in enumerate(chunks):
|
28 |
+
chunk_path = f"chunk_{i}.wav"
|
29 |
+
chunk.export(chunk_path, format="wav")
|
30 |
|
31 |
+
waveform, sample_rate = torchaudio.load(chunk_path)
|
32 |
+
inputs = processor(waveform[0], sampling_rate=sample_rate, return_tensors="pt")
|
33 |
|
34 |
+
with torch.no_grad():
|
35 |
+
generated_ids = model.generate(
|
36 |
+
inputs["input_features"],
|
37 |
+
forced_decoder_ids=forced_decoder_ids
|
38 |
+
)
|
39 |
|
40 |
+
translation = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
41 |
+
full_translation += translation + " "
|
42 |
+
os.remove(chunk_path)
|
43 |
|
44 |
+
return full_translation.strip()
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
# Print error for debugging in HF logs
|
48 |
+
print("ERROR:", str(e))
|
49 |
+
return f"An error occurred: {str(e)}"
|
50 |
|
|
|
51 |
|
52 |
# Gradio UI components
|
53 |
mic_transcribe = gr.Interface(
|