Spaces:
Runtime error
Runtime error
Marcus Vinicius Zerbini Canhaço commited on
Commit ·
c09ca6f
1
Parent(s): 4add9f7
feat: atualização do detector com otimizações para GPU T4
Browse files
src/presentation/web/gradio_interface.py
CHANGED
|
@@ -8,6 +8,9 @@ from src.infrastructure.services.notification_services import NotificationServic
|
|
| 8 |
from huggingface_hub import hf_hub_download, list_repo_files, snapshot_download
|
| 9 |
import tempfile
|
| 10 |
import shutil
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class GradioInterface:
|
| 13 |
"""Interface Gradio usando Clean Architecture."""
|
|
@@ -31,19 +34,36 @@ class GradioInterface:
|
|
| 31 |
# Criar diretório de cache se não existir
|
| 32 |
if self.is_huggingface:
|
| 33 |
self.videos_cache_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 34 |
|
| 35 |
-
def _download_dataset_videos(self) ->
|
| 36 |
"""Baixa os vídeos do dataset do Hugging Face."""
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
def list_sample_videos(self) -> list:
|
| 49 |
"""Lista os vídeos de exemplo do dataset ou da pasta local."""
|
|
@@ -52,35 +72,55 @@ class GradioInterface:
|
|
| 52 |
videos = []
|
| 53 |
|
| 54 |
if self.is_huggingface:
|
| 55 |
-
|
| 56 |
-
self._download_dataset_videos()
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
-
|
| 60 |
base_dir = Path("videos")
|
| 61 |
if not base_dir.exists():
|
| 62 |
os.makedirs(base_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
for ext in video_extensions:
|
| 66 |
-
for video_path in base_dir.glob(f'**/*{ext}'):
|
| 67 |
-
videos.append({
|
| 68 |
-
'path': str(video_path),
|
| 69 |
-
'name': video_path.name,
|
| 70 |
-
'ground_truth': '📼 Vídeo de Teste'
|
| 71 |
-
})
|
| 72 |
-
|
| 73 |
return videos
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
-
|
| 77 |
return []
|
| 78 |
|
| 79 |
def load_sample_video(self, video_path: str) -> str:
|
| 80 |
"""Carrega um vídeo de exemplo do cache ou pasta local."""
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def create_interface(self) -> gr.Blocks:
|
| 86 |
"""Cria a interface Gradio."""
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download, list_repo_files, snapshot_download
|
| 9 |
import tempfile
|
| 10 |
import shutil
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
class GradioInterface:
|
| 16 |
"""Interface Gradio usando Clean Architecture."""
|
|
|
|
| 34 |
# Criar diretório de cache se não existir
|
| 35 |
if self.is_huggingface:
|
| 36 |
self.videos_cache_dir.mkdir(parents=True, exist_ok=True)
|
| 37 |
+
logger.info(f"Diretório de cache criado em: {self.videos_cache_dir}")
|
| 38 |
|
| 39 |
+
def _download_dataset_videos(self) -> bool:
|
| 40 |
"""Baixa os vídeos do dataset do Hugging Face."""
|
| 41 |
try:
|
| 42 |
+
logger.info(f"Iniciando download do dataset {self.dataset_id}")
|
| 43 |
+
|
| 44 |
+
# Listar arquivos disponíveis no dataset
|
| 45 |
+
files = list_repo_files(self.dataset_id, repo_type="dataset")
|
| 46 |
+
logger.info(f"Arquivos encontrados no dataset: {files}")
|
| 47 |
+
|
| 48 |
+
# Baixar cada arquivo de vídeo individualmente
|
| 49 |
+
for file in files:
|
| 50 |
+
if file.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
|
| 51 |
+
try:
|
| 52 |
+
local_file = hf_hub_download(
|
| 53 |
+
repo_id=self.dataset_id,
|
| 54 |
+
filename=file,
|
| 55 |
+
repo_type="dataset",
|
| 56 |
+
local_dir=str(self.videos_cache_dir)
|
| 57 |
+
)
|
| 58 |
+
logger.info(f"Arquivo baixado com sucesso: {local_file}")
|
| 59 |
+
except Exception as e:
|
| 60 |
+
logger.error(f"Erro ao baixar arquivo {file}: {str(e)}")
|
| 61 |
+
|
| 62 |
+
return True
|
| 63 |
+
|
| 64 |
except Exception as e:
|
| 65 |
+
logger.error(f"Erro ao baixar dataset: {str(e)}")
|
| 66 |
+
return False
|
| 67 |
|
| 68 |
def list_sample_videos(self) -> list:
|
| 69 |
"""Lista os vídeos de exemplo do dataset ou da pasta local."""
|
|
|
|
| 72 |
videos = []
|
| 73 |
|
| 74 |
if self.is_huggingface:
|
| 75 |
+
logger.info("Ambiente Hugging Face detectado, usando dataset")
|
| 76 |
+
if self._download_dataset_videos():
|
| 77 |
+
logger.info(f"Procurando vídeos em: {self.videos_cache_dir}")
|
| 78 |
+
# Listar todos os vídeos no diretório de cache
|
| 79 |
+
for ext in video_extensions:
|
| 80 |
+
for video_path in Path(self.videos_cache_dir).rglob(f'*{ext}'):
|
| 81 |
+
logger.info(f"Vídeo encontrado: {video_path}")
|
| 82 |
+
videos.append({
|
| 83 |
+
'path': str(video_path),
|
| 84 |
+
'name': video_path.name,
|
| 85 |
+
'ground_truth': '📼 Vídeo de Teste'
|
| 86 |
+
})
|
| 87 |
+
else:
|
| 88 |
+
logger.error("Falha ao baixar dataset")
|
| 89 |
else:
|
| 90 |
+
logger.info("Ambiente local detectado, usando pasta videos")
|
| 91 |
base_dir = Path("videos")
|
| 92 |
if not base_dir.exists():
|
| 93 |
os.makedirs(base_dir)
|
| 94 |
+
logger.info(f"Diretório videos criado: {base_dir}")
|
| 95 |
+
|
| 96 |
+
# Listar vídeos locais
|
| 97 |
+
for ext in video_extensions:
|
| 98 |
+
for video_path in base_dir.glob(f'**/*{ext}'):
|
| 99 |
+
logger.info(f"Vídeo local encontrado: {video_path}")
|
| 100 |
+
videos.append({
|
| 101 |
+
'path': str(video_path),
|
| 102 |
+
'name': video_path.name,
|
| 103 |
+
'ground_truth': '📼 Vídeo de Teste'
|
| 104 |
+
})
|
| 105 |
|
| 106 |
+
logger.info(f"Total de vídeos encontrados: {len(videos)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return videos
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
+
logger.error(f"Erro ao listar vídeos: {str(e)}")
|
| 111 |
return []
|
| 112 |
|
| 113 |
def load_sample_video(self, video_path: str) -> str:
|
| 114 |
"""Carrega um vídeo de exemplo do cache ou pasta local."""
|
| 115 |
+
try:
|
| 116 |
+
if video_path and os.path.exists(video_path):
|
| 117 |
+
logger.info(f"Carregando vídeo: {video_path}")
|
| 118 |
+
return video_path
|
| 119 |
+
logger.warning(f"Vídeo não encontrado: {video_path}")
|
| 120 |
+
return ""
|
| 121 |
+
except Exception as e:
|
| 122 |
+
logger.error(f"Erro ao carregar vídeo: {str(e)}")
|
| 123 |
+
return ""
|
| 124 |
|
| 125 |
def create_interface(self) -> gr.Blocks:
|
| 126 |
"""Cria a interface Gradio."""
|