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