Spaces:
Running
Running
Commit
•
44da960
1
Parent(s):
3cd1ead
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from deep_translator import GoogleTranslator
|
3 |
+
import gradio as gr
|
4 |
+
import soundfile as sf
|
5 |
+
|
6 |
+
def speech_translation(audio, language):
|
7 |
+
if audio is None:
|
8 |
+
return "No audio input provided!", "No audio input provided!"
|
9 |
+
|
10 |
+
# Convert audio to .wav format if not already
|
11 |
+
if not audio.endswith(".wav"):
|
12 |
+
wav_data, samplerate = sf.read(audio)
|
13 |
+
sf.write("temp_audio.wav", wav_data, samplerate)
|
14 |
+
audio_file = "temp_audio.wav"
|
15 |
+
else:
|
16 |
+
audio_file = audio
|
17 |
+
|
18 |
+
# ASR processing
|
19 |
+
files = {
|
20 |
+
'file': open(audio_file, "rb"),
|
21 |
+
'language': (None, language),
|
22 |
+
'vtt': (None, 'true'),
|
23 |
+
}
|
24 |
+
response = requests.post('https://asr.iitm.ac.in/ssl_asr/decode', files=files)
|
25 |
+
|
26 |
+
print(response.json())
|
27 |
+
try:
|
28 |
+
asr_output = response.json()['transcript']
|
29 |
+
except:
|
30 |
+
asr_output = "Error in ASR processing"
|
31 |
+
|
32 |
+
asr_output = asr_output.replace("।", "")
|
33 |
+
asr_output = asr_output.replace(".", "")
|
34 |
+
|
35 |
+
|
36 |
+
translator = GoogleTranslator(source=language, target='en')
|
37 |
+
translation = translator.translate(asr_output)
|
38 |
+
|
39 |
+
|
40 |
+
return translation
|
41 |
+
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=speech_translation,
|
44 |
+
inputs=[
|
45 |
+
gr.Audio(type="filepath", label="Record your speech"),
|
46 |
+
gr.Dropdown(["telugu", "hindi", "marathi", "bengali"], label="Select Language")
|
47 |
+
],
|
48 |
+
outputs=["text"],
|
49 |
+
title="Speech Translation",
|
50 |
+
description="Record your speech and get the English translation.",
|
51 |
+
)
|
52 |
+
|
53 |
+
iface.launch(share=True)
|