Download app.py from gpsxpc/bft: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/spaces/gpsxpc/bft/resolve/main/app.py
- Command line
-
hf download hf://spaces/gpsxpc/bft/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gpsxpc/bft/resolve/main/app.py
2.11 kB
| 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() | |