youssef
commited on
Commit
·
f5765c8
1
Parent(s):
be20973
Dockerfile
Browse files- Dockerfile +34 -0
- src/video_processor/processor.py +3 -24
Dockerfile
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM nvidia/cuda:12.3.2-cudnn9-devel-ubuntu22.04
|
2 |
+
|
3 |
+
# Set environment variables
|
4 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
5 |
+
ENV PYTHONUNBUFFERED=1
|
6 |
+
ENV FLASH_ATTENTION_SKIP_CUDA_BUILD=TRUE
|
7 |
+
|
8 |
+
# Install system dependencies
|
9 |
+
RUN apt-get update && apt-get install -y \
|
10 |
+
python3.10 \
|
11 |
+
python3-pip \
|
12 |
+
python3.10-dev \
|
13 |
+
build-essential \
|
14 |
+
ninja-build \
|
15 |
+
git \
|
16 |
+
&& rm -rf /var/lib/apt/lists/*
|
17 |
+
|
18 |
+
# Create and set working directory
|
19 |
+
WORKDIR /app
|
20 |
+
|
21 |
+
# Copy requirements first to leverage Docker cache
|
22 |
+
COPY requirements.txt .
|
23 |
+
|
24 |
+
# Install Python dependencies
|
25 |
+
RUN pip3 install --no-cache-dir -r requirements.txt
|
26 |
+
|
27 |
+
# Install flash-attention
|
28 |
+
RUN pip3 install --no-cache-dir flash-attn --no-build-isolation
|
29 |
+
|
30 |
+
# Copy the rest of the application
|
31 |
+
COPY . .
|
32 |
+
|
33 |
+
# Set the default command
|
34 |
+
CMD ["python3", "-m", "src.video_processor.processor"]
|
src/video_processor/processor.py
CHANGED
@@ -1,10 +1,7 @@
|
|
1 |
import torch
|
2 |
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
from typing import List, Dict
|
4 |
-
import numpy as np
|
5 |
import logging
|
6 |
-
import subprocess
|
7 |
-
import os
|
8 |
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
@@ -17,25 +14,6 @@ class VideoAnalyzer:
|
|
17 |
raise RuntimeError("CUDA is required but not available!")
|
18 |
|
19 |
logger.info("Initializing VideoAnalyzer")
|
20 |
-
|
21 |
-
# Try to install flash-attention at runtime
|
22 |
-
logger.info("Attempting to install flash-attention...")
|
23 |
-
try:
|
24 |
-
env = os.environ.copy()
|
25 |
-
env['FLASH_ATTENTION_SKIP_CUDA_BUILD'] = "TRUE"
|
26 |
-
subprocess.run(
|
27 |
-
'pip install flash-attn --no-build-isolation',
|
28 |
-
env=env,
|
29 |
-
shell=True,
|
30 |
-
check=True,
|
31 |
-
capture_output=True
|
32 |
-
)
|
33 |
-
logger.info("Successfully installed flash-attention")
|
34 |
-
use_flash_attn = True
|
35 |
-
except subprocess.CalledProcessError as e:
|
36 |
-
logger.warning(f"Failed to install flash-attention: {e.stderr.decode()}")
|
37 |
-
use_flash_attn = False
|
38 |
-
|
39 |
self.model_path = "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
|
40 |
logger.info(f"Loading model from {self.model_path}")
|
41 |
|
@@ -44,13 +22,14 @@ class VideoAnalyzer:
|
|
44 |
self.model_path,
|
45 |
torch_dtype=torch.bfloat16
|
46 |
)
|
|
|
47 |
|
48 |
self.model = AutoModelForImageTextToText.from_pretrained(
|
49 |
self.model_path,
|
50 |
torch_dtype=torch.bfloat16,
|
51 |
-
_attn_implementation="flash_attention_2"
|
52 |
).to(DEVICE)
|
53 |
-
logger.info(f"Model loaded on device: {self.model.device} using attention implementation:
|
54 |
|
55 |
def process_video(self, video_path: str, frame_interval: int = 30) -> List[Dict]:
|
56 |
logger.info(f"Processing video: {video_path} with frame_interval={frame_interval}")
|
|
|
1 |
import torch
|
2 |
from transformers import AutoProcessor, AutoModelForImageTextToText
|
3 |
from typing import List, Dict
|
|
|
4 |
import logging
|
|
|
|
|
5 |
|
6 |
logger = logging.getLogger(__name__)
|
7 |
|
|
|
14 |
raise RuntimeError("CUDA is required but not available!")
|
15 |
|
16 |
logger.info("Initializing VideoAnalyzer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
self.model_path = "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
|
18 |
logger.info(f"Loading model from {self.model_path}")
|
19 |
|
|
|
22 |
self.model_path,
|
23 |
torch_dtype=torch.bfloat16
|
24 |
)
|
25 |
+
|
26 |
|
27 |
self.model = AutoModelForImageTextToText.from_pretrained(
|
28 |
self.model_path,
|
29 |
torch_dtype=torch.bfloat16,
|
30 |
+
_attn_implementation="flash_attention_2"
|
31 |
).to(DEVICE)
|
32 |
+
logger.info(f"Model loaded on device: {self.model.device} using attention implementation: flash_attention_2")
|
33 |
|
34 |
def process_video(self, video_path: str, frame_interval: int = 30) -> List[Dict]:
|
35 |
logger.info(f"Processing video: {video_path} with frame_interval={frame_interval}")
|