Spaces:
Sleeping
Sleeping
AreesaAshfaq
commited on
Commit
•
2486975
1
Parent(s):
4438826
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,26 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
'German': 'de',
|
13 |
-
'Italian': 'it',
|
14 |
-
'Portuguese': 'pt',
|
15 |
-
'Spanish': 'es',
|
16 |
-
'Chinese': 'zh',
|
17 |
-
'Japanese': 'ja',
|
18 |
-
'Korean': 'ko',
|
19 |
-
'Russian': 'ru',
|
20 |
-
'Arabic': 'ar',
|
21 |
-
'Turkish': 'tr',
|
22 |
-
'Hindi': 'hi',
|
23 |
-
'Swahili': 'sw',
|
24 |
-
# Add more languages if needed
|
25 |
}
|
26 |
|
27 |
-
def
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
inputs = tokenizer.encode(text, return_tensors="pt")
|
30 |
-
|
31 |
-
translated = model.generate(inputs, decoder_start_token_id=tokenizer.lang_code_to_id[target_lang_code])
|
32 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
33 |
return translated_text
|
34 |
|
@@ -39,12 +31,15 @@ def main():
|
|
39 |
text_to_translate = st.text_area("Enter text in English:")
|
40 |
|
41 |
# Language options
|
42 |
-
selected_language = st.selectbox("Select target language:", list(
|
43 |
|
44 |
if st.button("Translate"):
|
45 |
if text_to_translate:
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
48 |
st.write(f"**Translation in {selected_language}:**")
|
49 |
st.write(translated_text)
|
50 |
else:
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
+
# Define a dictionary to map language names to model identifiers
|
5 |
+
models = {
|
6 |
+
'French': 'Helsinki-NLP/opus-mt-en-fr',
|
7 |
+
'Spanish': 'Helsinki-NLP/opus-mt-en-es',
|
8 |
+
'German': 'Helsinki-NLP/opus-mt-en-de',
|
9 |
+
'Italian': 'Helsinki-NLP/opus-mt-en-it',
|
10 |
+
'Portuguese': 'Helsinki-NLP/opus-mt-en-pt',
|
11 |
+
# Add more language models if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
|
14 |
+
def load_model(model_name):
|
15 |
+
"""Load the model and tokenizer based on the selected model name."""
|
16 |
+
model = MarianMTModel.from_pretrained(model_name)
|
17 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
18 |
+
return model, tokenizer
|
19 |
+
|
20 |
+
def translate_text(text, model, tokenizer):
|
21 |
+
"""Translate text using the provided model and tokenizer."""
|
22 |
inputs = tokenizer.encode(text, return_tensors="pt")
|
23 |
+
translated = model.generate(inputs)
|
|
|
24 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
25 |
return translated_text
|
26 |
|
|
|
31 |
text_to_translate = st.text_area("Enter text in English:")
|
32 |
|
33 |
# Language options
|
34 |
+
selected_language = st.selectbox("Select target language:", list(models.keys()))
|
35 |
|
36 |
if st.button("Translate"):
|
37 |
if text_to_translate:
|
38 |
+
# Load the selected model
|
39 |
+
model_name = models[selected_language]
|
40 |
+
model, tokenizer = load_model(model_name)
|
41 |
+
|
42 |
+
translated_text = translate_text(text_to_translate, model, tokenizer)
|
43 |
st.write(f"**Translation in {selected_language}:**")
|
44 |
st.write(translated_text)
|
45 |
else:
|