salma-remyx commited on
Commit
eab56dc
1 Parent(s): 3ffd673

adding decorator

Browse files
Files changed (2) hide show
  1. app.py +7 -1
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import sys
3
  import uuid
 
4
  import torch
5
  import random
6
  import numpy as np
@@ -39,6 +40,7 @@ def extract_descriptions(doc, head):
39
  descriptions.append(chunk.text)
40
  return descriptions
41
 
 
42
  def caption_refiner(caption):
43
  doc = nlp(caption)
44
  subject, action_verb = find_subject(doc)
@@ -48,6 +50,7 @@ def caption_refiner(caption):
48
  else:
49
  return caption
50
 
 
51
  def sam2(image, input_boxes, model_id="facebook/sam-vit-base"):
52
  device = "cuda" if torch.cuda.is_available() else "cpu"
53
  model = SamModel.from_pretrained(model_id).to(device)
@@ -61,12 +64,14 @@ def sam2(image, input_boxes, model_id="facebook/sam-vit-base"):
61
  )
62
  return masks
63
 
 
64
  def load_florence2(model_id="microsoft/Florence-2-base-ft", device='cuda'):
65
  torch_dtype = torch.float16 if device == 'cuda' else torch.float32
66
  florence_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch_dtype, trust_remote_code=True).to(device)
67
  florence_processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
68
  return florence_model, florence_processor
69
 
 
70
  def florence2(image, prompt="", task="<OD>"):
71
  device = florence_model.device
72
  torch_dtype = florence_model.dtype
@@ -83,7 +88,7 @@ def florence2(image, prompt="", task="<OD>"):
83
  return parsed_answer[task]
84
 
85
 
86
- # Load and preprocess an image.
87
  def depth_estimation(image_path):
88
  model.eval()
89
  image, _, f_px = depth_pro.load_rgb(image_path)
@@ -261,6 +266,7 @@ def filter_bboxes(data, iou_threshold=0.5):
261
 
262
  return {'bboxes': filtered_bboxes, 'labels': filtered_labels, 'caption': data['caption']}
263
 
 
264
  def process_image(image_path: str):
265
  depth, fx = depth_estimation(image_path)
266
 
 
1
  import os
2
  import sys
3
  import uuid
4
+ import spaces
5
  import torch
6
  import random
7
  import numpy as np
 
40
  descriptions.append(chunk.text)
41
  return descriptions
42
 
43
+ @spaces.GPU
44
  def caption_refiner(caption):
45
  doc = nlp(caption)
46
  subject, action_verb = find_subject(doc)
 
50
  else:
51
  return caption
52
 
53
+ @spaces.GPU
54
  def sam2(image, input_boxes, model_id="facebook/sam-vit-base"):
55
  device = "cuda" if torch.cuda.is_available() else "cpu"
56
  model = SamModel.from_pretrained(model_id).to(device)
 
64
  )
65
  return masks
66
 
67
+ @spaces.GPU
68
  def load_florence2(model_id="microsoft/Florence-2-base-ft", device='cuda'):
69
  torch_dtype = torch.float16 if device == 'cuda' else torch.float32
70
  florence_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch_dtype, trust_remote_code=True).to(device)
71
  florence_processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
72
  return florence_model, florence_processor
73
 
74
+ @spaces.GPU
75
  def florence2(image, prompt="", task="<OD>"):
76
  device = florence_model.device
77
  torch_dtype = florence_model.dtype
 
88
  return parsed_answer[task]
89
 
90
 
91
+ @spaces.GPU
92
  def depth_estimation(image_path):
93
  model.eval()
94
  image, _, f_px = depth_pro.load_rgb(image_path)
 
266
 
267
  return {'bboxes': filtered_bboxes, 'labels': filtered_labels, 'caption': data['caption']}
268
 
269
+ @spaces.GPU
270
  def process_image(image_path: str):
271
  depth, fx = depth_estimation(image_path)
272
 
requirements.txt CHANGED
@@ -23,6 +23,7 @@ spacy==3.7.5
23
  onnxruntime-gpu
24
  pandas
25
  html5lib
 
26
 
27
  git+https://github.com/apple/ml-depth-pro.git
28
  git+https://github.com/facebookresearch/sam2.git
 
23
  onnxruntime-gpu
24
  pandas
25
  html5lib
26
+ spaces
27
 
28
  git+https://github.com/apple/ml-depth-pro.git
29
  git+https://github.com/facebookresearch/sam2.git