Spaces:
Runtime error
Runtime error
Commit
·
df3368c
1
Parent(s):
df869a9
init
Browse files- app.py +30 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
4 |
+
|
5 |
+
|
6 |
+
@st.cache_resource
|
7 |
+
def init_model():
|
8 |
+
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
10 |
+
return model, tokenizer
|
11 |
+
|
12 |
+
|
13 |
+
max_source_length = 512
|
14 |
+
max_target_length = 128
|
15 |
+
|
16 |
+
model, tokenizer = init_model()
|
17 |
+
|
18 |
+
st.title('T5-Small')
|
19 |
+
with st.form('my_form'):
|
20 |
+
text = st.text_area('Enter text:', '')
|
21 |
+
cols = st.columns(3)
|
22 |
+
submitted = cols[0].form_submit_button('translate')
|
23 |
+
task_prefix = cols[1].text_input("input language", "translate Chinese to English: ")
|
24 |
+
|
25 |
+
placeholder = st.markdown("", unsafe_allow_html=True)
|
26 |
+
if submitted:
|
27 |
+
with st.spinner("Translating..."):
|
28 |
+
input_ids = tokenizer(f"{task_prefix}{text}", return_tensors="pt").input_ids
|
29 |
+
outputs = model.generate(input_ids)
|
30 |
+
placeholder.markdown(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
torchaudio
|
4 |
+
transformers
|
5 |
+
sentencepiece
|