Marcus Vinicius Zerbini Canhaço commited on
Commit
2dfae6f
·
1 Parent(s): 9baead7

feat: atualização do detector com otimizações para GPU T4

Browse files
Dockerfile CHANGED
@@ -36,23 +36,22 @@ RUN pip install --no-cache-dir -U pip setuptools wheel && \
36
  pip install --no-cache-dir --upgrade "tokenizers>=0.15.0" && \
37
  pip install --no-cache-dir --upgrade "git+https://github.com/huggingface/transformers.git"
38
 
39
- # Criar diretório de vídeos e cache
40
- RUN mkdir -p /code/videos /code/.cache/huggingface /code/.cache/torch && \
41
- chmod -R 777 /code/.cache /code/videos
42
 
43
  # Configurar variáveis de ambiente
44
  ENV HOST=0.0.0.0 \
45
  PORT=7860 \
46
  PYTHONUNBUFFERED=1 \
47
  PYTHONPATH=/code \
48
- TRANSFORMERS_CACHE=/code/.cache/huggingface \
49
- TORCH_HOME=/code/.cache/torch \
50
  GRADIO_SERVER_NAME=0.0.0.0 \
51
  GRADIO_SERVER_PORT=7860 \
52
  SYSTEM=spaces \
53
  CUDA_VISIBLE_DEVICES=0 \
54
- HUGGINGFACE_HUB_CACHE=/code/.cache/huggingface \
55
- HF_HOME=/code/.cache/huggingface \
56
  TORCH_CUDA_ARCH_LIST="7.5" \
57
  MAX_WORKERS=2 \
58
  TRANSFORMERS_OFFLINE=0 \
 
36
  pip install --no-cache-dir --upgrade "tokenizers>=0.15.0" && \
37
  pip install --no-cache-dir --upgrade "git+https://github.com/huggingface/transformers.git"
38
 
39
+ # Criar diretório de vídeos
40
+ RUN mkdir -p /code/videos && chmod -R 777 /code/videos
 
41
 
42
  # Configurar variáveis de ambiente
43
  ENV HOST=0.0.0.0 \
44
  PORT=7860 \
45
  PYTHONUNBUFFERED=1 \
46
  PYTHONPATH=/code \
47
+ TRANSFORMERS_CACHE=/tmp/huggingface \
48
+ TORCH_HOME=/tmp/torch \
49
  GRADIO_SERVER_NAME=0.0.0.0 \
50
  GRADIO_SERVER_PORT=7860 \
51
  SYSTEM=spaces \
52
  CUDA_VISIBLE_DEVICES=0 \
53
+ HUGGINGFACE_HUB_CACHE=/tmp/huggingface \
54
+ HF_HOME=/tmp/huggingface \
55
  TORCH_CUDA_ARCH_LIST="7.5" \
56
  MAX_WORKERS=2 \
57
  TRANSFORMERS_OFFLINE=0 \
deploy.sh CHANGED
@@ -18,25 +18,18 @@ echo "🚀 Preparando deploy para GitHub..."
18
  echo "📦 Adicionando arquivos para GitHub..."
19
  git add .
20
 
21
- # Verificar se há alterações para commitar
22
- if [[ -n $(git status -s) ]]; then
23
- echo "📝 Existem alterações para commitar no GitHub"
24
-
25
- # Fazer commit
26
- git commit -m "$commit_message"
27
-
28
- # Force push para GitHub
29
- echo "🚀 Forçando push para GitHub..."
30
- git push -f origin main
31
-
32
- if [ $? -eq 0 ]; then
33
- echo "✅ Deploy para GitHub concluído com sucesso!"
34
- else
35
- echo "❌ Erro durante o deploy para GitHub"
36
- exit 1
37
- fi
38
  else
39
- echo " Workspace limpo, nenhuma alteração para GitHub"
 
40
  fi
41
 
42
  # Deploy para Hugging Face
@@ -54,32 +47,26 @@ fi
54
  echo "📦 Adicionando todos os arquivos..."
55
  git add --all
56
 
57
- # Verificar se há alterações para o Hugging Face
58
- if [[ -n $(git status -s) ]]; then
59
- echo "📝 Existem alterações para commitar no Hugging Face"
60
-
61
- # Fazer commit
62
- git commit -m "$commit_message"
63
-
64
- echo "🚀 Enviando para Hugging Face Space..."
65
- git push -f space main
66
-
67
- if [ $? -eq 0 ]; then
68
- echo "✅ Deploy para Hugging Face concluído com sucesso!"
69
- echo "🌐 Seu app estará disponível em alguns minutos em:"
70
- echo " https://huggingface.co/spaces/marcuscanhaco/weapon-detection-app"
71
- echo ""
72
- echo "⚠️ Lembre-se de verificar no Hugging Face Space se:"
73
- echo " 1. O Space está configurado para usar GPU T4"
74
- echo " 2. As variáveis de ambiente estão configuradas corretamente:"
75
- echo " - HUGGING_FACE_TOKEN"
76
- echo " - NOTIFICATION_EMAIL"
77
- echo " - SENDGRID_API_KEY"
78
- echo " 3. Os requisitos de memória estão adequados"
79
- else
80
- echo "❌ Erro durante o deploy para Hugging Face"
81
- exit 1
82
- fi
83
  else
