Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from transformers import pipeline | |
| # Language pair to model mapping | |
| model_mapping = { | |
| ("English", "Hindi"):"Helsinki-NLP/opus-mt-en-hi", # "Helsinki-NLP/opus-mt-en-hi", | |
| ("Hindi", "English"): "TestZee/Finetuned-hindi-to-english-V5", # "TestZee/Finetuned-hindi-to-english-V5", | |
| ("English", "Telugu"):"hima06varshini/TeluguTranslatica", # "harshitha2406/English_to_Telugu",not working | |
| ("Telugu", "English"):"hima06varshini/TeluguTranslatica", # "ai4bharat/indic-translator-te-en",not working | |
| } | |
| # Streamlit page config | |
| st.set_page_config(page_title="Translator", layout="wide") | |
| # Only allowed languages | |
| languages = ["English", "Hindi", "Telugu"] | |
| # UI | |
| st.markdown(""" | |
| <style> | |
| .big-font { font-size:25px !important; font-weight: bold; } | |
| .output-box { background-color: #f9f9f9; padding: 20px; border-radius: 10px; min-height: 300px; font-size: 22px; } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Two columns | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| source_lang = st.selectbox("Detected Language", languages, index=0) | |
| st.markdown('<div class="big-font">Enter your text:</div>', unsafe_allow_html=True) | |
| user_input = st.text_area("", "hi how are you", height=250) | |
| translate_button = st.button("Translate", use_container_width=True) | |
| with col2: | |
| target_lang = st.selectbox("Translate To", languages, index=1) | |
| if translate_button and user_input: | |
| if (source_lang, target_lang) in model_mapping: | |
| model_name = model_mapping[(source_lang, target_lang)] | |
| translator = pipeline("translation", model=model_name) | |
| translated_text = translator(user_input, max_length=400)[0]['translation_text'] | |
| else: | |
| translated_text = "β Translation not available for selected language pair." | |
| else: | |
| translated_text = "" | |
| st.markdown('<div class="output-box">' + (translated_text if translated_text else "") + '</div>', unsafe_allow_html=True) | |
| if translated_text and "β" not in translated_text: | |
| st.button("Copy", use_container_width=True) | |
| st.button("Paraphrase", use_container_width=True) | |