torch import
Browse files
app.py
CHANGED
@@ -1,60 +1,61 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
-
from datasets import Dataset
|
3 |
-
import streamlit as st
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
st.
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
text_input =
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
st.markdown(
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from datasets import Dataset
|
3 |
+
import streamlit as st
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Set the background color and layout with set_page_config
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="English to Tawra Translator",
|
9 |
+
page_icon=":repeat:",
|
10 |
+
layout="wide",
|
11 |
+
)
|
12 |
+
|
13 |
+
# Streamlit app setup
|
14 |
+
st.title(":repeat: English to Tawra Translator")
|
15 |
+
st.markdown("Welcome to the English to Tawra Translator. :sparkles: Simply enter your text in English, and get the translation in Tawra instantly! :thumbsup:")
|
16 |
+
|
17 |
+
# Text input
|
18 |
+
if 'text_input' not in st.session_state:
|
19 |
+
st.session_state.text_input = ""
|
20 |
+
text_input = st.text_area("Enter English text to translate", height=150, value=st.session_state.text_input)
|
21 |
+
|
22 |
+
# Define your model from Hugging Face
|
23 |
+
model_directory = "repleeka/eng-taw-nmt"
|
24 |
+
|
25 |
+
device = 0 if torch.cuda.is_available() else -1
|
26 |
+
translation_pipeline = pipeline(
|
27 |
+
task="translation",
|
28 |
+
model="repleeka/eng-taw-nmt",
|
29 |
+
tokenizer="repleeka/eng-taw-nmt",
|
30 |
+
device=device
|
31 |
+
)
|
32 |
+
|
33 |
+
# Translate button
|
34 |
+
if st.button("Translate", key="translate_button"):
|
35 |
+
if text_input:
|
36 |
+
with st.spinner("Translating... Please wait"):
|
37 |
+
# Prepare data for translation
|
38 |
+
sentences = [text_input]
|
39 |
+
data = Dataset.from_dict({"text": sentences})
|
40 |
+
|
41 |
+
# Apply translation
|
42 |
+
try:
|
43 |
+
results = data.map(lambda x: {"translation": translation_pipeline(x["text"])})
|
44 |
+
result = results[0]["translation"][0]['translation_text']
|
45 |
+
|
46 |
+
# Capitalize the first letter of the result
|
47 |
+
result = result.capitalize()
|
48 |
+
|
49 |
+
# Display translation result with custom styling
|
50 |
+
st.markdown("#### Translated text:")
|
51 |
+
st.markdown(f'<h2 class="result-text">{result}</2>', unsafe_allow_html=True)
|
52 |
+
# st.markdown(result)
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"Translation error: {e}")
|
56 |
+
else:
|
57 |
+
st.warning("Please enter text to translate.")
|
58 |
+
|
59 |
+
# Clear input button
|
60 |
+
if st.button("Clear Input"):
|
61 |
+
st.session_state.text_input = ""
|