skallewag commited on
Commit
235a666
·
verified ·
1 Parent(s): 35fadf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -36
app.py CHANGED
@@ -10,9 +10,9 @@
10
  import os
11
  import sys
12
  import subprocess
13
- import importlib.util
14
  import logging
15
  import time
 
16
 
17
  # Configure logging
18
  logging.basicConfig(level=logging.INFO,
@@ -44,49 +44,122 @@ def run_command(cmd, description=None):
44
 
45
  def install_dependencies():
46
  """Install required dependencies"""
 
 
 
47
  # Check if ffmpeg is installed
48
  logger.info("Checking for ffmpeg...")
49
  if not run_command("which ffmpeg", "Checking ffmpeg"):
50
  logger.info("Installing ffmpeg...")
51
- run_command("apt-get update && apt-get install -y ffmpeg", "Installing ffmpeg")
52
 
53
  # Install Python dependencies
54
  logger.info("Installing Python dependencies...")
 
 
55
  if os.path.exists("assets/requirements/requirements.txt"):
56
  run_command("pip install -r assets/requirements/requirements.txt", "Installing base requirements")
57
- else:
58
- logger.warning("Base requirements file not found, creating minimal requirements")
59
- with open("requirements.txt", "w") as f:
60
- f.write("""torch>=1.12.0
61
- torchvision>=0.13.0
62
- opencv-python-headless>=4.5.0
63
- numpy>=1.23.5
64
- gradio>=3.13.0
65
- Pillow>=9.0.0
66
- openai-whisper
67
- """)
68
- run_command("pip install -r requirements.txt", "Installing minimal requirements")
69
 
 
70
  if os.path.exists("assets/requirements/requirements_custom.txt"):
71
  run_command("pip install -r assets/requirements/requirements_custom.txt", "Installing custom requirements")
 
 
 
 
 
 
 
 
72
 
73
  def setup_environment():
74
  """Set up the necessary directories and environment"""
75
- # Create necessary directories
76
- os.makedirs('utils', exist_ok=True)
77
- os.makedirs('modeling', exist_ok=True)
78
- os.makedirs('modeling/architectures', exist_ok=True)
79
- os.makedirs('tasks', exist_ok=True)
80
- os.makedirs('examples', exist_ok=True)
81
- logger.info("Created required directories")
82
-
83
  # Make sure demo/seem directory exists
84
  if not os.path.exists("demo/seem"):
85
  logger.error("demo/seem directory not found!")
86
  return False
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  return True
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  def main():
91
  """Main entry point"""
92
  logger.info("Starting SEEM Hugging Face Space")
@@ -98,27 +171,20 @@ def main():
98
  if not setup_environment():
99
  return
100
 
 
 
 
101
  # Prepare to run the actual app
102
  app_path = "demo/seem/app.py"
103
  if not os.path.exists(app_path):
104
  logger.error(f"Application file not found at {app_path}!")
105
  return
106
 
107
- logger.info(f"Loading application from {app_path}")
108
-
109
- # Add the demo directory to Python path
110
- sys.path.insert(0, os.path.abspath('demo'))
111
 
112
- # Load and run the app module
113
- try:
114
- spec = importlib.util.spec_from_file_location("seem_app", app_path)
115
- seem_app = importlib.util.module_from_spec(spec)
116
- spec.loader.exec_module(seem_app)
117
- logger.info("SEEM application loaded successfully")
118
- except Exception as e:
119
- logger.error(f"Error loading application: {e}")
120
- import traceback
121
- traceback.print_exc()
122
 
123
  if __name__ == "__main__":
124
  main()
 
10
  import os
11
  import sys
12
  import subprocess
 
13
  import logging
14
  import time
15
+ import shutil
16
 
17
  # Configure logging
18
  logging.basicConfig(level=logging.INFO,
 
44
 
45
  def install_dependencies():
46
  """Install required dependencies"""
47
+ # Update package lists
48
+ run_command("apt-get update", "Updating package lists")
49
+
50
  # Check if ffmpeg is installed
51
  logger.info("Checking for ffmpeg...")
52
  if not run_command("which ffmpeg", "Checking ffmpeg"):
53
  logger.info("Installing ffmpeg...")
54
+ run_command("apt-get install -y ffmpeg", "Installing ffmpeg")
55
 
56
  # Install Python dependencies
57
  logger.info("Installing Python dependencies...")
58
+
59
+ # Install requirements
60
  if os.path.exists("assets/requirements/requirements.txt"):
61
  run_command("pip install -r assets/requirements/requirements.txt", "Installing base requirements")
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Install custom requirements
64
  if os.path.exists("assets/requirements/requirements_custom.txt"):
65
  run_command("pip install -r assets/requirements/requirements_custom.txt", "Installing custom requirements")
66
+
67
+ # Install detectron2-xyz if not already installed
68
+ try:
69
+ import detectron2
70
+ logger.info("detectron2 is already installed")
71
+ except ImportError:
72
+ logger.info("Installing detectron2-xyz...")
73
+ run_command("pip install git+https://github.com/MaureenZOU/detectron2-xyz.git", "Installing detectron2-xyz")
74
 
75
  def setup_environment():
76
  """Set up the necessary directories and environment"""
 
 
 
 
 
 
 
 
77
  # Make sure demo/seem directory exists
78
  if not os.path.exists("demo/seem"):
79
  logger.error("demo/seem directory not found!")
80
  return False
81
 
82
+ # Create examples directory if it doesn't exist
83
+ os.makedirs('demo/seem/examples', exist_ok=True)
84
+
85
+ # Make sure configs directory exists
86
+ if not os.path.exists("configs"):
87
+ logger.error("configs directory not found!")
88
+ # Create empty configs directory
89
+ os.makedirs('configs/seem', exist_ok=True)
90
+ logger.info("Created configs directory")
91
+
92
+ # Create default config file if it doesn't exist
93
+ config_path = "configs/seem/focall_unicl_lang_demo.yaml"
94
+ if not os.path.exists(config_path):
95
+ os.makedirs(os.path.dirname(config_path), exist_ok=True)
96
+ with open(config_path, 'w') as f:
97
+ f.write("""# Model config for demo
98
+ model:
99
+ type: seem
100
+ sem_seg_head:
101
+ type: UniHead
102
+ num_classes: 133
103
+ embed_dim: 768
104
+ learning_type: end2end
105
+ lang_encoder: unicl
106
+ dim_feedforward: 1024
107
+ dropout: 0.1
108
+ activation: relu
109
+ nheads: 8
110
+ dec_layers: 9
111
+ enc_layers: 0
112
+ text_encoder: "bert-base-uncased"
113
+ mask_dim: 256
114
+ task_switch:
115
+ semantic: true
116
+ instance: true
117
+ panoptic: true
118
+ refimg: true
119
+ grounding: true
120
+ audio: false
121
+ audio_grounding: false
122
+ video: true
123
+ """)
124
+ logger.info(f"Created default config file at {config_path}")
125
+
126
+ # Create constants.py in utils if it doesn't exist
127
+ utils_dir = "utils"
128
+ os.makedirs(utils_dir, exist_ok=True)
129
+ constants_path = os.path.join(utils_dir, "constants.py")
130
+ if not os.path.exists(constants_path):
131
+ with open(constants_path, 'w') as f:
132
+ f.write("""# COCO Panoptic Classes
133
+ COCO_PANOPTIC_CLASSES = [
134
+ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',
135
+ 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat',
136
+ 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack',
137
+ 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
138
+ 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
139
+ 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
140
+ 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
141
+ 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse',
142
+ 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator',
143
+ 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
144
+ ]
145
+ """)
146
+ logger.info(f"Created constants.py at {constants_path}")
147
+
148
  return True
149
 
150
+ def download_model_weights():
151
+ """Download model weights if they don't exist"""
152
+ model_path = "seem_focall_v0.pt"
153
+ if not os.path.exists(model_path):
154
+ logger.info(f"Downloading model weights {model_path}...")
155
+ run_command(
156
+ f"wget https://huggingface.co/xdecoder/SEEM/resolve/main/{model_path}",
157
+ f"Downloading {model_path}"
158
+ )
159
+ logger.info(f"Downloaded {model_path}")
160
+ else:
161
+ logger.info(f"Model weights {model_path} already exist")
162
+
163
  def main():
164
  """Main entry point"""
165
  logger.info("Starting SEEM Hugging Face Space")
 
171
  if not setup_environment():
172
  return
173
 
174
+ # Download model weights
175
+ download_model_weights()
176
+
177
  # Prepare to run the actual app
178
  app_path = "demo/seem/app.py"
179
  if not os.path.exists(app_path):
180
  logger.error(f"Application file not found at {app_path}!")
181
  return
182
 
183
+ logger.info(f"Running application from {app_path}")
 
 
 
184
 
185
+ # Run the app
186
+ cmd = f"python {app_path}"
187
+ run_command(cmd, "Running SEEM demo app")
 
 
 
 
 
 
 
188
 
189
  if __name__ == "__main__":
190
  main()