Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,23 @@ import streamlit as st
|
|
| 2 |
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 3 |
|
| 4 |
# Preload translation models
|
| 5 |
-
|
| 6 |
-
"en-ur":
|
| 7 |
-
|
| 8 |
-
"
|
| 9 |
-
|
| 10 |
-
"en-
|
| 11 |
-
"
|
| 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")
|
| 18 |
|
|
@@ -22,10 +28,9 @@ input_lang = st.sidebar.selectbox("Select Input Language", ["English", "Urdu", "
|
|
| 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 =
|
| 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:
|
|
|
|
| 2 |
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 3 |
|
| 4 |
# Preload translation models
|
| 5 |
+
model_names = {
|
| 6 |
+
"en-ur": "Helsinki-NLP/opus-mt-en-ur",
|
| 7 |
+
"ur-en": "Helsinki-NLP/opus-mt-ur-en",
|
| 8 |
+
"en-fr": "Helsinki-NLP/opus-mt-en-fr",
|
| 9 |
+
"fr-en": "Helsinki-NLP/opus-mt-fr-en",
|
| 10 |
+
"en-de": "Helsinki-NLP/opus-mt-en-de",
|
| 11 |
+
"de-en": "Helsinki-NLP/opus-mt-de-en"
|
|
|
|
|
|
|
| 12 |
}
|
| 13 |
|
| 14 |
+
models = {}
|
| 15 |
+
|
| 16 |
+
# Load models once at the start
|
| 17 |
+
for key, model_name in model_names.items():
|
| 18 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 19 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 20 |
+
models[key] = pipeline("translation", model=model, tokenizer=tokenizer)
|
| 21 |
+
|
| 22 |
# Streamlit UI
|
| 23 |
st.title("Language Translator")
|
| 24 |
|
|
|
|
| 28 |
output_lang = st.sidebar.selectbox("Select Output Language", ["Urdu", "English", "French", "Spanish", "German"])
|
| 29 |
|
| 30 |
# Text input
|
| 31 |
+
input_text = st.text_area("Enter Text to Translate", placeholder = "Type here...")
|
| 32 |
|
| 33 |
# Translation logic
|
|
|
|
| 34 |
if st.button("Translate"):
|
| 35 |
model_key = f"{input_lang.lower()[0:2]}-{output_lang.lower()[0:2]}"
|
| 36 |
if model_key in models:
|