Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,25 @@
|
|
| 1 |
|
| 2 |
import streamlit as st
|
| 3 |
-
from transformers import
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
target_lang = st.selectbox("Select target language:", get_languages())
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
translation = translate_text(input_text, source_lang, target_lang)
|
| 19 |
-
st.success(f"Translated text: {translation}")
|
| 20 |
-
else:
|
| 21 |
-
st.warning("Please enter text to translate.")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
translation = translator(text, max_length=500)[0]['translation_text']
|
| 30 |
-
return translation
|
| 31 |
-
|
| 32 |
-
if __name__ == "__main__":
|
| 33 |
-
main()
|
| 34 |
-
streamlit run app.py
|
|
|
|
| 1 |
|
| 2 |
import streamlit as st
|
| 3 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 4 |
|
| 5 |
+
# Load the multilingual translation model and tokenizer
|
| 6 |
+
model_name = "facebook/mbart-large-50" # Choose a suitable model
|
| 7 |
+
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
| 8 |
+
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Create the Streamlit app interface
|
| 11 |
+
st.title("Multilingual Translator")
|
| 12 |
|
| 13 |
+
source_text = st.text_area("Enter text to translate")
|
| 14 |
+
target_language = st.selectbox("Choose target language", tokenizer.lang_codes.keys())
|
|
|
|
| 15 |
|
| 16 |
+
if st.button("Translate"):
|
| 17 |
+
translated_text = translate_text(model, tokenizer, source_text, target_language)
|
| 18 |
+
st.write("Translated text:", translated_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Define the translation function
|
| 21 |
+
def translate_text(model, tokenizer, source_text, target_language):
|
| 22 |
+
inputs = tokenizer(source_text, return_tensors="pt")
|
| 23 |
+
outputs = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[target_language])
|
| 24 |
+
translated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 25 |
+
return translated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|