84
- echo " Workspace limpo, nenhuma alteração para Hugging Face"
 
85
  fi
 
18
  echo "📦 Adicionando arquivos para GitHub..."
19
  git add .
20
 
21
+ # Fazer commit
22
+ git commit -m "$commit_message"
23
+
24
+ # Force push para GitHub
25
+ echo "🚀 Forçando push para GitHub..."
26
+ git push -f origin main
27
+
28
+ if [ $? -eq 0 ]; then
29
+ echo " Deploy para GitHub concluído com sucesso!"
 
 
 
 
 
 
 
 
30
  else
31
+ echo " Erro durante o deploy para GitHub"
32
+ exit 1
33
  fi
34
 
35
  # Deploy para Hugging Face
 
47
  echo "📦 Adicionando todos os arquivos..."
48
  git add --all
49
 
50
+ # Fazer commit
51
+ git commit -m "$commit_message"
52
+
53
+ # Force push para Hugging Face
54
+ echo "🚀 Enviando para Hugging Face Space..."
55
+ git push -f space main
56
+
57
+ if [ $? -eq 0 ]; then
58
+ echo "✅ Deploy para Hugging Face concluído com sucesso!"
59
+ echo "🌐 Seu app estará disponível em alguns minutos em:"
60
+ echo " https://huggingface.co/spaces/marcuscanhaco/weapon-detection-app"
61
+ echo ""
62
+ echo "⚠️ Lembre-se de verificar no Hugging Face Space se:"
63
+ echo " 1. O Space está configurado para usar GPU T4"
64
+ echo " 2. As variáveis de ambiente estão configuradas corretamente:"
65
+ echo " - HUGGING_FACE_TOKEN"
66
+ echo " - NOTIFICATION_EMAIL"
67
+ echo " - SENDGRID_API_KEY"
68
+ echo " 3. Os requisitos de memória estão adequados"
 
 
 
 
 
 
 
69
  else
70
+ echo " Erro durante o deploy para Hugging Face"
71
+ exit 1
72
  fi
src/domain/detectors/cpu.py CHANGED
@@ -60,7 +60,7 @@ class WeaponDetectorCPU(BaseDetector):
60
  torch.set_num_interop_threads(min(8, os.cpu_count()))
61
 
62
  # Carregar modelo com configurações otimizadas
63
- cache_dir = os.getenv('CACHE_DIR', '/tmp/weapon_detection_cache')
64
  os.makedirs(cache_dir, exist_ok=True)
65
 
66
  model_name = "google/owlv2-base-patch16"
 
60
  torch.set_num_interop_threads(min(8, os.cpu_count()))
61
 
62
  # Carregar modelo com configurações otimizadas
63
+ cache_dir = os.path.join(tempfile.gettempdir(), 'weapon_detection_cache')
64
  os.makedirs(cache_dir, exist_ok=True)
65
 
66
  model_name = "google/owlv2-base-patch16"
src/domain/detectors/gpu.py CHANGED
@@ -10,6 +10,7 @@ import cv2
10
  from PIL import Image
11
  from transformers import Owlv2Processor, Owlv2ForObjectDetection
12
  from .base import BaseDetector, BaseCache
 
13
 
14
  logger = logging.getLogger(__name__)
15
 
@@ -48,7 +49,7 @@ class WeaponDetectorGPU(BaseDetector):
48
  self.device = self._get_best_device()
49
 
50
  # Diretório de cache para o modelo
51
- cache_dir = os.getenv('CACHE_DIR', '/tmp/weapon_detection_cache')
52
  os.makedirs(cache_dir, exist_ok=True)
53
 
54
  # Limpar memória GPU
 
10
  from PIL import Image
11
  from transformers import Owlv2Processor, Owlv2ForObjectDetection
12
  from .base import BaseDetector, BaseCache
13
+ import tempfile
14
 
15
  logger = logging.getLogger(__name__)
16
 
 
49
  self.device = self._get_best_device()
50
 
51
  # Diretório de cache para o modelo
52
+ cache_dir = os.path.join(tempfile.gettempdir(), 'weapon_detection_cache')
53
  os.makedirs(cache_dir, exist_ok=True)
54
 
55
  # Limpar memória GPU