Spaces:
Sleeping
Sleeping
shahpalash10
commited on
Commit
•
8d593f1
1
Parent(s):
1eadbef
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,25 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
-
from gtts import gTTS
|
4 |
-
import
|
5 |
-
import pyaudio
|
6 |
|
7 |
# Create a translation pipeline
|
8 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# Check if the microphone input is requested
|
28 |
-
if st.checkbox("Use Microphone for English Input"):
|
29 |
-
with audio_input:
|
30 |
-
if microphone_index is None:
|
31 |
-
st.warning("No microphone found. Please make sure your microphone is connected.")
|
32 |
-
else:
|
33 |
-
st.warning("Listening for audio input... Speak in English.")
|
34 |
-
try:
|
35 |
-
with sr.Microphone(device_index=microphone_index) as source:
|
36 |
-
audio = recognizer.listen(source)
|
37 |
-
st.success("Audio input recorded. Translating...")
|
38 |
-
|
39 |
-
# Recognize the English speech
|
40 |
-
english_text = recognizer.recognize_google(audio, language='en')
|
41 |
-
|
42 |
-
# Translate the English text to Hindi
|
43 |
-
out = pipe(english_text, src_lang='en', tgt_lang='hi')
|
44 |
-
|
45 |
-
# Extract the translation
|
46 |
-
translation_text = out[0]['translation_text']
|
47 |
-
st.text(f"English Input: {english_text}")
|
48 |
-
st.text(f"Hindi Translation: {translation_text}")
|
49 |
-
|
50 |
-
# Convert the translated text to speech
|
51 |
-
tts = gTTS(translation_text, lang='hi')
|
52 |
-
tts.save("translated_audio.mp3")
|
53 |
-
|
54 |
-
# Display the audio player for listening to the speech
|
55 |
-
st.audio("translated_audio.mp3", format='audio/mp3')
|
56 |
-
|
57 |
-
except sr.WaitTimeoutError:
|
58 |
-
st.warning("No speech detected. Please speak into the microphone.")
|
59 |
-
except sr.RequestError as e:
|
60 |
-
st.error(f"Could not request results from Google Speech Recognition service: {e}")
|
61 |
-
except sr.UnknownValueError:
|
62 |
-
st.warning("Speech recognition could not understand the audio.")
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
+
from gtts import gTTS # Google Text-to-Speech library
|
4 |
+
import IPython.display as ipd # For playing audio in the notebook
|
|
|
5 |
|
6 |
# Create a translation pipeline
|
7 |
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')
|
8 |
|
9 |
+
text = st.text_area("Enter some English text")
|
10 |
+
if text:
|
11 |
+
out = pipe(text, src_lang='en', tgt_lang='hi')
|
12 |
+
st.json(out)
|
13 |
+
|
14 |
+
# Extract the translated text from the JSON output
|
15 |
+
translation_text = out[0]['translation_text']
|
16 |
+
|
17 |
+
# Convert the translated text to speech
|
18 |
+
tts = gTTS(translation_text, lang='hi') # You can specify the desired language ('hi' for Hindi)
|
19 |
+
|
20 |
+
# Save the generated speech as an audio file (e.g., "translated_audio.mp3")
|
21 |
+
audio_path = "translated_audio.mp3"
|
22 |
+
tts.save(audio_path)
|
23 |
+
|
24 |
+
# Display the audio player for listening to the speech
|
25 |
+
st.audio(audio_path, format='audio/mp3')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|