Spaces:
Runtime error
Runtime error
rvian
commited on
Commit
·
144ccda
1
Parent(s):
bd9a03c
first commit
Browse files- app.py +66 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
modelos_opcao =[
|
| 6 |
+
"Narrativa/mbart-large-50-finetuned-opus-en-pt-translation",
|
| 7 |
+
# "unicamp-dl/translation-en-pt-t5" # desempenho inferior ao MBART (porém, mais rápido)
|
| 8 |
+
]
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Carrega o modelo
|
| 12 |
+
def carregar_modelo_e_tokenizador_mbart(modelo):
|
| 13 |
+
# https://huggingface.co/Narrativa/mbart-large-50-finetuned-opus-en-pt-translation
|
| 14 |
+
from transformers import MBart50TokenizerFast, MBartForConditionalGeneration
|
| 15 |
+
|
| 16 |
+
st.write(f'Carregando modelo {modelo}')
|
| 17 |
+
tokenizer = MBart50TokenizerFast.from_pretrained(modelo)
|
| 18 |
+
model = MBartForConditionalGeneration.from_pretrained(modelo).to("cuda")
|
| 19 |
+
|
| 20 |
+
tokenizer.src_lang = 'en_XX'
|
| 21 |
+
return model, tokenizer
|
| 22 |
+
|
| 23 |
+
# TODO:batch?
|
| 24 |
+
def traduzir_en_pt(text):
|
| 25 |
+
inputs = tokenizer(text, return_tensors='pt')
|
| 26 |
+
input_ids = inputs.input_ids.to('cuda')
|
| 27 |
+
attention_mask = inputs.attention_mask.to('cuda')
|
| 28 |
+
output = model.generate(input_ids, attention_mask=attention_mask, forced_bos_token_id=tokenizer.lang_code_to_id['pt_XX'])
|
| 29 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
###################
|
| 33 |
+
#### interface ####
|
| 34 |
+
###################
|
| 35 |
+
|
| 36 |
+
# Cabeçalho
|
| 37 |
+
st.title('Tradutor de datasets (inglês para português)')
|
| 38 |
+
# Carrega dataset
|
| 39 |
+
dataset = st.file_uploader("Carrege o dataset (coluna a ser traduzida deve ser nomeada como 'texto')", type=["csv"])
|
| 40 |
+
st.write('Carregando dataset...')
|
| 41 |
+
|
| 42 |
+
if dataset is not None:
|
| 43 |
+
st.write('🎲 Dataset carregado com sucesso!')
|
| 44 |
+
dataset = pd.read_csv(dataset)
|
| 45 |
+
st.write(dataset)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
modelo_selecionado = st.selectbox('Escolha um modelo', modelos_opcao)
|
| 49 |
+
|
| 50 |
+
if st.button("Carregar modelo escolhido"):
|
| 51 |
+
tokenizer, model = carregar_modelo_e_tokenizador_mbart(modelo_selecionado)
|
| 52 |
+
st.write(f"🎰 Modelo {modelo_selecionado} carregado com sucesso! 🔥")
|
| 53 |
+
|
| 54 |
+
qtde_linhas_traduzir = st.slider('Quantidade de linhas a serem traduzidas', 1, len(dataset), 50)
|
| 55 |
+
if st.button(f"Traduzir {qtde_linhas_traduzir} linhas"):
|
| 56 |
+
for i in range(qtde_linhas_traduzir):
|
| 57 |
+
st.write(f'🔡 Traduzindo linha {i+1}...')
|
| 58 |
+
st.write(f'Texto: {dataset.iloc[i]["texto"]}')
|
| 59 |
+
texto_traduzido= traduzir_en_pt(dataset.iloc[i]["texto"])
|
| 60 |
+
st.write(f'Tradução: {texto_traduzido}')
|
| 61 |
+
|
| 62 |
+
# adiciona traducao em nova coluna dataset
|
| 63 |
+
dataset["traduzido"]= texto_traduzido
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
st.write("Fim 👍")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
setencepiece
|
| 3 |
+
sacremoses
|
| 4 |
+
pandas
|