Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import os
|
4 |
+
from gtts import gTTS
|
5 |
+
|
6 |
+
# Initialize the Hugging Face model pipeline
|
7 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr") # Example for English to French translation
|
8 |
+
|
9 |
+
# Function to translate text
|
10 |
+
def translate_text(text, src_lang, target_lang):
|
11 |
+
# Translate using the Hugging Face pipeline
|
12 |
+
translation = translator(text, src_lang=src_lang, tgt_lang=target_lang)
|
13 |
+
return translation[0]['translation_text']
|
14 |
+
|
15 |
+
# Function to convert text to speech
|
16 |
+
def text_to_speech(text, lang='en'):
|
17 |
+
tts = gTTS(text=text, lang=lang, slow=False)
|
18 |
+
tts.save("output.mp3")
|
19 |
+
return "output.mp3"
|
20 |
+
|
21 |
+
# Streamlit UI
|
22 |
+
st.title("Multilingual Text Translator and Text-to-Speech")
|
23 |
+
st.write("Enter the text below to translate and convert to speech")
|
24 |
+
|
25 |
+
# Text input for the user
|
26 |
+
input_text = st.text_area("Enter text to translate:")
|
27 |
+
|
28 |
+
# Language selection
|
29 |
+
src_lang = st.selectbox("Select source language", ["en", "fr", "es", "de", "it"])
|
30 |
+
target_lang = st.selectbox("Select target language", ["fr", "en", "es", "de", "it"])
|
31 |
+
|
32 |
+
if st.button("Translate and Convert to Speech"):
|
33 |
+
if input_text:
|
34 |
+
# Translate the text
|
35 |
+
translated_text = translate_text(input_text, src_lang, target_lang)
|
36 |
+
st.write(f"Translated Text: {translated_text}")
|
37 |
+
|
38 |
+
# Convert the translated text to speech
|
39 |
+
audio_file = text_to_speech(translated_text, lang=target_lang)
|
40 |
+
st.audio(audio_file)
|
41 |
+
|
42 |
+
# Remove the generated audio file after use
|
43 |
+
os.remove(audio_file)
|
44 |
+
else:
|
45 |
+
st.error("Please enter text to translate.")
|