Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,59 @@
|
|
| 1 |
from deep_translator import MyMemoryTranslator
|
| 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
|
| 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=[
|
| 28 |
gr.File(label="Upload do arquivo .txt", file_types=[".txt"]),
|
| 29 |
gr.Dropdown(
|
| 30 |
-
choices=
|
| 31 |
-
value="
|
| 32 |
label="Idioma de destino"
|
| 33 |
)
|
| 34 |
],
|
|
|
|
| 1 |
from deep_translator import MyMemoryTranslator
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Lista de idiomas suportados pelo Deep Translator (MyMemory)
|
| 5 |
+
IDIOMAS = {
|
| 6 |
+
"English": "en-GB",
|
| 7 |
+
"Portuguese (Brazil)": "pt-BR",
|
| 8 |
+
"Portuguese (Portugal)": "pt-PT",
|
| 9 |
+
"Spanish": "es",
|
| 10 |
+
"French": "fr-FR",
|
| 11 |
+
"German": "de",
|
| 12 |
+
"Italian": "it"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
def traduzir_arquivo(file, dest_language):
|
| 16 |
+
"""
|
| 17 |
+
Traduz um arquivo de texto grande usando Deep Translator (MyMemory API).
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
file: arquivo .txt enviado pelo usuário
|
| 21 |
+
dest_language: código do idioma de destino (MyMemory)
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
bytes: arquivo traduzido para download
|
| 25 |
+
str: mensagem de status ou erro
|
| 26 |
+
"""
|
| 27 |
try:
|
| 28 |
# Detectar tipo de objeto
|
| 29 |
if hasattr(file, "read"):
|
| 30 |
text = file.read().decode("utf-8") # file-like object
|
| 31 |
else:
|
| 32 |
+
text = str(file) # já é string (NamedString do Hugging Face)
|
| 33 |
|
| 34 |
+
# Dividir em blocos de até 5000 caracteres
|
| 35 |
chunk_size = 5000
|
| 36 |
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
|
| 37 |
|
| 38 |
+
# Traduzir cada bloco
|
| 39 |
translator = MyMemoryTranslator(target=dest_language)
|
| 40 |
translated_chunks = [translator.translate(chunk) for chunk in chunks]
|
| 41 |
|
| 42 |
translated_text = "\n".join(translated_chunks)
|
| 43 |
|
| 44 |
return translated_text.encode("utf-8"), "✅ Tradução concluída com sucesso!"
|
| 45 |
+
|
| 46 |
except Exception as e:
|
| 47 |
return None, f"❌ Erro ao traduzir: {str(e)}"
|
| 48 |
|
| 49 |
+
# Interface Gradio
|
| 50 |
interface = gr.Interface(
|
| 51 |
fn=traduzir_arquivo,
|
| 52 |
inputs=[
|
| 53 |
gr.File(label="Upload do arquivo .txt", file_types=[".txt"]),
|
| 54 |
gr.Dropdown(
|
| 55 |
+
choices=list(IDIOMAS.keys()),
|
| 56 |
+
value="Portuguese (Brazil)",
|
| 57 |
label="Idioma de destino"
|
| 58 |
)
|
| 59 |
],
|