Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,15 @@ import pickle
|
|
| 7 |
|
| 8 |
st.write(f"Using TensorFlow version: {tf.__version__}")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def load_tokenizers():
|
| 11 |
with open("en_tokenizer.pkl", "rb") as f:
|
| 12 |
en_tokenizer = pickle.load(f)
|
|
@@ -23,24 +32,23 @@ def translate_text(text, model, en_tokenizer, fr_tokenizer, en_max_len):
|
|
| 23 |
|
| 24 |
st.title("English to French Translator")
|
| 25 |
st.write("Enter an English sentence, and the model will generate the French translation.")
|
| 26 |
-
try:
|
| 27 |
-
model = load_model("translation_model.h5")
|
| 28 |
-
except:
|
| 29 |
-
pass
|
| 30 |
|
|
|
|
| 31 |
en_tokenizer, fr_tokenizer = load_tokenizers()
|
| 32 |
en_max_len = 55
|
| 33 |
|
| 34 |
-
if "
|
| 35 |
-
st.session_state.
|
| 36 |
|
| 37 |
if st.button("Use Example Sentence"):
|
| 38 |
-
st.session_state.
|
| 39 |
|
| 40 |
-
user_input = st.text_input("Enter an English sentence:", st.session_state.
|
| 41 |
|
| 42 |
if st.button("Translate"):
|
| 43 |
-
if
|
|
|
|
|
|
|
| 44 |
translation = translate_text(user_input, model, en_tokenizer, fr_tokenizer, en_max_len)
|
| 45 |
st.write(f"**French Translation:** {translation}")
|
| 46 |
else:
|
|
|
|
| 7 |
|
| 8 |
st.write(f"Using TensorFlow version: {tf.__version__}")
|
| 9 |
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_translation_model():
|
| 12 |
+
try:
|
| 13 |
+
return load_model("translation_model.h5")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
st.error(f"Error loading model: {e}")
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
@st.cache_resource
|
| 19 |
def load_tokenizers():
|
| 20 |
with open("en_tokenizer.pkl", "rb") as f:
|
| 21 |
en_tokenizer = pickle.load(f)
|
|
|
|
| 32 |
|
| 33 |
st.title("English to French Translator")
|
| 34 |
st.write("Enter an English sentence, and the model will generate the French translation.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
model = load_translation_model()
|
| 37 |
en_tokenizer, fr_tokenizer = load_tokenizers()
|
| 38 |
en_max_len = 55
|
| 39 |
|
| 40 |
+
if "user_input" not in st.session_state:
|
| 41 |
+
st.session_state.user_input = ""
|
| 42 |
|
| 43 |
if st.button("Use Example Sentence"):
|
| 44 |
+
st.session_state.user_input = "This world is bad"
|
| 45 |
|
| 46 |
+
user_input = st.text_input("Enter an English sentence:", value=st.session_state.user_input)
|
| 47 |
|
| 48 |
if st.button("Translate"):
|
| 49 |
+
if model is None:
|
| 50 |
+
st.write("Error: Model not loaded.")
|
| 51 |
+
elif user_input:
|
| 52 |
translation = translate_text(user_input, model, en_tokenizer, fr_tokenizer, en_max_len)
|
| 53 |
st.write(f"**French Translation:** {translation}")
|
| 54 |
else:
|