Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,38 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the model and tokenizer
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("EzekielMW/Eksl_dataset")
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("EzekielMW/Eksl_dataset")
|
8 |
+
|
9 |
+
def translate(text, source_language, target_language):
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
inputs = tokenizer(text.lower(), return_tensors="pt").to(device)
|
12 |
+
inputs['input_ids'][0][0] = tokenizer.convert_tokens_to_ids(source_language)
|
13 |
+
translated_tokens = model.to(device).generate(
|
14 |
+
**inputs,
|
15 |
+
forced_bos_token_id=tokenizer.convert_tokens_to_ids(target_language),
|
16 |
+
max_length=100,
|
17 |
+
num_beams=5,
|
18 |
+
)
|
19 |
+
result = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
|
20 |
+
|
21 |
+
if target_language == 'ksl':
|
22 |
+
result = result.upper()
|
23 |
+
|
24 |
+
return result
|
25 |
+
|
26 |
+
st.title('Translation App')
|
27 |
+
|
28 |
+
# Text input
|
29 |
+
text = st.text_input('Enter text to translate')
|
30 |
+
source_language = st.selectbox('Source Language', ['eng', 'swa', 'ksl'])
|
31 |
+
target_language = st.selectbox('Target Language', ['eng', 'swa', 'ksl'])
|
32 |
+
|
33 |
+
if st.button('Translate'):
|
34 |
+
if text:
|
35 |
+
translation = translate(text, source_language, target_language)
|
36 |
+
st.write(f'Translation: {translation}')
|
37 |
+
else:
|
38 |
+
st.write('Please enter text to translate.')
|