Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Language pair to model mapping
|
| 5 |
+
model_mapping = {
|
| 6 |
+
("English", "Hindi"):"Helsinki-NLP/opus-mt-en-hi", # "Helsinki-NLP/opus-mt-en-hi",
|
| 7 |
+
("Hindi", "English"): "TestZee/Finetuned-hindi-to-english-V5", # "TestZee/Finetuned-hindi-to-english-V5",
|
| 8 |
+
("English", "Telugu"):"hima06varshini/TeluguTranslatica", # "harshitha2406/English_to_Telugu",not working
|
| 9 |
+
("Telugu", "English"):"hima06varshini/TeluguTranslatica", # "ai4bharat/indic-translator-te-en",not working
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# Streamlit page config
|
| 13 |
+
st.set_page_config(page_title="Translator", layout="wide")
|
| 14 |
+
|
| 15 |
+
# Only allowed languages
|
| 16 |
+
languages = ["English", "Hindi", "Telugu"]
|
| 17 |
+
|
| 18 |
+
# UI
|
| 19 |
+
st.markdown("""
|
| 20 |
+
<style>
|
| 21 |
+
.big-font { font-size:25px !important; font-weight: bold; }
|
| 22 |
+
.output-box { background-color: #f9f9f9; padding: 20px; border-radius: 10px; min-height: 300px; font-size: 22px; }
|
| 23 |
+
</style>
|
| 24 |
+
""", unsafe_allow_html=True)
|
| 25 |
+
|
| 26 |
+
# Two columns
|
| 27 |
+
col1, col2 = st.columns(2)
|
| 28 |
+
|
| 29 |
+
with col1:
|
| 30 |
+
source_lang = st.selectbox("Detected Language", languages, index=0)
|
| 31 |
+
st.markdown('<div class="big-font">Enter your text:</div>', unsafe_allow_html=True)
|
| 32 |
+
user_input = st.text_area("", "hi how are you", height=250)
|
| 33 |
+
translate_button = st.button("Translate", use_container_width=True)
|
| 34 |
+
|
| 35 |
+
with col2:
|
| 36 |
+
target_lang = st.selectbox("Translate To", languages, index=1)
|
| 37 |
+
|
| 38 |
+
if translate_button and user_input:
|
| 39 |
+
if (source_lang, target_lang) in model_mapping:
|
| 40 |
+
model_name = model_mapping[(source_lang, target_lang)]
|
| 41 |
+
translator = pipeline("translation", model=model_name)
|
| 42 |
+
translated_text = translator(user_input, max_length=400)[0]['translation_text']
|
| 43 |
+
else:
|
| 44 |
+
translated_text = "❌ Translation not available for selected language pair."
|
| 45 |
+
else:
|
| 46 |
+
translated_text = ""
|
| 47 |
+
|
| 48 |
+
st.markdown('<div class="output-box">' + (translated_text if translated_text else "") + '</div>', unsafe_allow_html=True)
|
| 49 |
+
|
| 50 |
+
if translated_text and "❌" not in translated_text:
|
| 51 |
+
st.button("Copy", use_container_width=True)
|
| 52 |
+
st.button("Paraphrase", use_container_width=True)
|