File size: 17,317 Bytes
e59dc66
 
 
 
 
 
57d5e90
f666747
02532a9
 
45a88d3
 
 
 
 
e59dc66
 
 
 
02532a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f666747
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b040dc
57d5e90
 
 
 
e59dc66
f8e5af1
 
 
02532a9
 
f8e5af1
 
 
 
 
 
02532a9
f8e5af1
 
02532a9
f8e5af1
 
 
 
 
57d5e90
e59dc66
57d5e90
f8e5af1
 
 
 
 
 
57d5e90
 
 
 
 
 
02532a9
57d5e90
02532a9
57d5e90
 
 
e59dc66
 
 
57d5e90
e59dc66
 
 
 
57d5e90
 
 
 
 
 
f8e5af1
 
 
 
 
 
e59dc66
 
 
da1f9eb
 
 
 
 
 
 
e59dc66
da1f9eb
 
 
 
e59dc66
da1f9eb
 
e59dc66
 
45a88d3
 
 
 
da1f9eb
 
e59dc66
 
 
57d5e90
e59dc66
 
 
 
 
f8e5af1
 
57d5e90
e59dc66
 
 
 
 
 
 
57d5e90
 
 
 
 
f8e5af1
 
 
 
02532a9
f8e5af1
e59dc66
 
57d5e90
e59dc66
 
 
 
 
 
 
da1f9eb
 
 
 
e59dc66
da1f9eb
 
b3a024e
da1f9eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3a024e
da1f9eb
 
 
e59dc66
da1f9eb
 
 
 
 
e59dc66
da1f9eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3a024e
da1f9eb
 
 
 
 
 
e59dc66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02532a9
 
 
 
 
 
 
 
 
 
 
e59dc66
 
f8e5af1
57d5e90
 
 
f8e5af1
 
 
e59dc66
 
 
 
 
 
 
 
 
f8e5af1
 
 
 
e59dc66
 
 
f8e5af1
02532a9
 
 
 
 
 
 
 
 
e59dc66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8e5af1
 
 
 
 
 
 
 
 
 
 
e59dc66
f666747
 
 
 
 
 
 
 
 
 
2d5a207
f666747
 
 
e59dc66
 
 
 
 
 
 
 
2d5a207
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import gradio as gr
from PIL import Image
import os
import time
import numpy as np
import torch
import warnings
import stat
import subprocess
import sys
import asyncio
import nest_asyncio

# Apply nest_asyncio to allow nested event loops
nest_asyncio.apply()

# Set environment variables
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"

# Print system information
print(f"Python version: {sys.version}")
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available via PyTorch: {torch.cuda.is_available()}")
print(f"CUDA version via PyTorch: {torch.version.cuda if torch.cuda.is_available() else 'Not available'}")

