Ketengan-Diffusion-Lab commited on
Commit
5220358
1 Parent(s): 225c3f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -10,17 +10,17 @@ transformers.logging.set_verbosity_error()
10
  transformers.logging.disable_progress_bar()
11
  warnings.filterwarnings('ignore')
12
 
13
- # Force CPU usage
14
- device = torch.device("cpu")
15
- torch.set_default_tensor_type(torch.FloatTensor)
16
 
17
  model_name = 'cognitivecomputations/dolphin-vision-7b'
18
 
19
- # create model and load it to CPU
20
  model = AutoModelForCausalLM.from_pretrained(
21
  model_name,
22
- torch_dtype=torch.float32, # Use float32 for CPU
23
- device_map={'': device},
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
- output_ids = model.generate(
54
- input_ids,
55
- images=image_tensor,
56
- max_new_tokens=2048,
57
- use_cache=True
58
- )[0]
 
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