dockerfile
Browse files- app.py +25 -4
- dockerfile +27 -0
- llava_inference.py +91 -21
app.py
CHANGED
@@ -2,15 +2,36 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import sys
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Add error handling for module imports
|
8 |
try:
|
9 |
-
|
|
|
10 |
except Exception as e:
|
11 |
-
|
12 |
-
|
13 |
model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def answer_question(image, question):
|
16 |
if model is None:
|
|
|
2 |
from PIL import Image
|
3 |
import os
|
4 |
import sys
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Set up logging
|
8 |
+
logging.basicConfig(
|
9 |
+
level=logging.INFO,
|
10 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
11 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
12 |
+
)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
+
logger.info("Starting UK Public Transport Assistant app")
|
16 |
|
17 |
# Add error handling for module imports
|
18 |
try:
|
19 |
+
from llava_inference import LLaVAHelper
|
20 |
+
logger.info("Successfully imported LLaVAHelper")
|
21 |
except Exception as e:
|
22 |
+
logger.error(f"Failed to import LLaVAHelper: {e}")
|
23 |
+
logger.error("Stack trace:", exc_info=True)
|
24 |
model = None
|
25 |
+
else:
|
26 |
+
# Initialize model
|
27 |
+
try:
|
28 |
+
logger.info("Initializing LLaVA model...")
|
29 |
+
model = LLaVAHelper()
|
30 |
+
logger.info("LLaVA model initialized successfully")
|
31 |
+
except Exception as e:
|
32 |
+
logger.error(f"Failed to initialize LLaVA model: {e}")
|
33 |
+
logger.error("Stack trace:", exc_info=True)
|
34 |
+
model = None
|
35 |
|
36 |
def answer_question(image, question):
|
37 |
if model is None:
|
dockerfile
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
# Install system dependencies
|
6 |
+
RUN apt-get update && apt-get install -y \
|
7 |
+
build-essential \
|
8 |
+
git \
|
9 |
+
&& rm -rf /var/lib/apt/lists/*
|
10 |
+
|
11 |
+
# Copy requirements first for better caching
|
12 |
+
COPY requirements.txt .
|
13 |
+
|
14 |
+
# Install Python dependencies
|
15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
16 |
+
|
17 |
+
# Copy application code
|
18 |
+
COPY . .
|
19 |
+
|
20 |
+
# Make sure the cache directory exists
|
21 |
+
RUN mkdir -p ./model_cache
|
22 |
+
|
23 |
+
# Expose port for Gradio
|
24 |
+
EXPOSE 7860
|
25 |
+
|
26 |
+
# Start the application
|
27 |
+
CMD ["python", "app.py"]
|
llava_inference.py
CHANGED
@@ -1,45 +1,115 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from transformers import AutoTokenizer, AutoConfig
|
4 |
-
import torch
|
5 |
-
import requests
|
6 |
-
from PIL import Image
|
7 |
-
from io import BytesIO
|
8 |
import os
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
class LLaVAHelper:
|
11 |
def __init__(self, model_name="llava-hf/llava-1.5-7b-hf"):
|
|
|
|
|
|
|
|
|
|
|
12 |
# Create cache directory if it doesn't exist
|
13 |
os.makedirs("./model_cache", exist_ok=True)
|
|
|
14 |
|
15 |
-
#
|
16 |
try:
|
17 |
AutoConfig.from_pretrained(model_name)
|
|
|
18 |
except Exception as e:
|
19 |
-
|
20 |
# Try a different model version as fallback
|
21 |
model_name = "llava-hf/llava-1.5-13b-hf"
|
22 |
-
|
23 |
|
24 |
try:
|
25 |
# Use specific tokenizer class to avoid issues
|
|
|
26 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
27 |
model_name,
|
28 |
cache_dir="./model_cache",
|
29 |
use_fast=False, # Use the Python implementation instead of the Rust one
|
30 |
-
|
31 |
)
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
self.model.eval()
|
45 |
|
|
|
1 |
+
import sys
|
2 |
+
import logging
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
|
5 |
+
# Configure logging
|
6 |
+
logging.basicConfig(
|
7 |
+
level=logging.INFO,
|
8 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
9 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
10 |
+
)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
# First try to import from llava
|
14 |
+
try:
|
15 |
+
from llava.model.builder import load_pretrained_model
|
16 |
+
from llava.mm_utils import process_images, tokenizer_image_token
|
17 |
+
logger.info("Successfully imported llava modules")
|
18 |
+
except ImportError as e:
|
19 |
+
logger.error(f"Failed to import llava modules: {e}")
|
20 |
+
sys.exit(1)
|
21 |
+
|
22 |
+
# Then import other dependencies
|
23 |
+
try:
|
24 |
+
from transformers import AutoTokenizer, AutoConfig
|
25 |
+
import torch
|
26 |
+
import requests
|
27 |
+
from PIL import Image
|
28 |
+
from io import BytesIO
|
29 |
+
logger.info("Successfully imported other required modules")
|
30 |
+
except ImportError as e:
|
31 |
+
logger.error(f"Failed to import dependency: {e}")
|
32 |
+
sys.exit(1)
|
33 |
+
|
34 |
class LLaVAHelper:
|
35 |
def __init__(self, model_name="llava-hf/llava-1.5-7b-hf"):
|
36 |
+
"""
|
37 |
+
Initialize the LLaVA model for image-text processing
|
38 |
+
"""
|
39 |
+
logger.info(f"Initializing LLaVAHelper with model: {model_name}")
|
40 |
+
|
41 |
# Create cache directory if it doesn't exist
|
42 |
os.makedirs("./model_cache", exist_ok=True)
|
43 |
+
logger.info("Created model cache directory")
|
44 |
|
45 |
+
# Try loading just the config to ensure the model is valid
|
46 |
try:
|
47 |
AutoConfig.from_pretrained(model_name)
|
48 |
+
logger.info(f"Successfully loaded config for {model_name}")
|
49 |
except Exception as e:
|
50 |
+
logger.warning(f"Error loading model config: {e}")
|
51 |
# Try a different model version as fallback
|
52 |
model_name = "llava-hf/llava-1.5-13b-hf"
|
53 |
+
logger.info(f"Trying alternative model: {model_name}")
|
54 |
|
55 |
try:
|
56 |
# Use specific tokenizer class to avoid issues
|
57 |
+
logger.info("Loading tokenizer...")
|
58 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
59 |
model_name,
|
60 |
cache_dir="./model_cache",
|
61 |
use_fast=False, # Use the Python implementation instead of the Rust one
|
62 |
+
trust_remote_code=True
|
63 |
)
|
64 |
+
logger.info("Tokenizer loaded successfully")
|
65 |
|
66 |
+
# Inspect the load_pretrained_model function to understand its parameters
|
67 |
+
import inspect
|
68 |
+
logger.info(f"load_pretrained_model signature: {inspect.signature(load_pretrained_model)}")
|
69 |
+
|
70 |
+
# Try loading with different parameter combinations
|
71 |
+
logger.info("Loading model...")
|
72 |
+
try:
|
73 |
+
# First attempt - standard parameter order
|
74 |
+
self.model, self.image_processor, _ = load_pretrained_model(
|
75 |
+
model_path=model_name,
|
76 |
+
model_base=None,
|
77 |
+
cache_dir="./model_cache",
|
78 |
+
)
|
79 |
+
except Exception as e1:
|
80 |
+
logger.warning(f"First attempt to load model failed: {e1}")
|
81 |
+
try:
|
82 |
+
# Second attempt - try with model_name parameter
|
83 |
+
self.model, self.image_processor, _ = load_pretrained_model(
|
84 |
+
model_name=model_name,
|
85 |
+
model_path=model_name,
|
86 |
+
model_base=None,
|
87 |
+
cache_dir="./model_cache",
|
88 |
+
)
|
89 |
+
except Exception as e2:
|
90 |
+
logger.warning(f"Second attempt to load model failed: {e2}")
|
91 |
+
# Third attempt - minimal parameters
|
92 |
+
self.model, self.image_processor, _ = load_pretrained_model(
|
93 |
+
model_name,
|
94 |
+
None,
|
95 |
+
"./model_cache",
|
96 |
+
)
|
97 |
+
|
98 |
+
logger.info("Model loaded successfully")
|
99 |
+
self.model.eval()
|
100 |
+
|
101 |
+
# Move model to appropriate device
|
102 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
103 |
+
logger.info(f"Using device: {self.device}")
|
104 |
+
if self.device == "cpu":
|
105 |
+
# If using CPU, make sure model is in the right place
|
106 |
+
self.model = self.model.to(self.device)
|
107 |
+
|
108 |
+
logger.info(f"Model successfully loaded on {self.device}")
|
109 |
+
except Exception as e:
|
110 |
+
logger.error(f"Detailed initialization error: {e}")
|
111 |
+
logger.error("Stack trace:", exc_info=True)
|
112 |
+
raise
|
113 |
|
114 |
self.model.eval()
|
115 |
|