Spaces:
Sleeping
Sleeping
updated caption.py so that model is downloaded only once
Browse files- utils/Caption.py +40 -2
utils/Caption.py
CHANGED
@@ -1,13 +1,51 @@
|
|
1 |
import torch
|
2 |
from transformers import AutoModel, AutoTokenizer
|
|
|
3 |
import spaces
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@spaces.GPU
|
6 |
def get_caption(image):
|
7 |
|
8 |
-
model =
|
9 |
model = model.to(device='cuda')
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5', trust_remote_code=True)
|
11 |
model.eval()
|
12 |
question = "Describe the image."
|
13 |
msgs = [{'role': 'user', 'content': question}]
|
|
|
1 |
import torch
|
2 |
from transformers import AutoModel, AutoTokenizer
|
3 |
+
import os
|
4 |
import spaces
|
5 |
|
6 |
+
def download_model_and_tokenizer():
|
7 |
+
"""Download model and tokenizer to the specified directory."""
|
8 |
+
print("Downloading model and tokenizer...")
|
9 |
+
model = AutoModel.from_pretrained(
|
10 |
+
'openbmb/MiniCPM-Llama3-V-2_5',
|
11 |
+
trust_remote_code=True,
|
12 |
+
torch_dtype=torch.float16,
|
13 |
+
cache_dir='models/MiniCPM'
|
14 |
+
)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
16 |
+
'openbmb/MiniCPM-Llama3-V-2_5',
|
17 |
+
trust_remote_code=True,
|
18 |
+
cache_dir='models/MiniCPM'
|
19 |
+
)
|
20 |
+
print("Download complete.")
|
21 |
+
return model, tokenizer
|
22 |
+
|
23 |
+
def load_model_and_tokenizer():
|
24 |
+
"""Load the model and tokenizer, downloading them if necessary."""
|
25 |
+
model_dir = 'models/MiniCPM'
|
26 |
+
|
27 |
+
# Check if directory exists and contains files
|
28 |
+
if not os.path.exists(model_dir) or not os.listdir(model_dir):
|
29 |
+
# If folder doesn't exist or is empty, download the model and tokenizer
|
30 |
+
os.makedirs(model_dir, exist_ok=True)
|
31 |
+
model, tokenizer = download_model_and_tokenizer()
|
32 |
+
else:
|
33 |
+
print("Loading model and tokenizer from local directory...")
|
34 |
+
model = AutoModel.from_pretrained(
|
35 |
+
model_dir,
|
36 |
+
trust_remote_code=True,
|
37 |
+
torch_dtype=torch.float16
|
38 |
+
)
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
40 |
+
model_dir,
|
41 |
+
trust_remote_code=True
|
42 |
+
)
|
43 |
+
return model, tokenizer
|
44 |
@spaces.GPU
|
45 |
def get_caption(image):
|
46 |
|
47 |
+
model, tokenizer = load_model_and_tokenizer()
|
48 |
model = model.to(device='cuda')
|
|
|
49 |
model.eval()
|
50 |
question = "Describe the image."
|
51 |
msgs = [{'role': 'user', 'content': question}]
|