Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ transformers.logging.disable_progress_bar()
|
|
| 11 |
warnings.filterwarnings('ignore')
|
| 12 |
|
| 13 |
# set device
|
| 14 |
-
torch.set_default_device('cuda
|
| 15 |
|
| 16 |
model_name = 'cognitivecomputations/dolphin-vision-7b'
|
| 17 |
|
|
@@ -38,14 +38,17 @@ def inference(prompt, image):
|
|
| 38 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
| 39 |
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
| 40 |
|
| 41 |
-
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype, device=model.device) # Move to model's device
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
| 51 |
|
|
|
|
| 11 |
warnings.filterwarnings('ignore')
|
| 12 |
|
| 13 |
# set device
|
| 14 |
+
torch.set_default_device('cuda') # or 'cpu'
|
| 15 |
|
| 16 |
model_name = 'cognitivecomputations/dolphin-vision-7b'
|
| 17 |
|
|
|
|
| 38 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
| 39 |
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
| 40 |
|
|
|
|
| 41 |
|
| 42 |
+
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype, device=model.device)
|
| 43 |
+
|
| 44 |
+
# Generate with autocast for mixed precision on GPU
|
| 45 |
+
with torch.cuda.amp.autocast():
|
| 46 |
+
output_ids = model.generate(
|
| 47 |
+
input_ids.to(model.device), # Move input_ids to GPU
|
| 48 |
+
images=image_tensor,
|
| 49 |
+
max_new_tokens=2048,
|
| 50 |
+
use_cache=True
|
| 51 |
+
)[0]
|
| 52 |
|
| 53 |
return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
| 54 |
|