Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 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 = {
|
|
@@ -51,6 +50,10 @@ st.markdown("""
|
|
| 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}'
|
|
@@ -60,6 +63,9 @@ def load_translation_model(src_lang, tgt_lang):
|
|
| 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
|
|
@@ -116,12 +122,15 @@ def run_app():
|
|
| 116 |
# Button for translation
|
| 117 |
if st.button("π Translate", key="translate"):
|
| 118 |
if text_input:
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
else:
|
| 126 |
st.warning("β οΈ Please enter or upload some text to translate.")
|
| 127 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import MarianMTModel, MarianTokenizer
|
|
|
|
| 3 |
|
| 4 |
# Map of language codes to full language names and flag images
|
| 5 |
language_dict = {
|
|
|
|
| 50 |
</style>
|
| 51 |
""", unsafe_allow_html=True)
|
| 52 |
|
| 53 |
+
# Custom Exception for Same Source and Target Language
|
| 54 |
+
class SameLanguageException(Exception):
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
# Function to load model and tokenizer
|
| 58 |
def load_translation_model(src_lang, tgt_lang):
|
| 59 |
model_name = f'Helsinki-NLP/opus-mt-{src_lang}-{tgt_lang}'
|
|
|
|
| 63 |
|
| 64 |
# Function to translate text
|
| 65 |
def translate_text(text, src_lang, tgt_lang):
|
| 66 |
+
if src_lang == tgt_lang:
|
| 67 |
+
raise SameLanguageException("Source and target languages must be different.")
|
| 68 |
+
|
| 69 |
model, tokenizer = load_translation_model(src_lang, tgt_lang)
|
| 70 |
|
| 71 |
# Tokenize the input text
|
|
|
|
| 122 |
# Button for translation
|
| 123 |
if st.button("π Translate", key="translate"):
|
| 124 |
if text_input:
|
| 125 |
+
try:
|
| 126 |
+
with st.spinner("π Translating... Please wait..."):
|
| 127 |
+
# Translate the text
|
| 128 |
+
translated_text = translate_text(text_input, src_lang_code, tgt_lang_code)
|
| 129 |
+
# Display translated text directly in the success container
|
| 130 |
+
st.success(f"β
Translation: {translated_text}")
|
| 131 |
+
st.balloons() # Show animation when translation is complete
|
| 132 |
+
except SameLanguageException as e:
|
| 133 |
+
st.error(f"β Error: {str(e)}")
|
| 134 |
else:
|
| 135 |
st.warning("β οΈ Please enter or upload some text to translate.")
|
| 136 |
|