Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# Map of language codes to full language names and flag images
|
| 6 |
+
language_dict = {
|
| 7 |
+
"en": "English",
|
| 8 |
+
"de": "German",
|
| 9 |
+
"fr": "French",
|
| 10 |
+
"es": "Spanish",
|
| 11 |
+
"it": "Italian",
|
| 12 |
+
"nl": "Dutch",
|
| 13 |
+
"pt": "Portuguese",
|
| 14 |
+
"ro": "Romanian",
|
| 15 |
+
"ru": "Russian",
|
| 16 |
+
"zh": "Chinese"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Reverse map for language selection
|
| 20 |
+
language_codes = {v: k for k, v in language_dict.items()}
|
| 21 |
+
|
| 22 |
+
# Add custom CSS for styling
|
| 23 |
+
st.markdown("""
|
| 24 |
+
<style>
|
| 25 |
+
.stTextInput, .stTextArea {
|
| 26 |
+
background-color: #f4f6f8;
|
| 27 |
+
padding: 10px;
|
| 28 |
+
border-radius: 8px;
|
| 29 |
+
}
|
| 30 |
+
.stButton {
|
| 31 |
+
background-color: #4CAF50;
|
| 32 |
+
color: black;
|
| 33 |
+
padding: 10px 20px;
|
| 34 |
+
border-radius: 5px;
|
| 35 |
+
}
|
| 36 |
+
.stButton:hover {
|
| 37 |
+
background-color: #45a049;
|
| 38 |
+
}
|
| 39 |
+
.stText {
|
| 40 |
+
font-size: 1.2em;
|
| 41 |
+
font-family: 'Helvetica', sans-serif;
|
| 42 |
+
}
|
| 43 |
+
.footer {
|
| 44 |
+
padding: 10px;
|
| 45 |
+
background-color: #f1f1f1;
|
| 46 |
+
text-align: center;
|
| 47 |
+
}
|
| 48 |
+
h1 {
|
| 49 |
+
color: #4CAF50;
|
| 50 |
+
}
|
| 51 |
+
</style>
|
| 52 |
+
""", unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
# Function to load model and tokenizer
|
| 55 |
+
def load_translation_model(src_lang, tgt_lang):
|
| 56 |
+
model_name = f'Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}'
|
| 57 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 58 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 59 |
+
return model, tokenizer
|
| 60 |
+
|
| 61 |
+
# Function to translate text
|
| 62 |
+
def translate_text(text, src_lang, tgt_lang):
|
| 63 |
+
model, tokenizer = load_translation_model(src_lang, tgt_lang)
|
| 64 |
+
|
| 65 |
+
# Tokenize the input text
|
| 66 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 67 |
+
|
| 68 |
+
# Translate the text
|
| 69 |
+
translated = model.generate(**inputs)
|
| 70 |
+
|
| 71 |
+
# Decode the translated text
|
| 72 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 73 |
+
|
| 74 |
+
return translated_text
|
| 75 |
+
|
| 76 |
+
# Language selector with flags and names
|
| 77 |
+
def language_selector():
|
| 78 |
+
# Use two columns to display the language selectors
|
| 79 |
+
col1, col2 = st.columns(2)
|
| 80 |
+
|
| 81 |
+
with col1:
|
| 82 |
+
source_language = st.selectbox(
|
| 83 |
+
"Select source language",
|
| 84 |
+
list(language_dict.values()),
|
| 85 |
+
key="source_language"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
with col2:
|
| 89 |
+
target_language = st.selectbox(
|
| 90 |
+
"Select target language",
|
| 91 |
+
list(language_dict.values()),
|
| 92 |
+
key="target_language"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return language_codes[source_language], language_codes[target_language]
|
| 96 |
+
|
| 97 |
+
# Main application function
|
| 98 |
+
def run_app():
|
| 99 |
+
st.title("π Interactive Language Translation App")
|
| 100 |
+
|
| 101 |
+
st.subheader("Translate your text in real-time with ease! π")
|
| 102 |
+
|
| 103 |
+
# Select source and target languages
|
| 104 |
+
src_lang_code, tgt_lang_code = language_selector()
|
| 105 |
+
|
| 106 |
+
# Text input field with placeholder
|
| 107 |
+
text_input = st.text_area("Enter text to translate", placeholder="Type here...", height=150)
|
| 108 |
+
|
| 109 |
+
# Option to upload a text file for input
|
| 110 |
+
uploaded_file = st.file_uploader("Or upload a text file", type=["txt"])
|
| 111 |
+
|
| 112 |
+
if uploaded_file is not None:
|
| 113 |
+
text_input = uploaded_file.read().decode("utf-8")
|
| 114 |
+
st.text_area("File content", value=text_input, height=150)
|
| 115 |
+
|
| 116 |
+
# Button for translation
|
| 117 |
+
if st.button("π Translate", key="translate"):
|
| 118 |
+
if text_input:
|
| 119 |
+
with st.spinner("π Translating... Please wait..."):
|
| 120 |
+
# Translate the text
|
| 121 |
+
translated_text = translate_text(text_input, src_lang_code, tgt_lang_code)
|
| 122 |
+
# Display translated text directly in the success container
|
| 123 |
+
st.success(f"β
Translation: {translated_text}")
|
| 124 |
+
st.balloons() # Show animation when translation is complete
|
| 125 |
+
else:
|
| 126 |
+
st.warning("β οΈ Please enter or upload some text to translate.")
|
| 127 |
+
|
| 128 |
+
# Run the Streamlit app
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
run_app()
|