Spaces:
Sleeping
Sleeping
updated caption.py
Browse files- utils/Caption.py +2 -39
utils/Caption.py
CHANGED
|
@@ -1,51 +1,14 @@
|
|
| 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
|
| 48 |
model = model.to(device='cuda')
|
|
|
|
| 49 |
model.eval()
|
| 50 |
question = "Describe the image."
|
| 51 |
msgs = [{'role': 'user', 'content': question}]
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
| 3 |
import spaces
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@spaces.GPU
|
| 7 |
def get_caption(image):
|
| 8 |
|
| 9 |
+
model = AutoModel.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5', trust_remote_code=True, torch_dtype=torch.float16)
|
| 10 |
model = model.to(device='cuda')
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5', trust_remote_code=True)
|
| 12 |
model.eval()
|
| 13 |
question = "Describe the image."
|
| 14 |
msgs = [{'role': 'user', 'content': question}]
|