Update src/streamlit_app.py
Browse files- src/streamlit_app.py +9 -4
src/streamlit_app.py
CHANGED
|
@@ -47,22 +47,27 @@ def instantiate_translation_model(model_name, text, src_lg, tgt_lg):
|
|
| 47 |
return pipe(text)[0]["translation_text"]
|
| 48 |
|
| 49 |
# ---- M2M100 ----
|
| 50 |
-
|
| 51 |
elif "m2m100" in model_name:
|
| 52 |
pipe = load_pipeline("translation", model_path)
|
| 53 |
|
| 54 |
-
#
|
|
|
|
| 55 |
pipe.tokenizer.src_lang = src_lg
|
| 56 |
|
| 57 |
-
#
|
|
|
|
| 58 |
tgt_lang_id = pipe.tokenizer.convert_tokens_to_ids(tgt_lg)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
result = pipe(
|
| 61 |
text,
|
| 62 |
forced_bos_token_id=tgt_lang_id
|
| 63 |
)
|
| 64 |
return result[0]["translation_text"]
|
| 65 |
-
|
| 66 |
# ---- MBART ----
|
| 67 |
else:
|
| 68 |
pipe = pipeline(
|
|
|
|
| 47 |
return pipe(text)[0]["translation_text"]
|
| 48 |
|
| 49 |
# ---- M2M100 ----
|
|
|
|
| 50 |
elif "m2m100" in model_name:
|
| 51 |
pipe = load_pipeline("translation", model_path)
|
| 52 |
|
| 53 |
+
# 1. Definimos a língua de origem
|
| 54 |
+
# Em modelos customizados, as vezes o src_lang precisa ser o token completo
|
| 55 |
pipe.tokenizer.src_lang = src_lg
|
| 56 |
|
| 57 |
+
# 2. Pegamos o ID numérico do token de destino (ex: __pt__)
|
| 58 |
+
# Usamos convert_tokens_to_ids porque ele ignora a lógica interna de busca de idiomas
|
| 59 |
tgt_lang_id = pipe.tokenizer.convert_tokens_to_ids(tgt_lg)
|
| 60 |
|
| 61 |
+
if tgt_lang_id == pipe.tokenizer.unk_token_id:
|
| 62 |
+
st.error(f"Erro: O token {tgt_lg} não foi encontrado no vocabulário do modelo!")
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
# 3. Executamos a tradução forçando o ID de início de frase (BOS)
|
| 66 |
result = pipe(
|
| 67 |
text,
|
| 68 |
forced_bos_token_id=tgt_lang_id
|
| 69 |
)
|
| 70 |
return result[0]["translation_text"]
|
|
|
|
| 71 |
# ---- MBART ----
|
| 72 |
else:
|
| 73 |
pipe = pipeline(
|