Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
|
| 4 |
+
# Initialize the MarianMT model and tokenizer
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_model_and_tokenizer(model_name):
|
| 7 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 8 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 9 |
+
return model, tokenizer
|
| 10 |
+
|
| 11 |
+
# Language selection options (you can expand this list as needed)
|
| 12 |
+
language_options = {
|
| 13 |
+
"English to French": ("en", "fr"),
|
| 14 |
+
"French to English": ("fr", "en"),
|
| 15 |
+
"English to Spanish": ("en", "es"),
|
| 16 |
+
"Spanish to English": ("es", "en"),
|
| 17 |
+
"English to German": ("en", "de"),
|
| 18 |
+
"German to English": ("de", "en"),
|
| 19 |
+
"English to Italian": ("en", "it"),
|
| 20 |
+
"Italian to English": ("it", "en"),
|
| 21 |
+
# Add more language pairs as needed
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Streamlit UI setup
|
| 25 |
+
st.title("Hugging Face Translation App")
|
| 26 |
+
st.write("This app allows you to translate text between different languages using Hugging Face's translation models.")
|
| 27 |
+
|
| 28 |
+
# Select the language pair
|
| 29 |
+
language_pair = st.selectbox("Select language pair", list(language_options.keys()))
|
| 30 |
+
src_lang, tgt_lang = language_options[language_pair]
|
| 31 |
+
|
| 32 |
+
# Load the corresponding model and tokenizer
|
| 33 |
+
model_name = f"Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}"
|
| 34 |
+
try:
|
| 35 |
+
model, tokenizer = load_model_and_tokenizer(model_name)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error(f"Error loading model {model_name}: {e}")
|
| 38 |
+
st.stop() # Stop execution if the model loading fails
|
| 39 |
+
|
| 40 |
+
# Input text to translate
|
| 41 |
+
text = st.text_area("Enter text to translate")
|
| 42 |
+
|
| 43 |
+
# Translate button
|
| 44 |
+
if st.button("Translate"):
|
| 45 |
+
if text.strip():
|
| 46 |
+
# Prepare the input for the model
|
| 47 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 48 |
+
|
| 49 |
+
# Generate translation
|
| 50 |
+
translation = model.generate(**inputs)
|
| 51 |
+
|
| 52 |
+
# Decode the output
|
| 53 |
+
translated_text = tokenizer.decode(translation[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
# Display translation
|
| 56 |
+
st.write(f"**Original Text**: {text}")
|
| 57 |
+
st.write(f"**Translated Text**: {translated_text}")
|
| 58 |
+
else:
|
| 59 |
+
st.warning("Please enter some text to translate.")
|