skallewag commited on
Commit
81c3554
·
verified ·
1 Parent(s): 687eada

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -96
app.py CHANGED
@@ -22,96 +22,37 @@ except Exception as e:
22
  print(f"Error installing detectron2: {e}")
23
  sys.exit(1)
24
 
25
- # Create a custom distributed.py file that doesn't need mpi4py
26
- os.makedirs('utils', exist_ok=True)
27
- with open('utils/distributed.py', 'w') as f:
28
- f.write("""# Custom distributed.py without mpi4py dependency
29
- import os
30
- import torch
31
- import torch.distributed as dist
32
-
33
- class MPI:
34
- class COMM_WORLD:
35
- @staticmethod
36
- def Get_rank():
37
- return 0
38
- @staticmethod
39
- def Get_size():
40
- return 1
41
-
42
- def init_distributed(opt=None):
43
- if opt is not None:
44
- opt.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
45
- opt.rank = 0
46
- opt.world_size = 1
47
- opt.gpu = 0
48
- return opt
49
-
50
- return None
51
-
52
- def get_rank():
53
- return 0
54
-
55
- def get_world_size():
56
- return 1
57
-
58
- def is_main_process():
59
- return True
60
-
61
- def synchronize():
62
- pass
63
-
64
- def all_gather(data):
65
- return [data]
66
-
67
- def reduce_dict(input_dict, average=True):
68
- return input_dict
69
- """)
70
-
71
- # Create a simple visualizer if it doesn't exist
72
- if not os.path.exists('utils/visualizer.py'):
73
- with open('utils/visualizer.py', 'w') as f:
74
- f.write("""# Simple visualizer class
75
- import numpy as np
76
- import cv2
77
-
78
- class Visualizer:
79
- def __init__(self, img_rgb, metadata=None, scale=1.0):
80
- self.img = img_rgb
81
- self.metadata = metadata
82
- self.scale = scale
83
-
84
- def draw_binary_mask(self, mask, color=None, text=None):
85
- if color is None:
86
- color = [0, 255, 0] # Default to green
87
-
88
- mask_img = np.zeros_like(self.img, dtype=np.float32)
89
- color_mask = np.array(color) * 255
90
-
91
- for c in range(3):
92
- mask_img[:, :, c] = color_mask[c]
93
-
94
- mask_img = mask_img * mask[:, :, None] * 0.5
95
- self.img = self.img * (1 - mask[:, :, None] * 0.5) + mask_img
96
-
97
- if text:
98
- # Simplified text placement
99
- x, y = np.where(mask)[0][0], np.where(mask)[1][0] if np.any(mask) else (10, 10)
100
- cv2.putText(self.img, text, (y, x), cv2.FONT_HERSHEY_SIMPLEX, 0.5, tuple(map(int, color_mask)), 1)
101
-
102
- return self
103
-
104
- def draw_panoptic_seg(self, panoptic_seg, segments_info):
105
- # Simplified panoptic visualization - just a placeholder
106
- return self
107
-
108
- def get_image(self):
109
- return self.img.astype(np.uint8)
110
- """)
111
-
112
- # Set Python path to include the repository root
113
- os.environ["PYTHONPATH"] = os.getcwd()
114
- print(f"Set PYTHONPATH to: {os.getcwd()}")
115
 
116
  # Continue with regular imports
117
  import warnings
@@ -132,8 +73,22 @@ from utils.distributed import init_distributed
132
  from utils.arguments import load_opt_from_config_files
133
  from utils.constants import COCO_PANOPTIC_CLASSES
134
 
135
- # Import the interactive functions from the existing implementation
136
- from demo.seem.tasks.interactive import interactive_infer_image, interactive_infer_video
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  def parse_option():
139
  parser = argparse.ArgumentParser('SEEM Demo', add_help=False)
@@ -215,10 +170,20 @@ def inference(image, task, *args, **kwargs):
215
  video_pth = kwargs.get("video", None)
216
 
