Spaces:
Build error
Build error
Ketengan-Diffusion-Lab
commited on
Commit
•
5220358
1
Parent(s):
225c3f2
Update app.py
Browse files
app.py
CHANGED
@@ -10,17 +10,17 @@ transformers.logging.set_verbosity_error()
|
|
10 |
transformers.logging.disable_progress_bar()
|
11 |
warnings.filterwarnings('ignore')
|
12 |
|
13 |
-
#
|
14 |
-
device = torch.device("cpu")
|
15 |
-
|
16 |
|
17 |
model_name = 'cognitivecomputations/dolphin-vision-7b'
|
18 |
|
19 |
-
# create model and load it to
|
20 |
model = AutoModelForCausalLM.from_pretrained(
|
21 |
model_name,
|
22 |
-
torch_dtype=torch.
|
23 |
-
device_map=
|
24 |
trust_remote_code=True
|
25 |
)
|
26 |
|
@@ -40,9 +40,9 @@ def inference(prompt, image):
|
|
40 |
)
|
41 |
|
42 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
43 |
-
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
44 |
|
45 |
-
image_tensor = model.process_images([image], model.config)
|
46 |
|
47 |
# Add debug prints
|
48 |
print(f"Device of model: {next(model.parameters()).device}")
|
@@ -50,12 +50,13 @@ def inference(prompt, image):
|
|
50 |
print(f"Device of image_tensor: {image_tensor.device}")
|
51 |
|
52 |
# generate
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
|
60 |
return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
61 |
|
|
|
10 |
transformers.logging.disable_progress_bar()
|
11 |
warnings.filterwarnings('ignore')
|
12 |
|
13 |
+
# Set device to GPU if available, else CPU
|
14 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
+
print(f"Using device: {device}")
|
16 |
|
17 |
model_name = 'cognitivecomputations/dolphin-vision-7b'
|
18 |
|
19 |
+
# create model and load it to the specified device
|
20 |
model = AutoModelForCausalLM.from_pretrained(
|
21 |
model_name,
|
22 |
+
torch_dtype=torch.float16,
|
23 |
+
device_map="auto", # This will automatically use the GPU if available
|
24 |
trust_remote_code=True
|
25 |
)
|
26 |
|
|
|
40 |
)
|
41 |
|
42 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
43 |
+
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0).to(device)
|
44 |
|
45 |
+
image_tensor = model.process_images([image], model.config).to(device)
|
46 |
|
47 |
# Add debug prints
|
48 |
print(f"Device of model: {next(model.parameters()).device}")
|
|
|
50 |
print(f"Device of image_tensor: {image_tensor.device}")
|
51 |
|
52 |
# generate
|
53 |
+
with torch.cuda.amp.autocast():
|
54 |
+
output_ids = model.generate(
|
55 |
+
input_ids,
|
56 |
+
images=image_tensor,
|
57 |
+
max_new_tokens=1024,
|
58 |
+
use_cache=True
|
59 |
+
)[0]
|
60 |
|
61 |
return tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
|
62 |
|