|
import streamlit as st |
|
from transformers import MarianMTModel, MarianTokenizer |
|
from datasets import load_dataset |
|
from gtts import gTTS |
|
import os |
|
|
|
|
|
st.title("Text Translator with Voice") |
|
|
|
|
|
user_text = st.text_input("Enter the text you want to translate:") |
|
|
|
|
|
language_names = { |
|
'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am', 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az', 'Basque': 'eu', |
|
'Belarusian': 'be', 'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg', 'Catalan': 'ca', 'Cebuano': 'ceb', 'Chichewa': 'ny', |
|
'Chinese (Simplified)': 'zh-cn', 'Corsican': 'co', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'English': 'en', |
|
'Esperanto': 'eo', 'Estonian': 'et', 'Filipino': 'tl', 'Finnish': 'fi', 'French': 'fr', 'Frisian': 'fy', 'Galician': 'gl', |
|
'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu', 'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hawaiian': 'haw', |
|
'Hebrew': 'iw', 'Hindi': 'hi', 'Hmong': 'hmn', 'Hungarian': 'hu', 'Icelandic': 'is', 'Igbo': 'ig', 'Indonesian': 'id', |
|
'Irish': 'ga', 'Italian': 'it', 'Japanese': 'ja', 'Javanese': 'jw', 'Kannada': 'kn', 'Kazakh': 'kk', 'Khmer': 'km', |
|
'Korean': 'ko', 'Kurdish': 'ku', 'Kyrgyz': 'ky', 'Lao': 'lo', 'Latin': 'la', 'Latvian': 'lv', 'Lithuanian': 'lt', |
|
'Luxembourgish': 'lb', 'Macedonian': 'mk', 'Malagasy': 'mg', 'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt', 'Maori': 'mi', |
|
'Marathi': 'mr', 'Mongolian': 'mn', 'Myanmar (Burmese)': 'my', 'Nepali': 'ne', 'Norwegian': 'no', 'Odia': 'or', |
|
'Pashto': 'ps', 'Persian': 'fa', 'Polish': 'pl', 'Portuguese': 'pt', 'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru', |
|
'Samoan': 'sm', 'Scots Gaelic': 'gd', 'Serbian': 'sr', 'Sesotho': 'st', 'Shona': 'sn', 'Sindhi': 'sd', 'Sinhala': 'si', |
|
'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es', 'Sundanese': 'su', 'Swahili': 'sw', 'Swedish': 'sv', |
|
'Tajik': 'tg', 'Tamil': 'ta', 'Tatar': 'tt', 'Telugu': 'te', 'Thai': 'th', 'Turkish': 'tr', 'Turkmen': 'tk', 'Ukrainian': 'uk', |
|
'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy', 'Xhosa': 'xh', 'Yiddish': 'yi', 'Yoruba': 'yo', |
|
'Zulu': 'zu' |
|
} |
|
|
|
|
|
def load_translation_model(language_pair): |
|
model_name = f'Helsinki-NLP/opus-mt-{language_pair}' |
|
model = MarianMTModel.from_pretrained(model_name) |
|
tokenizer = MarianTokenizer.from_pretrained(model_name) |
|
return model, tokenizer |
|
|
|
|
|
def translate_text(text, target_language_code): |
|
try: |
|
|
|
language_pair = f'en-{target_language_code}' |
|
model, tokenizer = load_translation_model(language_pair) |
|
|
|
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True) |
|
|
|
|
|
translated_tokens = model.generate(**inputs) |
|
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True) |
|
|
|
return translated_text |
|
except Exception as e: |
|
return f"Translation failed: {str(e)}" |
|
|
|
|
|
st.subheader("Select the target language for translation:") |
|
selected_target_language = st.selectbox("Select a target language:", list(language_names.keys())) |
|
|
|
|
|
if user_text: |
|
translated_text = translate_text(user_text, language_names[selected_target_language]) |
|
|
|
|
|
styled_text = f'<div style="border: 2px solid #0072B5; padding: 10px; background-color: #E5E5E5; border-radius: 5px; color: black;">{translated_text}</div>' |
|
st.markdown(styled_text, unsafe_allow_html=True) |
|
|
|
|
|
tts = gTTS(translated_text, lang=language_names[selected_target_language]) |
|
tts_file_path = "translated_audio.mp3" |
|
tts.save(tts_file_path) |
|
|
|
|
|
st.audio(tts_file_path, format="audio/mp3") |
|
|
|
|
|
os.remove(tts_file_path) |