Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from deep_translator import MyMemoryTranslator
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
def traduzir_arquivo(file, dest_language):
|
| 5 |
+
try:
|
| 6 |
+
text = file.read().decode("utf-8")
|
| 7 |
+
|
| 8 |
+
# Dividir o texto em blocos de até 5000 caracteres
|
| 9 |
+
chunk_size = 5000
|
| 10 |
+
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 11 |
+
|
| 12 |
+
# Traduzir cada bloco
|
| 13 |
+
translator = MyMemoryTranslator(target=dest_language)
|
| 14 |
+
translated_chunks = [translator.translate(chunk) for chunk in chunks]
|
| 15 |
+
translated_text = "\n".join(translated_chunks)
|
| 16 |
+
|
| 17 |
+
# Retornar como arquivo de download
|
| 18 |
+
return translated_text.encode("utf-8")
|
| 19 |
+
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Erro ao traduzir: {str(e)}"
|
| 22 |
+
|
| 23 |
+
interface = gr.Interface(
|
| 24 |
+
fn=traduzir_arquivo,
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.File(label="Upload do arquivo .txt", file_types=[".txt"]),
|
| 27 |
+
gr.Dropdown(choices=["en", "pt", "es", "fr", "de", "it"], label="Idioma de destino")
|
| 28 |
+
],
|
| 29 |
+
outputs=gr.File(label="Arquivo traduzido"),
|
| 30 |
+
title="Tradutor de Arquivos Grandes",
|
| 31 |
+
description="Traduza arquivos de texto com até 5000 caracteres por bloco usando Deep Translator (MyMemory API)."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
interface.launch()
|