Spaces:
Sleeping
Sleeping
Commit
·
ee07b70
1
Parent(s):
cae6b5d
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,45 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from gtts import gTTS
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
|
| 6 |
|
| 7 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
| 8 |
|
| 9 |
+
|
| 10 |
+
recognizer = sr.Recognizer()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
audio_input = st.empty()
|
| 14 |
+
|
| 15 |
+
if st.checkbox("Use Microphone for English Input"):
|
| 16 |
+
with audio_input:
|
| 17 |
+
st.warning("Listening for audio input... Speak in English.")
|
| 18 |
+
try:
|
| 19 |
+
with sr.Microphone() as source:
|
| 20 |
+
audio = recognizer.listen(source)
|
| 21 |
+
st.success("Audio input recorded. Translating...")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
english_text = recognizer.recognize_google(audio, language='en')
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
out = pipe(english_text, src_lang='en', tgt_lang='hi')
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
translation_text = out[0]['translation_text']
|
| 31 |
+
st.text(f"English Input: {english_text}")
|
| 32 |
+
st.text(f"Hindi Translation: {translation_text}")
|
| 33 |
+
|
| 34 |
+
tts = gTTS(translation_text, lang='hi')
|
| 35 |
+
tts.save("translated_audio.mp3")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
st.audio("translated_audio.mp3", format='audio/mp3')
|
| 39 |
+
|
| 40 |
+
except sr.WaitTimeoutError:
|
| 41 |
+
st.warning("No speech detected. Please speak into the microphone.")
|
| 42 |
+
except sr.RequestError as e:
|
| 43 |
+
st.error(f"Could not request results from Google Speech Recognition service: {e}")
|
| 44 |
+
except sr.UnknownValueError:
|
| 45 |
+
st.warning("Speech recognition could not understand the audio.")
|