File size: 2,106 Bytes
a002675 a05452f 850960e a05452f c19f6f3 a05452f c19f6f3 a05452f a002675 a05452f a002675 9eed8f9 c19f6f3 a002675 a05452f a002675 a05452f a002675 a05452f a002675 a05452f a002675 a05452f a002675 | 1 2 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from deep_translator import MyMemoryTranslator
import gradio as gr
# Lista de idiomas suportados pelo Deep Translator (MyMemory)
IDIOMAS = {
"English": "en-GB",
'portuguese brazil': 'pt-BR',
"Portuguese (Portugal)": "pt-PT",
"Spanish": "es-ES",
"French": "fr-FR",
"German": "de-DE",
"Italian": "it-IT"
}
def traduzir_arquivo(file, dest_language):
"""
Traduz um arquivo de texto grande usando Deep Translator (MyMemory API).
Args:
file: arquivo .txt enviado pelo usuário
dest_language: código do idioma de destino (MyMemory)
Returns:
bytes: arquivo traduzido para download
str: mensagem de status ou erro
"""
try:
# Detectar tipo de objeto
if hasattr(file, "read"): # file-like object
text = file.read().decode("utf-8")
else: # NamedString do Hugging Face
text = str(file)
# Dividir em blocos de até 5000 caracteres
chunk_size = 5000
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
# Traduzir cada bloco
translator = MyMemoryTranslator(target=dest_language)
translated_chunks = [translator.translate(chunk) for chunk in chunks]
translated_text = "\n".join(translated_chunks)
return translated_text.encode("utf-8"), "✅ Tradução concluída com sucesso!"
except Exception as e:
return None, f"❌ Erro ao traduzir: {str(e)}"
# Interface Gradio
interface = gr.Interface(
fn=traduzir_arquivo,
inputs=[
gr.File(label="Upload do arquivo .txt", file_types=[".txt"]),
gr.Dropdown(
choices=list(IDIOMAS.keys()),
value="Portuguese (Brazil)",
label="Idioma de destino"
)
],
outputs=[
gr.File(label="Arquivo traduzido"),
gr.Textbox(label="Status")
],
title="Tradutor de Arquivos Grandes",
description="Traduza arquivos de texto grandes em blocos de até 5000 caracteres usando Deep Translator (MyMemory API)."
)
if __name__ == "__main__":
interface.launch()
|