|
|
|
|
|
|
|
import os |
|
import sys |
|
import subprocess |
|
|
|
print("Setting up SEEM environment...") |
|
|
|
|
|
print("Installing detectron2...") |
|
os.system("pip install -q git+https://github.com/MaureenZOU/detectron2-xyz.git") |
|
|
|
|
|
os.environ["PYTHONPATH"] = os.getcwd() |
|
print(f"Set PYTHONPATH to: {os.getcwd()}") |
|
|
|
|
|
app_path = "app.py" |
|
app_patched_path = "app_patched.py" |
|
|
|
with open(app_path, "r") as f: |
|
app_code = f.read() |
|
|
|
|
|
app_code = app_code.replace( |
|
"model = BaseModel(opt, build_model(opt)).from_pretrained(pretrained_pth).eval().cuda()", |
|
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n" |
|
"print(f\"Using device: {device}\")\n" |
|
"model = BaseModel(opt, build_model(opt)).from_pretrained(pretrained_pth).eval().to(device)" |
|
) |
|
|
|
|
|
app_code = app_code.replace( |
|
"@torch.no_grad()\ndef inference(image, task, *args, **kwargs):\n with torch.autocast(device_type='cuda', dtype=torch.float16):", |
|
"@torch.no_grad()\ndef inference(image, task, *args, **kwargs):\n if torch.cuda.is_available():\n with torch.autocast(device_type='cuda', dtype=torch.float16):" |
|
) |
|
|
|
|
|
app_code = app_code.replace( |
|
" if 'Video' in task:\n return interactive_infer_video(model, audio, image, task, *args, **kwargs)\n else:\n return interactive_infer_image(model, audio, image, task, *args, **kwargs)", |
|
" if 'Video' in task:\n return interactive_infer_video(model, audio, image, task, *args, **kwargs)\n else:\n return interactive_infer_image(model, audio, image, task, *args, **kwargs)\n else:\n # Run without autocast on CPU\n if 'Video' in task:\n return interactive_infer_video(model, audio, image, task, *args, **kwargs)\n else:\n return interactive_infer_image(model, audio, image, task, *args, **kwargs)" |
|
) |
|
|
|
|
|
if "demo/seem/examples/" in app_code: |
|
app_code = app_code.replace( |
|
"\"demo/seem/examples/", |
|
"\"examples/" |
|
) |
|
|
|
with open(app_patched_path, "w") as f: |
|
f.write(app_code) |
|
|
|
|
|
print("Starting SEEM demo...") |
|
os.system(f"python {app_patched_path}") |