Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,48 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
5 |
LANGUAGES = {
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
}
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
# Generate translations
|
22 |
-
translated = model.generate(**inputs)
|
23 |
-
# Decode the output
|
24 |
-
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
25 |
-
return translated_text
|
26 |
-
except Exception as e:
|
27 |
-
return f"Error: {str(e)}"
|
28 |
|
29 |
# Streamlit app
|
30 |
-
st.title(
|
31 |
|
32 |
-
#
|
33 |
-
text_to_translate = st.text_area("Enter text
|
34 |
-
|
35 |
|
36 |
-
|
|
|
37 |
if text_to_translate:
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
else:
|
42 |
-
st.
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import MarianMTModel, MarianTokenizer
|
3 |
|
4 |
+
# Load pre-trained MarianMT model and tokenizer
|
5 |
+
model_name = "Helsinki-NLP/opus-mt-en-xx" # Multilingual model supporting various languages
|
6 |
+
model = MarianMTModel.from_pretrained(model_name)
|
7 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Supported languages and their target language codes
|
10 |
LANGUAGES = {
|
11 |
+
'French': 'fr',
|
12 |
+
'Spanish': 'es',
|
13 |
+
'German': 'de',
|
14 |
+
'Chinese': 'zh',
|
15 |
+
'Russian': 'ru',
|
16 |
+
'Japanese': 'ja',
|
17 |
+
'Arabic': 'ar',
|
18 |
+
'Urdu': 'ur',
|
19 |
+
'Hindi': 'hi',
|
20 |
+
'Bengali': 'bn',
|
21 |
}
|
22 |
|
23 |
+
def translate_text(text, target_lang):
|
24 |
+
# Encode the text and prepare it for translation
|
25 |
+
encoded_text = tokenizer(text, return_tensors="pt")
|
26 |
+
# Translate text
|
27 |
+
translated = model.generate(**encoded_text, forced_bos_token_id=tokenizer.get_lang_id(target_lang))
|
28 |
+
# Decode the translated text
|
29 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
30 |
+
return translated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Streamlit app
|
33 |
+
st.title('Language Translator')
|
34 |
|
35 |
+
# Input text and language selection
|
36 |
+
text_to_translate = st.text_area("Enter text to translate")
|
37 |
+
target_language = st.selectbox("Select target language", list(LANGUAGES.keys()))
|
38 |
|
39 |
+
# Translate button
|
40 |
+
if st.button('Translate'):
|
41 |
if text_to_translate:
|
42 |
+
if target_language in LANGUAGES:
|
43 |
+
translated_text = translate_text(text_to_translate, LANGUAGES[target_language])
|
44 |
+
st.write(f"Translated text ({target_language}): {translated_text}")
|
45 |
+
else:
|
46 |
+
st.error("Target language not supported.")
|
47 |
else:
|
48 |
+
st.error("Please enter text to translate.")
|