Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Streamlit UI
|
| 11 |
st.title("Language Translator")
|
|
@@ -16,25 +22,14 @@ input_lang = st.sidebar.selectbox("Select Input Language", ["English", "Urdu", "
|
|
| 16 |
output_lang = st.sidebar.selectbox("Select Output Language", ["Urdu", "English", "French", "Spanish", "German"])
|
| 17 |
|
| 18 |
# Text input
|
| 19 |
-
input_text = st.text_area("Enter Text to Translate",
|
| 20 |
|
| 21 |
# Translation logic
|
| 22 |
translator = None
|
| 23 |
if st.button("Translate"):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
translator = load_translation_model("Helsinki-NLP/opus-mt-ur-en")
|
| 28 |
-
elif input_lang == "English" and output_lang == "French":
|
| 29 |
-
translator = pipeline("translation_en_to_fr")
|
| 30 |
-
elif input_lang == "French" and output_lang == "English":
|
| 31 |
-
translator = pipeline("translation_fr_to_en")
|
| 32 |
-
elif input_lang == "English" and output_lang == "German":
|
| 33 |
-
translator = pipeline("translation_en_to_de")
|
| 34 |
-
elif input_lang == "German" and output_lang == "English":
|
| 35 |
-
translator = pipeline("translation_de_to_en")
|
| 36 |
-
|
| 37 |
-
if translator:
|
| 38 |
translation = translator(input_text)
|
| 39 |
st.write("Translated Text:")
|
| 40 |
st.write(translation[0]['translation_text'])
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 3 |
|
| 4 |
+
# Preload translation models
|
| 5 |
+
models = {
|
| 6 |
+
"en-ur": pipeline("translation", model=MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-en-ur"),
|
| 7 |
+
tokenizer=MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ur")),
|
| 8 |
+
"ur-en": pipeline("translation", model=MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ur-en"),
|
| 9 |
+
tokenizer=MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ur-en")),
|
| 10 |
+
"en-fr": pipeline("translation_en_to_fr"),
|
| 11 |
+
"fr-en": pipeline("translation_fr_to_en"),
|
| 12 |
+
"en-de": pipeline("translation_en_to_de"),
|
| 13 |
+
"de-en": pipeline("translation_de_to_en")
|
| 14 |
+
}
|
| 15 |
|
| 16 |
# Streamlit UI
|
| 17 |
st.title("Language Translator")
|
|
|
|
| 22 |
output_lang = st.sidebar.selectbox("Select Output Language", ["Urdu", "English", "French", "Spanish", "German"])
|
| 23 |
|
| 24 |
# Text input
|
| 25 |
+
input_text = st.text_area("Enter Text to Translate",placeholder = "Type here...")
|
| 26 |
|
| 27 |
# Translation logic
|
| 28 |
translator = None
|
| 29 |
if st.button("Translate"):
|
| 30 |
+
model_key = f"{input_lang.lower()[0:2]}-{output_lang.lower()[0:2]}"
|
| 31 |
+
if model_key in models:
|
| 32 |
+
translator = models[model_key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
translation = translator(input_text)
|
| 34 |
st.write("Translated Text:")
|
| 35 |
st.write(translation[0]['translation_text'])
|