# Try to run nvidia-smi
def run_nvidia_smi():
    try:
        result = subprocess.run(['nvidia-smi'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        if result.returncode == 0:
            print("nvidia-smi output:")
            print(result.stdout)
            return True
        else:
            print("nvidia-smi error:")
            print(result.stderr)
            return False
    except Exception as e:
        print(f"Error running nvidia-smi: {str(e)}")
        return False

# Run nvidia-smi
nvidia_smi_available = run_nvidia_smi()
print(f"nvidia-smi available: {nvidia_smi_available}")

# Show CUDA devices
if torch.cuda.is_available():
    print(f"CUDA device count: {torch.cuda.device_count()}")
    for i in range(torch.cuda.device_count()):
        print(f"CUDA Device {i}: {torch.cuda.get_device_name(i)}")
    print(f"Current CUDA device: {torch.cuda.current_device()}")

# Ensure all cache directories exist with proper permissions
def setup_cache_directories():
    # Gradio cache directory
    cache_dir = os.path.join(os.getcwd(), "gradio_cached_examples")
    os.makedirs(cache_dir, exist_ok=True)
    
    # HuggingFace cache directories
    hf_cache = os.path.join(os.getcwd(), ".cache", "huggingface")
    transformers_cache = os.path.join(hf_cache, "transformers")
    os.makedirs(hf_cache, exist_ok=True)
    os.makedirs(transformers_cache, exist_ok=True)
    
    # Set permissions
    try:
        for directory in [cache_dir, hf_cache, transformers_cache]:
            if os.path.exists(directory):
                os.chmod(directory, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)  # 0o777
                print(f"Set permissions for {directory}")
    except Exception as e:
        print(f"Warning: Could not set permissions: {str(e)}")
    
    return cache_dir

# Set up cache directories
cache_dir = setup_cache_directories()

# Suppress specific warnings that might be caused by package version mismatches
warnings.filterwarnings("ignore", message=".*The 'nopython' keyword.*")
warnings.filterwarnings("ignore", message=".*Torch is not compiled with CUDA enabled.*")
warnings.filterwarnings("ignore", category=UserWarning)

# Check for actual GPU availability
def check_gpu_availability():
    """Check if GPU is actually available and working"""
    print("Checking GPU availability...")
    
    if not torch.cuda.is_available():
        print("CUDA is not available in PyTorch")
        return False
    
    try:
        # Try to initialize CUDA and run a simple operation
        print("Attempting to create a tensor on CUDA...")
        x = torch.rand(10, device="cuda")
        y = x + x
        print("Successfully created and operated on CUDA tensor")
        return True
    except Exception as e:
        print(f"GPU initialization failed: {str(e)}")
        return False

# Global variables
internvl2_pipeline = None
MODEL_LOADED = False
USE_GPU = check_gpu_availability()

if USE_GPU:
    print("GPU is available and working properly")
else:
    print("WARNING: GPU is not available or not working properly. This application requires GPU acceleration.")

# Check if lmdeploy is available and try to import
try:
    from lmdeploy import pipeline, TurbomindEngineConfig
    LMDEPLOY_AVAILABLE = True
    print("Successfully imported lmdeploy")
except ImportError as e:
    LMDEPLOY_AVAILABLE = False
    print(f"lmdeploy import failed: {str(e)}. Will use a placeholder for demos.")

# Model configuration
MODEL_ID = "OpenGVLab/InternVL2-40B-AWQ"  # 4-bit quantized model

def load_internvl2_model():
    """Load the InternVL2 model using lmdeploy"""
    global internvl2_pipeline, MODEL_LOADED
    
    # If already loaded, return
    if internvl2_pipeline is not None:
        return True
    
    # If lmdeploy is not available, we'll use a demo placeholder
    if not LMDEPLOY_AVAILABLE:
        print("lmdeploy not available. Using demo placeholder.")
        MODEL_LOADED = False
        return False

    # Check if GPU is available
    if not USE_GPU:
        print("Cannot load InternVL2 model without GPU acceleration.")
        MODEL_LOADED = False
        return False
        
    print("Loading InternVL2 model...")
    try:
        # Force synchronous execution for everything
        import os
        # Set environment variables to force synchronous behavior
        os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
        # Disable asyncio in lmdeploy
        os.environ["LMDEPLOY_DISABLE_ASYNC"] = "1"
        
        # Configure for AWQ quantized model
        backend_config = TurbomindEngineConfig(
            model_format='awq',
            session_len=2048  # Explicitly set session length
        )
        
        # Create a synchronous pipeline to avoid asyncio issues
        # Explicitly set all parameters that might default to async behavior
        internvl2_pipeline = pipeline(
            MODEL_ID, 
            backend_config=backend_config,
            log_level='INFO',
            model_name_or_path=None,
            backend_name="turbomind",
            stream=False,  # Important: disable streaming
            tensor_parallel=1,  # Use single GPU to avoid distributed processing
        )
        
        print("InternVL2 model loaded successfully!")
        MODEL_LOADED = True
        return True
    except Exception as e:
        print(f"Error loading InternVL2 model: {str(e)}")
        if "CUDA out of memory" in str(e):
            print("Not enough GPU memory for the model")
        elif "Found no NVIDIA driver" in str(e):
            print("NVIDIA GPU driver not found or not properly configured")
        MODEL_LOADED = False
        return False

def analyze_image(image, prompt):
    """Analyze the image using InternVL2 model"""
    try:
        start_time = time.time()
        
        # Skip model loading if lmdeploy is not available
        if not LMDEPLOY_AVAILABLE:
            return ("This is a demo placeholder. The actual model couldn't be loaded because lmdeploy " 
                   "is not properly installed. Check your installation and dependencies.")
        
        # Check for GPU
        if not USE_GPU:
            return ("ERROR: This application requires a GPU to run InternVL2. "
                  "The NVIDIA driver was not detected on this system. "
                  "Please make sure this Space is using a GPU-enabled instance and that the GPU is correctly initialized.")
        
        # Make sure the model is loaded
        if not load_internvl2_model():
            return "Couldn't load InternVL2 model. See logs for details."
            
        # Convert numpy array to PIL Image
        if isinstance(image, np.ndarray):
            image_pil = Image.fromarray(image).convert('RGB')
        else:
            # If somehow it's already a PIL Image
            image_pil = image.convert('RGB')

        # We'll use a completely different approach - multiprocessing
        # This runs the model in a separate process, avoiding any event loop conflicts
        import multiprocessing as mp
        
        # Define a function to run in a separate process
        def run_in_process(prompt, image_path, result_queue):
            try:
                # Set environment variables in the subprocess
                import os
                os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
                os.environ["LMDEPLOY_DISABLE_ASYNC"] = "1"
                
                # Import libraries inside the process
                from lmdeploy import pipeline, TurbomindEngineConfig
                
                # Save the image to a temporary file to pass between processes
                import tempfile
                import torch
                
                # Check GPU in subprocess
                print(f"Subprocess GPU available: {torch.cuda.is_available()}")
                
                # Configure for AWQ quantized model
                backend_config = TurbomindEngineConfig(
                    model_format='awq',
                    session_len=2048
                )
                
                # Create new pipeline in the subprocess
                model_pipeline = pipeline(
                    MODEL_ID, 
                    backend_config=backend_config,
                    log_level='INFO',
                    model_name_or_path=None,
                    backend_name="turbomind",
                    stream=False,
                    tensor_parallel=1,
                )
                
                # Load the image in the subprocess
                from PIL import Image
                image = Image.open(image_path).convert('RGB')
                
                # Run inference
                response = model_pipeline((prompt, image))
                result = response.text if hasattr(response, "text") else str(response)
                
                # Put the result in the queue
                result_queue.put(("success", result))
                
            except Exception as e:
                import traceback
                error_msg = f"Error in subprocess: {str(e)}\n{traceback.format_exc()}"
                result_queue.put(("error", error_msg))
        
        # Create a temporary file for the image
        import tempfile
        with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
            temp_path = temp_file.name
            image_pil.save(temp_path)
        
        try:
            # Create a process-safe queue
            result_queue = mp.Queue()
            
            # Start the process
            print("Starting model inference in a separate process")
            process = mp.Process(
                target=run_in_process, 
                args=(prompt, temp_path, result_queue)
            )
            
            # Make it a daemon so it terminates when the main process ends
            process.daemon = True
            process.start()
            
            # Wait for the process to complete (with timeout)
            process.join(timeout=180)  # 3 minute timeout
            
            # Delete the temporary file
            try:
                os.unlink(temp_path)
            except:
                pass
                
            if process.is_alive():
                # Terminate the process if it's still running after timeout
                process.terminate()
                return "Model inference timed out after 180 seconds. The model might be too slow on this hardware."
            
            # Get the result from the queue (non-blocking to avoid hanging)
            if not result_queue.empty():
                status, result = result_queue.get(block=False)
                if status == "error":
                    return f"Error in model inference: {result}"
                else:
                    elapsed_time = time.time() - start_time
                    return result
            else:
                return "Unknown error: Model inference process completed but did not produce a result"
            
        except Exception as e:
            print(f"Error in multiprocessing: {str(e)}")
            return f"Error setting up multiprocessing: {str(e)}"
            
    except Exception as e:
        print(f"Error in image analysis: {str(e)}")
        # Try to clean up memory in case of error
        if USE_GPU:
            torch.cuda.empty_cache()
        return f"Error in image analysis: {str(e)}"

def process_image(image, analysis_type="general"):
    """Process the image and return the analysis"""
    if image is None:
        return "Please upload an image."
    
    # Define prompt based on analysis type
    if analysis_type == "general":
        prompt = "Describe this image in detail."
    elif analysis_type == "text":
        prompt = "What text can you see in this image? Please transcribe it accurately."
    elif analysis_type == "chart":
        prompt = "Analyze any charts, graphs or diagrams in this image in detail, including trends, data points, and conclusions."
    elif analysis_type == "people":
        prompt = "Describe the people in this image - their appearance, actions, and expressions."
    elif analysis_type == "technical":
        prompt = "Provide a technical analysis of this image, including object identification, spatial relationships, and any technical elements present."
    else:
        prompt = "Describe this image in detail."
    
    start_time = time.time()
    
    # Get analysis from the model
    analysis = analyze_image(image, prompt)
    
    elapsed_time = time.time() - start_time
    return f"{analysis}\n\nAnalysis completed in {elapsed_time:.2f} seconds."

# Define the Gradio interface
def create_interface():
    with gr.Blocks(title="Image Analysis with InternVL2") as demo:
        gr.Markdown("# Image Analysis with InternVL2-40B")
        
        # System diagnostics
        system_info = f"""
        ## System Diagnostics:
        - PyTorch Version: {torch.__version__}
        - CUDA Available: {torch.cuda.is_available()}
        - GPU Working: {USE_GPU}
        - nvidia-smi Available: {nvidia_smi_available}
        """
        
        gr.Markdown(system_info)
        gr.Markdown("Upload an image to analyze it using the InternVL2-40B model.")
        
        # Show warnings based on system status
        if not LMDEPLOY_AVAILABLE:
            gr.Markdown("⚠️ **WARNING**: lmdeploy is not properly installed. This demo will not function correctly.", elem_classes=["warning-message"])
        
        if not USE_GPU:
            gr.Markdown("🚫 **ERROR**: NVIDIA GPU not detected. This application requires GPU acceleration to run InternVL2 model.", elem_classes=["error-message"])
        
        with gr.Row():
            with gr.Column(scale=1):
                input_image = gr.Image(type="pil", label="Upload Image")
                analysis_type = gr.Radio(
                    ["general", "text", "chart", "people", "technical"],
                    label="Analysis Type",
                    value="general"
                )
                submit_btn = gr.Button("Analyze Image")
                
                # Disable button if GPU is not available
                if not USE_GPU:
                    submit_btn.interactive = False
            
            with gr.Column(scale=2):
                output_text = gr.Textbox(label="Analysis Result", lines=20)
                if not USE_GPU:
                    output_text.value = f"""ERROR: NVIDIA GPU driver not detected. This application requires GPU acceleration to run the InternVL2 model.

Diagnostics:
- PyTorch Version: {torch.__version__}
- CUDA Available via PyTorch: {torch.cuda.is_available()}
- nvidia-smi Available: {nvidia_smi_available}
- GPU Working: {USE_GPU}

Please ensure this Space is using a GPU-enabled instance and that the GPU is correctly initialized."""
        
        submit_btn.click(
            fn=process_image,
            inputs=[input_image, analysis_type],
            outputs=output_text
        )
        
        gr.Markdown("""
        ## Analysis Types
        - **General**: General description of the image
        - **Text**: Focus on identifying and transcribing text in the image
        - **Chart**: Detailed analysis of charts, graphs, and diagrams
        - **People**: Description of people, their appearance and actions
        - **Technical**: Technical analysis identifying objects and spatial relationships
        """)
        
        # Hardware requirements notice
        gr.Markdown("""
        ## System Requirements
        This application requires:
        - NVIDIA GPU with CUDA support
        - At least 16GB of GPU memory recommended
        - GPU drivers properly installed and configured
        
        If you're running this on Hugging Face Spaces, make sure to select a GPU-enabled hardware type.
        """)
        
        # Examples
        try:
            gr.Examples(
                examples=[
                    ["data_temp/page_2.png", "general"],
                    ["data_temp/page_2.png", "text"],
                    ["data_temp/page_2.png", "chart"]
                ],
                inputs=[input_image, analysis_type],
                outputs=output_text,
                fn=process_image,
                cache_examples=True
            )
        except Exception as e:
            print(f"Warning: Could not load examples: {str(e)}")
    
    return demo

# Main function
if __name__ == "__main__":
    # Create the Gradio interface
    demo = create_interface()
    
    # Launch the interface (removed incompatible parameters)
    demo.launch(share=False, server_name="0.0.0.0")