assentian1970 commited on
Commit
ef750f9
·
verified ·
1 Parent(s): 5bdc624

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -28
app.py CHANGED
@@ -13,52 +13,59 @@ from transformers import AutoModel, AutoTokenizer
13
  from modelscope.hub.snapshot_download import snapshot_download
14
  from PIL import Image
15
  from decord import VideoReader, cpu
16
- import os
17
  import gc
18
  import io
19
  import tempfile
20
  from ultralytics import YOLO
21
  import numpy as np
22
  import cv2
 
 
23
 
24
  # Load YOLOv11 model (update the path as needed)
25
  YOLO_MODEL = YOLO('best_yolov11.pt')
26
 
27
- # Check if CUDA is available
28
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
29
 
30
- # Initialize GPU if available (already done by debug() above)
31
- if DEVICE == "cuda":
32
- def init_debug():
33
- torch.randn(10).cuda()
34
- init_debug()
35
 
36
- # File type validation
37
- IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'}
38
- VIDEO_EXTENSIONS = {'.mp4', '.mkv', '.mov', '.avi', '.flv', '.wmv', '.webm', '.m4v'}
39
 
40
- def get_file_extension(filename):
41
- return os.path.splitext(filename)[1].lower()
 
 
 
 
 
 
42
 
43
- def is_image(filename):
44
- return get_file_extension(filename) in IMAGE_EXTENSIONS
 
 
 
 
 
 
 
 
 
 
45
 
46
- def is_video(filename):
47
- return get_file_extension(filename) in VIDEO_EXTENSIONS
48
 
49
- # Model configuration
50
- MODEL_NAME = 'iic/mPLUG-Owl3-7B-240728'
51
- MODEL_CACHE_DIR = os.getenv('TRANSFORMERS_CACHE', './models')
52
- os.makedirs(MODEL_CACHE_DIR, exist_ok=True)
53
 
54
- # Download and cache the model
55
- try:
56
- model_path = snapshot_download(MODEL_NAME, cache_dir=MODEL_CACHE_DIR)
57
- except Exception as e:
58
- print(f"Error downloading model: {str(e)}")
59
- model_path = os.path.join(MODEL_CACHE_DIR, MODEL_NAME)
60
 
61
- MAX_NUM_FRAMES = 32
 
 
 
 
62
 
63
  def load_model_and_tokenizer():
64
  """Load a fresh instance of the model and tokenizer"""
 
13
  from modelscope.hub.snapshot_download import snapshot_download
14
  from PIL import Image
15
  from decord import VideoReader, cpu
16
+
17
  import gc
18
  import io
19
  import tempfile
20
  from ultralytics import YOLO
21
  import numpy as np
22
  import cv2
23
+ import os
24
+ os.system("nvidia-smi")
25
 
26
  # Load YOLOv11 model (update the path as needed)
27
  YOLO_MODEL = YOLO('best_yolov11.pt')
28
 
29
+ model_dir = snapshot_download('iic/mPLUG-Owl3-7B-240728', cache_dir='./')
30
+ os.system('ls')
31
+ # README, How to run demo on different devices
32
 
33
+ # For Nvidia GPUs.
34
+ # python web_demo_2.6.py --device cuda
 
 
 
35
 
36
+ # For Mac with MPS (Apple silicon or AMD GPUs).
37
+ # PYTORCH_ENABLE_MPS_FALLBACK=1 python web_demo_2.6.py --device mps
 
38
 
39
+ # Argparser
40
+ parser = argparse.ArgumentParser(description='demo')
41
+ parser.add_argument('--device', type=str, default='cuda', help='cuda or mps')
42
+ parser.add_argument("--host", type=str, default="0.0.0.0")
43
+ parser.add_argument("--port", type=int)
44
+ args = parser.parse_args()
45
+ device = args.device
46
+ assert device in ['cuda', 'mps']
47
 
48
+ # Load model
49
+ model_path = './iic/mPLUG-Owl3-7B-240728'
50
+ if 'int4' in model_path:
51
+ if device == 'mps':
52
+ print('Error: running int4 model with bitsandbytes on Mac is not supported right now.')
53
+ exit()
54
+ model = AutoModel.from_pretrained(model_path, attn_implementation='sdpa', trust_remote_code=True)
55
+ else:
56
+ model = AutoModel.from_pretrained(model_path, attn_implementation='sdpa', trust_remote_code=True, torch_dtype=torch.bfloat16)
57
+ model = model.to(device=device)
58
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
59
+ model.eval()
60
 
 
 
61
 
 
 
 
 
62
 
 
 
 
 
 
 
63
 
64
+ ERROR_MSG = "Error, please retry"
65
+ model_name = 'mPLUG-Owl3'
66
+ MAX_NUM_FRAMES = 64
67
+ IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'}
68
+ VIDEO_EXTENSIONS = {'.mp4', '.mkv', '.mov', '.avi', '.flv', '.wmv', '.webm', '.m4v'}
69
 
70
  def load_model_and_tokenizer():
71
  """Load a fresh instance of the model and tokenizer"""