Spaces:
Running
on
Zero
Running
on
Zero
f lisa
Browse files- app.py +50 -0
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -20,10 +20,14 @@ import gradio as gr
|
|
| 20 |
|
| 21 |
import torch
|
| 22 |
import torch.nn.functional as F
|
|
|
|
| 23 |
from PIL import Image
|
| 24 |
import numpy as np
|
| 25 |
import time
|
| 26 |
import threading
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
from ncut_pytorch.backbone import extract_features, load_model
|
| 29 |
from ncut_pytorch.backbone import MODEL_DICT, LAYER_DICT, RES_DICT
|
|
@@ -541,6 +545,48 @@ def run_fn(
|
|
| 541 |
images = [transform_image(image, resolution=resolution) for image in images]
|
| 542 |
images = torch.stack(images)
|
| 543 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
if "AlignedThreeModelAttnNodes" == model_name:
|
| 545 |
# dirty patch for the alignedcut paper
|
| 546 |
model = load_alignedthreemodel()
|
|
@@ -1244,6 +1290,10 @@ with demo:
|
|
| 1244 |
with gr.Column():
|
| 1245 |
gr.Markdown("###### Running out of GPU? Try [Demo](https://ncut-pytorch.readthedocs.io/en/latest/demo/) hosted at UPenn")
|
| 1246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1247 |
if DOWNLOAD_ALL_MODELS_DATASETS:
|
| 1248 |
from ncut_pytorch.backbone import download_all_models
|
| 1249 |
threading.Thread(target=download_all_models).start()
|
|
|
|
| 20 |
|
| 21 |
import torch
|
| 22 |
import torch.nn.functional as F
|
| 23 |
+
import transformers
|
| 24 |
from PIL import Image
|
| 25 |
import numpy as np
|
| 26 |
import time
|
| 27 |
import threading
|
| 28 |
+
import subprocess
|
| 29 |
+
import sys
|
| 30 |
+
import importlib
|
| 31 |
|
| 32 |
from ncut_pytorch.backbone import extract_features, load_model
|
| 33 |
from ncut_pytorch.backbone import MODEL_DICT, LAYER_DICT, RES_DICT
|
|
|
|
| 545 |
images = [transform_image(image, resolution=resolution) for image in images]
|
| 546 |
images = torch.stack(images)
|
| 547 |
|
| 548 |
+
|
| 549 |
+
if is_lisa:
|
| 550 |
+
# LISA and Llava is not compatible with the current version of transformers
|
| 551 |
+
# please contact the author for update
|
| 552 |
+
# this is a dirty patch for the LISA model
|
| 553 |
+
|
| 554 |
+
# pre-import the SD3 pipeline
|
| 555 |
+
from diffusers import StableDiffusion3Pipeline
|
| 556 |
+
|
| 557 |
+
# unloading the current transformers
|
| 558 |
+
for module in list(sys.modules.keys()):
|
| 559 |
+
if "transformers" in module:
|
| 560 |
+
del sys.modules[module]
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
def install_transformers_version(version, target_dir):
|
| 564 |
+
"""Install a specific version of transformers to a target directory."""
|
| 565 |
+
if not os.path.exists(target_dir):
|
| 566 |
+
os.makedirs(target_dir)
|
| 567 |
+
|
| 568 |
+
# Use subprocess to run the pip command
|
| 569 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', f'transformers=={version}', '-t', target_dir])
|
| 570 |
+
|
| 571 |
+
target_dir = '/tmp/lisa_transformers_v433'
|
| 572 |
+
if not os.path.exists(target_dir):
|
| 573 |
+
install_transformers_version('4.33.0', target_dir)
|
| 574 |
+
|
| 575 |
+
# Add the new version path to sys.path
|
| 576 |
+
sys.path.insert(0, target_dir)
|
| 577 |
+
|
| 578 |
+
transformers = importlib.import_module("transformers")
|
| 579 |
+
|
| 580 |
+
if not is_lisa:
|
| 581 |
+
# remove the LISA model from the sys.path
|
| 582 |
+
|
| 583 |
+
if "/tmp/lisa_transformers_v433" in sys.path:
|
| 584 |
+
sys.path.remove("/tmp/lisa_transformers_v433")
|
| 585 |
+
|
| 586 |
+
transformers = importlib.import_module("transformers")
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
|
| 590 |
if "AlignedThreeModelAttnNodes" == model_name:
|
| 591 |
# dirty patch for the alignedcut paper
|
| 592 |
model = load_alignedthreemodel()
|
|
|
|
| 1290 |
with gr.Column():
|
| 1291 |
gr.Markdown("###### Running out of GPU? Try [Demo](https://ncut-pytorch.readthedocs.io/en/latest/demo/) hosted at UPenn")
|
| 1292 |
|
| 1293 |
+
# for local development
|
| 1294 |
+
if os.path.exists("/hf_token.txt"):
|
| 1295 |
+
os.environ["HF_ACCESS_TOKEN"] = open("/hf_token.txt").read().strip()
|
| 1296 |
+
|
| 1297 |
if DOWNLOAD_ALL_MODELS_DATASETS:
|
| 1298 |
from ncut_pytorch.backbone import download_all_models
|
| 1299 |
threading.Thread(target=download_all_models).start()
|
requirements.txt
CHANGED
|
@@ -12,7 +12,7 @@ pillow==9.4.0
|
|
| 12 |
SAM-2 @ git+https://github.com/huzeyann/segment-anything-2.git
|
| 13 |
segment-anything @ git+https://github.com/facebookresearch/segment-anything.git@6fdee8f
|
| 14 |
mobile-sam @ git+https://github.com/ChaoningZhang/MobileSAM.git@c12dd83
|
| 15 |
-
lisa @ git+https://github.com/huzeyann/LISA.git
|
| 16 |
timm==0.9.2
|
| 17 |
open-clip-torch==2.20.0
|
| 18 |
ncut-pytorch>=1.3.13
|
|
|
|
| 12 |
SAM-2 @ git+https://github.com/huzeyann/segment-anything-2.git
|
| 13 |
segment-anything @ git+https://github.com/facebookresearch/segment-anything.git@6fdee8f
|
| 14 |
mobile-sam @ git+https://github.com/ChaoningZhang/MobileSAM.git@c12dd83
|
| 15 |
+
lisa @ git+https://github.com/huzeyann/LISA/tree/old-transformers-working.git
|
| 16 |
timm==0.9.2
|
| 17 |
open-clip-torch==2.20.0
|
| 18 |
ncut-pytorch>=1.3.13
|