Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| App principal para execução no HuggingFace Space. | |
| Este arquivo executa o treinamento do modelo EDA e inicia o TensorBoard. | |
| """ | |
| import os | |
| import subprocess | |
| import sys | |
| import threading | |
| import time | |
| def start_tensorboard(): | |
| """ | |
| Inicia o TensorBoard em background, verificando se já está rodando. | |
| """ | |
| # Os logs do TensorBoard são salvos no output_dir (./results) quando report_to=["tensorboard"] | |
| # Criar diretório de resultados se não existir | |
| results_dir = os.path.join(os.path.dirname(__file__), "results") | |
| os.makedirs(results_dir, exist_ok=True) | |
| # Verificar se TensorBoard já está rodando na porta 6006 | |
| import socket | |
| port_in_use = False | |
| try: | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| result = sock.connect_ex(('0.0.0.0', 6006)) | |
| if result == 0: | |
| port_in_use = True | |
| sock.close() | |
| except Exception: | |
| pass | |
| if port_in_use: | |
| print("=" * 60) | |
| print("⚠️ TensorBoard já está rodando na porta 6006") | |
| print("=" * 60) | |
| return | |
| print("=" * 60) | |
| print("Iniciando TensorBoard...") | |
| print(f"Logdir: {results_dir}") | |
| print("TensorBoard estará disponível na interface do HuggingFace Space") | |
| print("=" * 60) | |
| # Matar qualquer processo TensorBoard anterior (se houver) | |
| os.system("pkill -f 'tensorboard.*6006' 2>/dev/null || true") | |
| time.sleep(1) | |
| # Iniciar TensorBoard em background | |
| # IMPORTANTE: Os logs são salvos em ./results quando report_to=["tensorboard"] | |
| # Porta 6006 é a porta padrão do TensorBoard | |
| os.system("tensorboard --logdir=results --host=0.0.0.0 --port=6006 > /dev/null 2>&1 &") | |
| # Aguardar um pouco para garantir que o TensorBoard iniciou | |
| time.sleep(2) | |
| print("✅ TensorBoard iniciado em background") | |
| def main(): | |
| """ | |
| Executa o script de treinamento. | |
| """ | |
| # Iniciar TensorBoard em thread separada | |
| tensorboard_thread = threading.Thread(target=start_tensorboard, daemon=True) | |
| tensorboard_thread.start() | |
| print("=" * 60) | |
| print("Iniciando treinamento do modelo EDA") | |
| print("=" * 60) | |
| script_path = os.path.join(os.path.dirname(__file__), "train.py") | |
| if not os.path.exists(script_path): | |
| print(f"❌ Erro: Arquivo {script_path} não encontrado!") | |
| sys.exit(1) | |
| try: | |
| result = subprocess.run( | |
| [sys.executable, script_path], | |
| check=True, | |
| capture_output=False, | |
| ) | |
| print("\n✅ Treinamento concluído com sucesso!") | |
| return result.returncode | |
| except subprocess.CalledProcessError as e: | |
| print(f"\n❌ Erro durante o treinamento: {e}") | |
| sys.exit(e.returncode) | |
| except KeyboardInterrupt: | |
| print("\n⚠️ Treinamento interrompido pelo usuário") | |
| sys.exit(130) | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |