Ketengan-Diffusion-Lab commited on
Commit
f8d9f18
·
verified ·
1 Parent(s): cd44f8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -11,7 +11,7 @@ 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,9 +19,9 @@ model_name = 'cognitivecomputations/dolphin-vision-7b'
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,12 +39,12 @@ 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).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,
@@ -65,4 +65,4 @@ with gr.Blocks() as demo:
65
 
66
  submit_button.click(fn=inference, inputs=[prompt_input, image_input], outputs=output_text)
67
 
68
- demo.launch()
 
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',
23
  trust_remote_code=True
24
+ )
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)
43
 
44
+ image_tensor = model.process_images([image], model.config).to(device)
45
 
46
  # generate
47
+ with torch.cuda.amp.autocast():
48
  output_ids = model.generate(
49
  input_ids,
50
  images=image_tensor,
 
65
 
66
  submit_button.click(fn=inference, inputs=[prompt_input, image_input], outputs=output_text)
67
 
68
+ demo.launch(share=True)