import subprocess subprocess.run(['pwd']) subprocess.run(['pip', 'install', '-e', './UniVAD/models/GroundingDINO'], check=True) # subprocess.run(["pip", "install", "gradio==4.21.0"]) subprocess.run(["pip", "install", "fastapi==0.108.0"]) sys.path.insert(0, './UniVAD/models/GroundingDINO') import gradio as gr from UniVAD.tools import process_image subprocess.run(["wget", "-q","https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth"], check=True) subprocess.run(["wget", "-q","https://huggingface.co/xinyu1205/recognize-anything-plus-model/resolve/main/ram_plus_swin_large_14m.pth"], check=True) subprocess.run(["wget", "-q","https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_h.pth"], check=True) import torch import torchvision.transforms as transforms from UniVAD.univad import UniVAD from ram.models import ram_plus from UniVAD.models.segment_anything import ( sam_hq_model_registry, SamPredictor, ) # Grounding DINO from UniVAD.models.grounded_sam import ( load_model, ) import spaces image_size = 336 device = "cuda" if torch.cuda.is_available() else "cpu" univad_model = UniVAD(image_size=image_size).to(device) ram_model = ram_plus( pretrained="./ram_plus_swin_large_14m.pth", image_size=384, vit="swin_l", ) ram_model.eval() ram_model = ram_model.to(device) grounding_model = load_model( "./UniVAD/models/GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py", "./groundingdino_swint_ogc.pth", "cuda" if torch.cuda.is_available() else "cpu" ) sam = sam_hq_model_registry["vit_h"]("./sam_hq_vit_h.pth").to(device) sam_predictor = SamPredictor(sam) transform = transforms.Compose( [ transforms.Resize((image_size, image_size)), transforms.ToTensor(), ] ) def preprocess_image(img): return img.resize((448, 448)) def update_image(image): if image is not None: return preprocess_image(image) @spaces.GPU def ad(image_pil, normal_image, box_threshold, text_threshold, text_prompt, background_prompt, cluster_num): return process_image(image_pil, normal_image, box_threshold, text_threshold, sam_predictor, grounding_model, univad_model, ram_model, text_prompt, background_prompt, cluster_num, image_size) with gr.Blocks() as demo: gr.HTML("""