Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,8 +10,8 @@ transformers.logging.set_verbosity_error()
|
|
| 10 |
transformers.logging.disable_progress_bar()
|
| 11 |
warnings.filterwarnings('ignore')
|
| 12 |
|
| 13 |
-
# set device
|
| 14 |
-
device = torch.device("cuda
|
| 15 |
|
| 16 |
model_name = 'cognitivecomputations/dolphin-vision-7b'
|
| 17 |
|
|
@@ -19,9 +19,9 @@ model_name = 'cognitivecomputations/dolphin-vision-7b'
|
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
model_name,
|
| 21 |
torch_dtype=torch.float16,
|
| 22 |
-
|
| 23 |
trust_remote_code=True
|
| 24 |
-
).to(device)
|
| 25 |
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 27 |
model_name,
|
|
@@ -39,15 +39,14 @@ def inference(prompt, image):
|
|
| 39 |
)
|
| 40 |
|
| 41 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
| 42 |
-
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
| 43 |
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# Generate with autocast for mixed precision on the specified GPU
|
| 48 |
-
with torch.cuda.amp.autocast():
|
| 49 |
output_ids = model.generate(
|
| 50 |
-
input_ids
|
| 51 |
images=image_tensor,
|
| 52 |
max_new_tokens=2048,
|
| 53 |
use_cache=True
|
|
|
|
| 10 |
transformers.logging.disable_progress_bar()
|
| 11 |
warnings.filterwarnings('ignore')
|
| 12 |
|
| 13 |
+
# set device
|
| 14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
|
| 16 |
model_name = 'cognitivecomputations/dolphin-vision-7b'
|
| 17 |
|
|
|
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
model_name,
|
| 21 |
torch_dtype=torch.float16,
|
| 22 |
+
device_map='auto', # Keep auto device mapping
|
| 23 |
trust_remote_code=True
|
| 24 |
+
).to(device) # Explicitly move the model to the device
|
| 25 |
|
| 26 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 27 |
model_name,
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
| 42 |
+
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0).to(device) # Move input_ids to device
|
| 43 |
|
| 44 |
+
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype, device=device) # Move image_tensor to device
|
| 45 |
|
| 46 |
+
# generate
|
| 47 |
+
with torch.cuda.amp.autocast(): # Use autocast for mixed precision
|
|
|
|
|
|
|
| 48 |
output_ids = model.generate(
|
| 49 |
+
input_ids,
|
| 50 |
images=image_tensor,
|
| 51 |
max_new_tokens=2048,
|
| 52 |
use_cache=True
|