Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,31 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
-
from transformers import
|
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 |
-
|
14 |
-
target_language = st.selectbox("Choose target language", tokenizer.lang_codes.keys())
|
15 |
|
16 |
-
|
17 |
-
|
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
|
26 |
-
from transformers import AutoConfig, AutoModelForSeq2SeqLM, AutoTokenizer
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
AutoModelForSeq2SeqLM.from_pretrained(model_name).clear_cache()
|
31 |
-
AutoTokenizer.from_pretrained(model_name).clear_cache()
|
32 |
|
|
|
|
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
st.title("Multilingual Translator Chatbot")
|
|
|
5 |
|
6 |
+
# Choose the translation model from Hugging Face
|
7 |
+
translation_model = st.selectbox("Select translation model", ["Helsinki-NLP/opus-mt-en-de", "Helsinki-NLP/opus-mt-en-fr"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Load the translation pipeline
|
10 |
+
translator = pipeline(task="translation", model=translation_model)
|
|
|
|
|
11 |
|
12 |
+
# User input for translation
|
13 |
+
user_input = st.text_area("Enter text for translation:", "")
|
14 |
|
15 |
+
if st.button("Translate"):
|
16 |
+
if user_input:
|
17 |
+
# Perform translation
|
18 |
+
translated_text = translator(user_input, max_length=500)[0]['translation_text']
|
19 |
+
st.success(f"Translated Text: {translated_text}")
|
20 |
+
else:
|
21 |
+
st.warning("Please enter text for translation.")
|
22 |
+
|
23 |
+
st.markdown("---")
|
24 |
+
|
25 |
+
st.subheader("About")
|
26 |
+
st.write(
|
27 |
+
"This is a simple Multilingual Translator chatbot that uses the Hugging Face Transformers library."
|
28 |
+
)
|
29 |
+
st.write(
|
30 |
+
"Select a translation model from the dropdown, enter text, and click 'Translate' to see the translation."
|
31 |
+
)
|