Spaces:
Paused
Paused
revert app.py changes
Browse files
app.py
CHANGED
|
@@ -6,55 +6,53 @@ import contextlib
|
|
| 6 |
import io
|
| 7 |
import traceback
|
| 8 |
import gradio as gr
|
| 9 |
-
#import spaces
|
| 10 |
from transformers import AutoModel, AutoTokenizer, AutoConfig, pipeline
|
| 11 |
from huggingface_hub import snapshot_download
|
| 12 |
import sys, pathlib
|
| 13 |
import os
|
| 14 |
os.environ["OPENAI_API_KEY"] = "test"
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
print("Importing modules...")
|
| 33 |
-
package_spec = importlib.util.spec_from_file_location(
|
| 34 |
-
VINE_PACKAGE,
|
| 35 |
-
Path(repo_dir) / "__init__.py",
|
| 36 |
-
submodule_search_locations=[str(repo_dir)],
|
| 37 |
-
)
|
| 38 |
-
package_module = importlib.util.module_from_spec(package_spec)
|
| 39 |
-
sys.modules[VINE_PACKAGE] = package_module
|
| 40 |
-
package_spec.loader.exec_module(package_module)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#@spaces.GPU(duration=200)
|
| 55 |
def process_video(video_file, categorical_keywords, unary_keywords, binary_keywords, object_pairs, output_fps):
|
| 56 |
log_buffer = io.StringIO()
|
| 57 |
-
VineConfig, VineModel, VinePipeline = load_vine()
|
| 58 |
try:
|
| 59 |
with contextlib.redirect_stdout(log_buffer), contextlib.redirect_stderr(log_buffer):
|
| 60 |
categorical_keywords = [kw.strip() for kw in categorical_keywords.split(",")] if categorical_keywords else []
|
|
@@ -82,6 +80,7 @@ def process_video(video_file, categorical_keywords, unary_keywords, binary_keywo
|
|
| 82 |
visualization_dir=visualization_dir,
|
| 83 |
visualize=True,
|
| 84 |
debug_visualizations=False,
|
|
|
|
| 85 |
)
|
| 86 |
model = VineModel(config)
|
| 87 |
|
|
@@ -93,6 +92,7 @@ def process_video(video_file, categorical_keywords, unary_keywords, binary_keywo
|
|
| 93 |
gd_config_path=gd_config_path,
|
| 94 |
gd_checkpoint_path=gd_checkpoint_path,
|
| 95 |
trust_remote_code=True,
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
results = vine_pipe(
|
|
@@ -104,7 +104,8 @@ def process_video(video_file, categorical_keywords, unary_keywords, binary_keywo
|
|
| 104 |
segmentation_method="grounding_dino_sam2",
|
| 105 |
return_top_k=5,
|
| 106 |
include_visualizations=True,
|
| 107 |
-
debug_visualizations=False
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
if isinstance(results, Mapping):
|
|
|
|
| 6 |
import io
|
| 7 |
import traceback
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
from transformers import AutoModel, AutoTokenizer, AutoConfig, pipeline
|
| 10 |
from huggingface_hub import snapshot_download
|
| 11 |
import sys, pathlib
|
| 12 |
import os
|
| 13 |
os.environ["OPENAI_API_KEY"] = "test"
|
| 14 |
|
| 15 |
+
# 1) Download the repo to a local cache dir
|
| 16 |
+
repo_dir = snapshot_download(repo_id="KevinX-Penn28/testing", revision="main")
|
| 17 |
|
| 18 |
+
# 2) Register the snapshot as an importable package
|
| 19 |
+
VINE_PACKAGE = "vine_remote_repo"
|
| 20 |
|
| 21 |
+
# Drop stale modules in case the script reloads
|
| 22 |
+
for module_name in list(sys.modules):
|
| 23 |
+
if module_name == VINE_PACKAGE or module_name.startswith(f"{VINE_PACKAGE}."):
|
| 24 |
+
del sys.modules[module_name]
|
| 25 |
|
| 26 |
+
package_spec = importlib.util.spec_from_file_location(
|
| 27 |
+
VINE_PACKAGE,
|
| 28 |
+
Path(repo_dir) / "__init__.py",
|
| 29 |
+
submodule_search_locations=[str(repo_dir)],
|
| 30 |
+
)
|
| 31 |
+
if not package_spec or not package_spec.loader:
|
| 32 |
+
raise ImportError(f"Cannot create package spec for {VINE_PACKAGE} at {repo_dir}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
package_module = importlib.util.module_from_spec(package_spec)
|
| 35 |
+
sys.modules[VINE_PACKAGE] = package_module
|
| 36 |
+
package_spec.loader.exec_module(package_module)
|
| 37 |
|
| 38 |
+
# 3) Import and use via the registered package
|
| 39 |
+
vine_config_module = importlib.import_module(f"{VINE_PACKAGE}.vine_config")
|
| 40 |
+
vine_model_module = importlib.import_module(f"{VINE_PACKAGE}.vine_model")
|
| 41 |
+
vine_pipeline_module = importlib.import_module(f"{VINE_PACKAGE}.vine_pipeline")
|
| 42 |
|
| 43 |
+
VineConfig = vine_config_module.VineConfig # your config class
|
| 44 |
+
VineModel = vine_model_module.VineModel # your model class
|
| 45 |
+
VinePipeline = vine_pipeline_module.VinePipeline
|
| 46 |
|
| 47 |
+
current_dir = Path(__file__).parent
|
| 48 |
+
sam_config_path = "//" + str(current_dir / "sam2_hiera_t.yaml")
|
| 49 |
+
sam_checkpoint_path = "//" + str(current_dir / "sam2_hiera_tiny.pt")
|
| 50 |
+
gd_config_path = "//" + str(current_dir / "GroundingDINO_SwinT_OGC.py")
|
| 51 |
+
gd_checkpoint_path = "//" + str(current_dir / "groundingdino_swint_ogc.pth")
|
| 52 |
+
visualization_dir = "//" + str(current_dir / "outputs")
|
| 53 |
|
|
|
|
| 54 |
def process_video(video_file, categorical_keywords, unary_keywords, binary_keywords, object_pairs, output_fps):
|
| 55 |
log_buffer = io.StringIO()
|
|
|
|
| 56 |
try:
|
| 57 |
with contextlib.redirect_stdout(log_buffer), contextlib.redirect_stderr(log_buffer):
|
| 58 |
categorical_keywords = [kw.strip() for kw in categorical_keywords.split(",")] if categorical_keywords else []
|
|
|
|
| 80 |
visualization_dir=visualization_dir,
|
| 81 |
visualize=True,
|
| 82 |
debug_visualizations=False,
|
| 83 |
+
device = "cuda"
|
| 84 |
)
|
| 85 |
model = VineModel(config)
|
| 86 |
|
|
|
|
| 92 |
gd_config_path=gd_config_path,
|
| 93 |
gd_checkpoint_path=gd_checkpoint_path,
|
| 94 |
trust_remote_code=True,
|
| 95 |
+
device = "cuda"
|
| 96 |
)
|
| 97 |
|
| 98 |
results = vine_pipe(
|
|
|
|
| 104 |
segmentation_method="grounding_dino_sam2",
|
| 105 |
return_top_k=5,
|
| 106 |
include_visualizations=True,
|
| 107 |
+
debug_visualizations=False,
|
| 108 |
+
device = 2
|
| 109 |
)
|
| 110 |
|
| 111 |
if isinstance(results, Mapping):
|