Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,26 @@ from deep_translator import MyMemoryTranslator
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
def traduzir_arquivo(file, dest_language):
|
| 5 |
-
"""
|
| 6 |
-
Traduz um arquivo de texto grande usando Deep Translator (MyMemory API).
|
| 7 |
-
|
| 8 |
-
Args:
|
| 9 |
-
file: arquivo .txt enviado pelo usuário
|
| 10 |
-
dest_language: código do idioma de destino ("pt", "en", etc.)
|
| 11 |
-
|
| 12 |
-
Returns:
|
| 13 |
-
bytes: arquivo traduzido (para download)
|
| 14 |
-
str: mensagem de status ou erro
|
| 15 |
-
"""
|
| 16 |
try:
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Dividir o texto em blocos
|
| 21 |
chunk_size = 5000
|
| 22 |
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 23 |
|
| 24 |
-
# Traduzir cada bloco
|
| 25 |
translator = MyMemoryTranslator(target=dest_language)
|
| 26 |
translated_chunks = [translator.translate(chunk) for chunk in chunks]
|
| 27 |
|
| 28 |
-
# Concatenar os blocos traduzidos
|
| 29 |
translated_text = "\n".join(translated_chunks)
|
| 30 |
|
| 31 |
-
# Retornar como arquivo para download + mensagem de sucesso
|
| 32 |
return translated_text.encode("utf-8"), "✅ Tradução concluída com sucesso!"
|
| 33 |
-
|
| 34 |
except Exception as e:
|
| 35 |
-
# Retornar None no arquivo e mensagem de erro
|
| 36 |
return None, f"❌ Erro ao traduzir: {str(e)}"
|
| 37 |
|
| 38 |
-
# Criar interface Gradio
|
| 39 |
interface = gr.Interface(
|
| 40 |
fn=traduzir_arquivo,
|
| 41 |
inputs=[
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
def traduzir_arquivo(file, dest_language):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
+
# Detectar tipo de objeto
|
| 7 |
+
if hasattr(file, "read"):
|
| 8 |
+
text = file.read().decode("utf-8") # file-like object
|
| 9 |
+
else:
|
| 10 |
+
text = str(file) # já é string
|
| 11 |
|
| 12 |
+
# Dividir o texto em blocos
|
| 13 |
chunk_size = 5000
|
| 14 |
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 15 |
|
|
|
|
| 16 |
translator = MyMemoryTranslator(target=dest_language)
|
| 17 |
translated_chunks = [translator.translate(chunk) for chunk in chunks]
|
| 18 |
|
|
|
|
| 19 |
translated_text = "\n".join(translated_chunks)
|
| 20 |
|
|
|
|
| 21 |
return translated_text.encode("utf-8"), "✅ Tradução concluída com sucesso!"
|
|
|
|
| 22 |
except Exception as e:
|
|
|
|
| 23 |
return None, f"❌ Erro ao traduzir: {str(e)}"
|
| 24 |
|
|
|
|
| 25 |
interface = gr.Interface(
|
| 26 |
fn=traduzir_arquivo,
|
| 27 |
inputs=[
|