mknolan commited on
Commit
f8e5af1
·
verified ·
1 Parent(s): 2d5a207

Upload InternVL2 implementation

Browse files
Files changed (2) hide show
  1. Dockerfile +18 -0
  2. app_internvl2.py +57 -1
Dockerfile CHANGED
@@ -6,6 +6,8 @@ ENV PYTHONUNBUFFERED=1
6
  ENV HF_HOME=/app/.cache/huggingface
7
  ENV TRANSFORMERS_CACHE=/app/.cache/huggingface/transformers
8
  ENV MPLCONFIGDIR=/tmp/matplotlib
 
 
9
 
10
  # Create necessary directories with proper permissions
11
  RUN mkdir -p /app/.cache/huggingface/transformers && \
@@ -23,11 +25,24 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
23
  python3-pip \
24
  python3-dev \
25
  python3-setuptools \
 
26
  && rm -rf /var/lib/apt/lists/*
27
 
28
  # Create a working directory
29
  WORKDIR /app
30
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # Copy requirements file
32
  COPY requirements.txt .
33
 
@@ -63,5 +78,8 @@ RUN mkdir -p gradio_cached_examples && \
63
  # Make port 7860 available for the app
64
  EXPOSE 7860
65
 
 
 
 
66
  # Start the application
67
  CMD ["python3", "app_internvl2.py"]
 
6
  ENV HF_HOME=/app/.cache/huggingface
7
  ENV TRANSFORMERS_CACHE=/app/.cache/huggingface/transformers
8
  ENV MPLCONFIGDIR=/tmp/matplotlib
9
+ # Force PyTorch to use the NCCl backend
10
+ ENV PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128
11
 
12
  # Create necessary directories with proper permissions
13
  RUN mkdir -p /app/.cache/huggingface/transformers && \
 
25
  python3-pip \
26
  python3-dev \
27
  python3-setuptools \
28
+ nvidia-cuda-toolkit \
29
  && rm -rf /var/lib/apt/lists/*
30
 
31
  # Create a working directory
32
  WORKDIR /app
33
 
34
+ # Add a script to check GPU status at startup
35
+ RUN echo '#!/bin/bash \n\
36
+ echo "Checking NVIDIA GPU status..." \n\
37
+ if ! command -v nvidia-smi &> /dev/null; then \n\
38
+ echo "WARNING: nvidia-smi command not found. NVIDIA driver might not be installed." \n\
39
+ else \n\
40
+ echo "NVIDIA driver found. Running nvidia-smi:" \n\
41
+ nvidia-smi \n\
42
+ fi \n\
43
+ exec "$@"' > /entrypoint.sh && \
44
+ chmod +x /entrypoint.sh
45
+
46
  # Copy requirements file
47
  COPY requirements.txt .
48
 
 
78
  # Make port 7860 available for the app
79
  EXPOSE 7860
80
 
81
+ # Use our entrypoint script to check GPU status before starting the app
82
+ ENTRYPOINT ["/entrypoint.sh"]
83
+
84
  # Start the application
85
  CMD ["python3", "app_internvl2.py"]
app_internvl2.py CHANGED
@@ -41,10 +41,31 @@ warnings.filterwarnings("ignore", message=".*The 'nopython' keyword.*")
41
  warnings.filterwarnings("ignore", message=".*Torch is not compiled with CUDA enabled.*")
42
  warnings.filterwarnings("ignore", category=UserWarning)
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  # Global variables
45
  internvl2_pipeline = None
46
  MODEL_LOADED = False
47
- USE_GPU = torch.cuda.is_available()
 
 
 
 
 
48
 
49
  # Check if lmdeploy is available and try to import
50
  try:
@@ -71,6 +92,12 @@ def load_internvl2_model():
71
  print("lmdeploy not available. Using demo placeholder.")
72
  MODEL_LOADED = False
73
  return False
 
 
 
 
 
 
74
 
75
  print("Loading InternVL2 model...")
76
  try:
@@ -91,6 +118,8 @@ def load_internvl2_model():
91
  print(f"Error loading InternVL2 model: {str(e)}")
92
  if "CUDA out of memory" in str(e):
93
  print("Not enough GPU memory for the model")
 
 
94
  MODEL_LOADED = False
95
  return False
96
 
@@ -104,6 +133,12 @@ def analyze_image(image, prompt):
104
  return ("This is a demo placeholder. The actual model couldn't be loaded because lmdeploy "
105
  "is not properly installed. Check your installation and dependencies.")
106
 
 
 
 
 
 
 
107
  # Make sure the model is loaded
108
  if not load_internvl2_model():
109
  return "Couldn't load InternVL2 model. See logs for details."
@@ -164,9 +199,13 @@ def create_interface():
164
  gr.Markdown("# Image Analysis with InternVL2-40B")
165
  gr.Markdown("Upload an image to analyze it using the InternVL2-40B model.")
166
 
 
167
  if not LMDEPLOY_AVAILABLE:
168
  gr.Markdown("⚠️ **WARNING**: lmdeploy is not properly installed. This demo will not function correctly.", elem_classes=["warning-message"])
169
 
 
 
 
170
  with gr.Row():
171
  with gr.Column(scale=1):
172
  input_image = gr.Image(type="pil", label="Upload Image")
@@ -176,9 +215,15 @@ def create_interface():
176
  value="general"
177
  )
178
  submit_btn = gr.Button("Analyze Image")
 
 
 
 
179
 
180
  with gr.Column(scale=2):
181
  output_text = gr.Textbox(label="Analysis Result", lines=20)
 
 
182
 
183
  submit_btn.click(
184
  fn=process_image,
@@ -195,6 +240,17 @@ def create_interface():
195
  - **Technical**: Technical analysis identifying objects and spatial relationships
196
  """)
197
 
 
 
 
 
 
 
 
 
 
 
 
198
  # Examples
199
  try:
200
  gr.Examples(
 
41
  warnings.filterwarnings("ignore", message=".*Torch is not compiled with CUDA enabled.*")
42
  warnings.filterwarnings("ignore", category=UserWarning)
43
 
44
+ # Check for actual GPU availability
45
+ def check_gpu_availability():
46
+ """Check if GPU is actually available and working"""
47
+ if not torch.cuda.is_available():
48
+ print("CUDA is not available in PyTorch")
49
+ return False
50
+
51
+ try:
52
+ # Try to initialize CUDA and run a simple operation
53
+ x = torch.rand(10, device="cuda")
54
+ y = x + x
55
+ return True
56
+ except Exception as e:
57
+ print(f"GPU initialization failed: {str(e)}")
58
+ return False
59
+
60
  # Global variables
61
  internvl2_pipeline = None
62
  MODEL_LOADED = False
63
+ USE_GPU = check_gpu_availability()
64
+
65
+ if USE_GPU:
66
+ print("GPU is available and working properly")
67
+ else:
68
+ print("WARNING: GPU is not available or not working properly. This application requires GPU acceleration.")
69
 
70
  # Check if lmdeploy is available and try to import
71
  try:
 
92
  print("lmdeploy not available. Using demo placeholder.")
93
  MODEL_LOADED = False
94
  return False
95
+
96
+ # Check if GPU is available
97
+ if not USE_GPU:
98
+ print("Cannot load InternVL2 model without GPU acceleration.")
99
+ MODEL_LOADED = False
100
+ return False
101
 
102
  print("Loading InternVL2 model...")
103
  try:
 
118
  print(f"Error loading InternVL2 model: {str(e)}")
119
  if "CUDA out of memory" in str(e):
120
  print("Not enough GPU memory for the model")
121
+ elif "Found no NVIDIA driver" in str(e):
122
+ print("NVIDIA GPU driver not found or not properly configured")
123
  MODEL_LOADED = False
124
  return False
125
 
 
133
  return ("This is a demo placeholder. The actual model couldn't be loaded because lmdeploy "
134
  "is not properly installed. Check your installation and dependencies.")
135
 
136
+ # Check for GPU
137
+ if not USE_GPU:
138
+ return ("ERROR: This application requires a GPU to run InternVL2. "
139
+ "The NVIDIA driver was not detected on this system. "
140
+ "Please make sure this Space is using a GPU-enabled instance.")
141
+
142
  # Make sure the model is loaded
143
  if not load_internvl2_model():
144
  return "Couldn't load InternVL2 model. See logs for details."
 
199
  gr.Markdown("# Image Analysis with InternVL2-40B")
200
  gr.Markdown("Upload an image to analyze it using the InternVL2-40B model.")
201
 
202
+ # Show warnings based on system status
203
  if not LMDEPLOY_AVAILABLE:
204
  gr.Markdown("⚠️ **WARNING**: lmdeploy is not properly installed. This demo will not function correctly.", elem_classes=["warning-message"])
205
 
206
+ if not USE_GPU:
207
+ gr.Markdown("🚫 **ERROR**: NVIDIA GPU not detected. This application requires GPU acceleration to run InternVL2 model.", elem_classes=["error-message"])
208
+
209
  with gr.Row():
210
  with gr.Column(scale=1):
211
  input_image = gr.Image(type="pil", label="Upload Image")
 
215
  value="general"
216
  )
217
  submit_btn = gr.Button("Analyze Image")
218
+
219
+ # Disable button if GPU is not available
220
+ if not USE_GPU:
221
+ submit_btn.interactive = False
222
 
223
  with gr.Column(scale=2):
224
  output_text = gr.Textbox(label="Analysis Result", lines=20)
225
+ if not USE_GPU:
226
+ output_text.value = "ERROR: NVIDIA GPU driver not detected. This application requires GPU acceleration to run the InternVL2 model. Please ensure this Space is using a GPU-enabled instance."
227
 
228
  submit_btn.click(
229
  fn=process_image,
 
240
  - **Technical**: Technical analysis identifying objects and spatial relationships
241
  """)
242
 
243
+ # Hardware requirements notice
244
+ gr.Markdown("""
245
+ ## System Requirements
246
+ This application requires:
247
+ - NVIDIA GPU with CUDA support
248
+ - At least 16GB of GPU memory recommended
249
+ - GPU drivers properly installed and configured
250
+
251
+ If you're running this on Hugging Face Spaces, make sure to select a GPU-enabled hardware type.
252
+ """)
253
+
254
  # Examples
255
  try:
256
  gr.Examples(