217
  # Call the appropriate interactive function
218
- if 'Video' in task:
219
- return interactive_infer_video(model, audio, image_input, task, refimg, reftxt, audio_pth, video_pth)
220
- else:
221
- return interactive_infer_image(model, audio, image_input, task, refimg, reftxt, audio_pth, video_pth)
 
 
 
 
 
 
 
 
 
 
222
 
223
  class ImageMask(gr.components.Image):
224
  """
 
22
  print(f"Error installing detectron2: {e}")
23
  sys.exit(1)
24
 
25
+ # Fix the distributed.py file if it's causing issues
26
+ if os.path.exists('utils/distributed.py'):
27
+ with open('utils/distributed.py', 'r') as f:
28
+ content = f.read()
29
+ if 'from mpi4py import MPI' in content:
30
+ print("Patching utils/distributed.py to work without mpi4py")
31
+ patched_content = content.replace(
32
+ "from mpi4py import MPI",
33
+ """try:
34
+ from mpi4py import MPI
35
+ except ImportError:
36
+ # Dummy MPI implementation
37
+ class MPI:
38
+ class COMM_WORLD:
39
+ @staticmethod
40
+ def Get_rank():
41
+ return 0
42
+ @staticmethod
43
+ def Get_size():
44
+ return 1"""
45
+ )
46
+ with open('utils/distributed.py', 'w') as f:
47
+ f.write(patched_content)
48
+ print("Patched utils/distributed.py")
49
+
50
+ # Ensure the Python path includes the current directory
51
+ current_dir = os.getcwd()
52
+ if current_dir not in sys.path:
53
+ sys.path.insert(0, current_dir)
54
+ os.environ["PYTHONPATH"] = current_dir
55
+ print(f"Set PYTHONPATH to: {current_dir}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Continue with regular imports
58
  import warnings
 
73
  from utils.arguments import load_opt_from_config_files
74
  from utils.constants import COCO_PANOPTIC_CLASSES
75
 
76
+ # Import the interactive functions using a try-except block to catch import errors
77
+ try:
78
+ from demo.seem.tasks.interactive import interactive_infer_image, interactive_infer_video
79
+ print("Successfully imported interactive functions")
80
+ except ImportError as e:
81
+ print(f"Error importing interactive functions: {e}")
82
+ print("Python path:", sys.path)
83
+ print("Current directory:", os.getcwd())
84
+ print("Contents of current directory:", os.listdir('.'))
85
+ if os.path.exists('demo'):
86
+ print("Contents of demo directory:", os.listdir('demo'))
87
+ if os.path.exists('demo/seem'):
88
+ print("Contents of demo/seem directory:", os.listdir('demo/seem'))
89
+ if os.path.exists('demo/seem/tasks'):
90
+ print("Contents of demo/seem/tasks directory:", os.listdir('demo/seem/tasks'))
91
+ sys.exit(1)
92
 
93
  def parse_option():
94
  parser = argparse.ArgumentParser('SEEM Demo', add_help=False)
 
170
  video_pth = kwargs.get("video", None)
171
 
172
  # Call the appropriate interactive function
173
+ try:
174
+ if 'Video' in task:
175
+ return interactive_infer_video(model, audio, image_input, task, refimg, reftxt, audio_pth, video_pth)
176
+ else:
177
+ return interactive_infer_image(model, audio, image_input, task, refimg, reftxt, audio_pth, video_pth)
178
+ except Exception as e:
179
+ print(f"Error during inference: {e}")
180
+ import traceback
181
+ traceback.print_exc()
182
+ warning_img = Image.new('RGB', (600, 400), color=(240, 240, 240))
183
+ d = ImageDraw.Draw(warning_img)
184
+ d.text((50, 150), f"Error: {str(e)}", fill=(255, 0, 0))
185
+ d.text((50, 200), "Please check logs for details.", fill=(255, 0, 0))
186
+ return warning_img, None
187
 
188
  class ImageMask(gr.components.Image):
189
  """