Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,74 @@
|
|
1 |
-
import re
|
2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
|
15 |
-
|
16 |
|
17 |
|
18 |
-
|
19 |
-
|
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 |
-
model=selected_model[0],
|
48 |
-
tokenizer=selected_model[0],
|
49 |
-
)
|
50 |
-
|
51 |
-
progress_bar = st.progress(0)
|
52 |
-
with st.spinner(text="π Translating..."):
|
53 |
-
text_to_translate = re.split('(?<=[.!?]) +', input_text)
|
54 |
-
total_progress = len(text_to_translate)
|
55 |
-
|
56 |
-
for i, text in enumerate(text_to_translate):
|
57 |
-
translation = task(text)
|
58 |
-
text_to_translate[i] = translation[0]["translation_text"]
|
59 |
-
progress_bar.progress((i + 1) / total_progress)
|
60 |
-
|
61 |
-
st.success("π£οΈ Translated!")
|
62 |
-
st.write(f"**Translation:** {' '.join(text_to_translate)}")
|
63 |
-
st.download_button(
|
64 |
-
label="Download translated text",
|
65 |
-
data="\n".join(text_to_translate),
|
66 |
-
file_name=f"{st.session_state['input_lang']}-{st.session_state['output_lang']}-{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.txt",
|
67 |
-
mime="text/plain"
|
68 |
-
)
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import os
|
3 |
+
import io
|
4 |
+
from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration
|
5 |
+
import time
|
6 |
+
import json
|
7 |
+
from typing import List
|
8 |
+
import torch
|
9 |
+
import random
|
10 |
+
import logging
|
11 |
|
12 |
+
if torch.cuda.is_available():
|
13 |
+
device = torch.device("cuda:0")
|
14 |
+
else:
|
15 |
+
device = torch.device("cpu")
|
16 |
+
logging.warning("GPU not found, using CPU, translation will be very slow.")
|
17 |
|
18 |
+
st.cache(suppress_st_warning=True, allow_output_mutation=True)
|
19 |
+
st.set_page_config(page_title="M2M100 Translator")
|
20 |
|
21 |
+
lang_id = {
|
22 |
+
|
23 |
+
"English": "en",
|
24 |
|
25 |
+
"French": "fr",
|
26 |
|
27 |
+
}
|
28 |
|
29 |
|
30 |
+
@st.cache(suppress_st_warning=True, allow_output_mutation=True)
|
31 |
+
def load_model(
|
32 |
+
pretrained_model: str = "facebook/m2m100_418M",
|
33 |
+
cache_dir: str = "models/",
|
34 |
+
):
|
35 |
+
tokenizer = M2M100Tokenizer.from_pretrained(pretrained_model, cache_dir=cache_dir)
|
36 |
+
model = M2M100ForConditionalGeneration.from_pretrained(
|
37 |
+
pretrained_model, cache_dir=cache_dir
|
38 |
+
).to(device)
|
39 |
+
model.eval()
|
40 |
+
return tokenizer, model
|
41 |
+
|
42 |
+
|
43 |
+
st.title("M2M100 Translator")
|
44 |
+
|
45 |
+
|
46 |
+
user_input: str = st.text_area(
|
47 |
+
"Input text",
|
48 |
+
height=200,
|
49 |
+
max_chars=5120,
|
50 |
)
|
51 |
|
52 |
+
source_lang = st.selectbox(label="Source language", options=list(lang_id.keys()))
|
53 |
+
target_lang = st.selectbox(label="Target language", options=list(lang_id.keys()))
|
54 |
|
55 |
+
if st.button("Run"):
|
56 |
+
time_start = time.time()
|
57 |
+
tokenizer, model = load_model()
|
58 |
|
59 |
+
src_lang = lang_id[source_lang]
|
60 |
+
trg_lang = lang_id[target_lang]
|
61 |
+
tokenizer.src_lang = src_lang
|
62 |
+
with torch.no_grad():
|
63 |
+
encoded_input = tokenizer(user_input, return_tensors="pt").to(device)
|
64 |
+
generated_tokens = model.generate(
|
65 |
+
**encoded_input, forced_bos_token_id=tokenizer.get_lang_id(trg_lang)
|
66 |
+
)
|
67 |
+
translated_text = tokenizer.batch_decode(
|
68 |
+
generated_tokens, skip_special_tokens=True
|
69 |
+
)[0]
|
70 |
+
|
71 |
+
time_end = time.time()
|
72 |
+
st.success(translated_text)
|
73 |
+
|
74 |
+
st.write(f"Computation time: {round((time_end-time_start),3)} segs")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|