File size: 923 Bytes
dd421a7
 
8d593f1
 
7f855a0
c505931
7f855a0
 
8d593f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import streamlit as st
from transformers import pipeline
from gtts import gTTS  # Google Text-to-Speech library
import IPython.display as ipd  # For playing audio in the notebook

# Create a translation pipeline
pipe = pipeline('translation', model='Helsinki-NLP/opus-mt-en-hi')

text = st.text_area("Enter some English text")
if text:
    out = pipe(text, src_lang='en', tgt_lang='hi')
    st.json(out)
    
    # Extract the translated text from the JSON output
    translation_text = out[0]['translation_text']
    
    # Convert the translated text to speech
    tts = gTTS(translation_text, lang='hi')  # You can specify the desired language ('hi' for Hindi)
    
    # Save the generated speech as an audio file (e.g., "translated_audio.mp3")
    audio_path = "translated_audio.mp3"
    tts.save(audio_path)
    
    # Display the audio player for listening to the speech
    st.audio(audio_path, format='audio/mp3')