xfu314 commited on
Commit
77e2834
·
1 Parent(s): 2026329
Files changed (4) hide show
  1. app.py +44 -12
  2. packages.txt +4 -0
  3. requirements.txt +35 -1
  4. setup.sh +101 -55
app.py CHANGED
@@ -1,27 +1,55 @@
1
  """
2
  Phantom Video Processor - Hugging Face Space
3
  """
 
4
  import gradio as gr
5
  import spaces
6
-
7
  import subprocess
8
- import os
9
  from pathlib import Path
10
 
11
- # 配置环境
 
 
 
12
  def setup_environment():
13
- setup_script = Path("setup.sh")
 
 
 
 
 
14
 
15
- if setup_script.exists() and not Path("/tmp/phantom_ready").exists():
16
- print("🔧 首次运行,配置环境...")
17
- subprocess.run(["bash", str(setup_script)], check=True)
18
- Path("/tmp/phantom_ready").touch()
19
- print("✅ 环境配置完成")
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  else:
21
- print(" 使用已有环境")
 
 
 
 
 
22
 
23
- # 启动时配置
24
- setup_environment()
 
 
25
 
26
  @spaces.GPU(duration=120)
27
  def process_video(video_file, robot_type, target_hand):
@@ -41,9 +69,13 @@ def process_video(video_file, robot_type, target_hand):
41
  status += f"视频: {video_file}\n"
42
  status += f"机器人: {robot_type}\n"
43
  status += f"手部: {target_hand}\n"
 
 
 
44
 
45
  return None, None, status
46
 
 
47
  with gr.Blocks(title="Phantom") as demo:
48
  gr.Markdown("# 🤖 Phantom - 机器人视频生成器")
49
 
 
1
  """
2
  Phantom Video Processor - Hugging Face Space
3
  """
4
+
5
  import gradio as gr
6
  import spaces
 
7
  import subprocess
8
+ import sys
9
  from pathlib import Path
10
 
11
+ # ========== 环境配置 ==========
12
+
13
+ PHANTOM_DIR = Path("/home/user/app/phantom")
14
+
15
  def setup_environment():
16
+ """配置Phantom环境(仅首次运行)"""
17
+
18
+ # 检查是否已配置
19
+ if Path("/tmp/.phantom_ready").exists():
20
+ print("✅ Phantom环境已配置")
21
+ return True
22
 
23
+ print("🔧 首次运行,配置环境(约5-10分钟)...")
24
+
25
+ # 运行setup.sh
26
+ setup_script = Path("/home/user/app/setup.sh")
27
+ if setup_script.exists():
28
+ try:
29
+ result = subprocess.run(
30
+ ["bash", str(setup_script)],
31
+ check=True,
32
+ capture_output=True,
33
+ text=True
34
+ )
35
+ print(result.stdout)
36
+ print("✅ 环境配置完成")
37
+ return True
38
+ except subprocess.CalledProcessError as e:
39
+ print(f"❌ 配置失败: {e.stderr}")
40
+ return False
41
  else:
42
+ print("⚠️ setup.sh不存在")
43
+ return False
44
+
45
+ # 添加Phantom到Python路径
46
+ if PHANTOM_DIR.exists():
47
+ sys.path.insert(0, str(PHANTOM_DIR))
48
 
49
+ # 启动时配置环境
50
+ phantom_ready = setup_environment()
51
+
52
+ # ========== 其余代码保持不变 ==========
53
 
54
  @spaces.GPU(duration=120)
55
  def process_video(video_file, robot_type, target_hand):
 
69
  status += f"视频: {video_file}\n"
70
  status += f"机器人: {robot_type}\n"
71
  status += f"手部: {target_hand}\n"
72
+
73
+ if not phantom_ready:
74
+ status += "\n⚠️ Phantom环境未就绪"
75
 
76
  return None, None, status
77
 
78
+ # Gradio界面
79
  with gr.Blocks(title="Phantom") as demo:
80
  gr.Markdown("# 🤖 Phantom - 机器人视频生成器")
81
 
packages.txt CHANGED
@@ -1,5 +1,9 @@
1
  ffmpeg
2
  libgl1-mesa-glx
3
  libglib2.0-0
 
 
 
 
4
  wget
5
  git
 
1
  ffmpeg
2
  libgl1-mesa-glx
3
  libglib2.0-0
4
+ libsm6
5
+ libxext6
6
+ libxrender-dev
7
+ libgomp1
8
  wget
9
  git
requirements.txt CHANGED
@@ -1,5 +1,39 @@
 
1
  gradio==4.44.0
2
  spaces==0.28.3
 
 
3
  torch==2.1.0
4
  torchvision==0.16.0
5
- gdown
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio和Spaces
2
  gradio==4.44.0
3
  spaces==0.28.3
4
+
5
+ # PyTorch (HF已预装,但指定版本)
6
  torch==2.1.0
7
  torchvision==0.16.0
8
+
9
+ # 基础科学计算
10
+ numpy==1.26.4
11
+ opencv-python==4.8.1.78
12
+ pillow==10.1.0
13
+ scipy==1.11.4
14
+ scikit-learn==1.3.2
15
+
16
+ # 配置管理
17
+ hydra-core==1.3.2
18
+ omegaconf==2.3.0
19
+
20
+ # 视频处理
21
+ mediapy==1.1.9
22
+
23
+ # 3D处理
24
+ open3d==0.18.0
25
+
26
+ # 机器学习
27
+ transformers==4.42.4
28
+ timm==0.9.12
29
+
30
+ # 其他工具
31
+ joblib
32
+ pandas
33
+ PyOpenGL==3.1.4
34
+ Rtree
35
+ protobuf==3.20.0
36
+ gdown
37
+
38
+ # MMCV (Phantom需要)
39
+ mmcv==1.3.9
setup.sh CHANGED
@@ -1,81 +1,127 @@
1
  #!/bin/bash
 
 
 
2
  set -e
3
 
4
- echo "🚀 配置Phantom环境..."
 
 
 
 
 
 
5
 
6
- cd /home/user/app/phantom
 
 
 
 
 
 
7
 
8
- # 安装SAM2
9
- if [ ! -f "/tmp/sam2_installed" ]; then
10
- echo "📦 安装SAM2..."
 
 
 
 
11
  cd submodules/sam2
12
- pip install -q -e ".[notebooks]"
13
- touch /tmp/sam2_installed
 
14
  fi
15
 
16
- # 安装HaMeR
17
- if [ ! -f "/tmp/hamer_installed" ]; then
18
- echo "📦 安装HaMeR..."
19
- cd /home/user/app/phantom/submodules/phantom-hamer
20
- pip install -q -e .[all]
21
- pip install -q -e third-party/ViTPose
 
 
 
 
22
 
23
- # 下载demo数据
24
- if [ ! -f "_DATA/hamer_demo_data.tar.gz" ]; then
 
25
  cd _DATA
26
  wget -q https://www.cs.utexas.edu/~pavlakos/hamer/data/hamer_demo_data.tar.gz
27
- tar -xzf hamer_demo_data.tar.gz
28
  rm hamer_demo_data.tar.gz
29
  fi
30
 
31
- touch /tmp/hamer_installed
 
 
 
 
 
 
 
 
 
32
  fi
33
 
34
- # 安装mmcv
35
- if [ ! -f "/tmp/mmcv_installed" ]; then
36
- echo "📦 安装mmcv..."
37
- pip install -q mmcv==1.3.9
38
- pip install -q mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu121/torch2.1/index.html
39
- touch /tmp/mmcv_installed
 
40
  fi
41
 
42
- # 安装robosuite
43
- if [ ! -f "/tmp/robosuite_installed" ]; then
44
- echo "📦 安装robosuite..."
45
- cd /home/user/app/phantom/submodules/phantom-robosuite
46
- pip install -q -e .
47
- touch /tmp/robosuite_installed
 
48
  fi
49
 
50
- # 安装robomimic
51
- if [ ! -f "/tmp/robomimic_installed" ]; then
52
- echo "📦 安装robomimic..."
53
- cd /home/user/app/phantom/submodules/phantom-robomimic
54
- pip install -q -e .
55
- touch /tmp/robomimic_installed
 
 
 
56
  fi
57
 
58
- # 下载E2FGVI权重
59
- if [ ! -f "/tmp/e2fgvi_weights" ]; then
60
- echo "📦 下载E2FGVI权重..."
61
- cd /home/user/app/phantom/submodules/phantom-E2FGVI/E2FGVI/release_model
62
- pip install -q gdown
63
- gdown --fuzzy https://drive.google.com/file/d/10wGdKSUOie0XmCr8SQ2A2FeDe-mfn5w3/view?usp=sharing
64
- touch /tmp/e2fgvi_weights
65
  fi
66
 
67
- # 安装E2FGVI
68
- if [ ! -f "/tmp/e2fgvi_installed" ]; then
69
- echo "📦 安装E2FGVI..."
70
- cd /home/user/app/phantom/submodules/phantom-E2FGVI
71
- pip install -q -e .
72
- touch /tmp/e2fgvi_installed
 
73
  fi
74
 
75
- # 安装Phantom主包
76
- if [ ! -f "/tmp/phantom_installed" ]; then
77
- echo "📦 安装Phantom..."
78
- cd /home/user/app/phantom
79
- pip install -q -e .
80
- touch /tmp/phantom_installed
81
- fi
 
 
 
 
 
 
 
1
  #!/bin/bash
2
+ # Phantom环境配置脚本
3
+ # 在app.py启动时运行(仅首次)
4
+
5
  set -e
6
 
7
+ PHANTOM_DIR="/home/user/app/phantom"
8
+ LOG_FILE="/tmp/phantom_setup.log"
9
+
10
+ # 日志函数
11
+ log() {
12
+ echo "[$(date +'%H:%M:%S')] $1" | tee -a "$LOG_FILE"
13
+ }
14
 
15
+ log "🚀 开始配置Phantom环境"
16
+
17
+ # 检查phantom目录
18
+ if [ ! -d "$PHANTOM_DIR" ]; then
19
+ log "❌ Phantom目录不存在"
20
+ exit 1
21
+ fi
22
 
23
+ cd "$PHANTOM_DIR"
24
+
25
+ # ========== 安装子模块 ==========
26
+
27
+ # 1. SAM2
28
+ if [ ! -f "/tmp/.sam2_installed" ]; then
29
+ log "📦 安装SAM2..."
30
  cd submodules/sam2
31
+ pip install -q -e . 2>&1 | tee -a "$LOG_FILE" || log "⚠️ SAM2安装出现警告"
32
+ touch /tmp/.sam2_installed
33
+ log "✅ SAM2完成"
34
  fi
35
 
36
+ # 2. HaMeR
37
+ if [ ! -f "/tmp/.hamer_installed" ]; then
38
+ log "📦 安装HaMeR..."
39
+ cd "$PHANTOM_DIR/submodules/phantom-hamer"
40
+ pip install -q -e .[all] 2>&1 | tee -a "$LOG_FILE" || log "⚠️ HaMeR安装出现警告"
41
+
42
+ # 安装ViTPose
43
+ if [ -d "third-party/ViTPose" ]; then
44
+ pip install -q -e third-party/ViTPose 2>&1 | tee -a "$LOG_FILE"
45
+ fi
46
 
47
+ # 下载demo数据(如果不存在)
48
+ if [ ! -d "_DATA/hamer_demo_data" ]; then
49
+ log "📥 下载HaMeR demo数据..."
50
  cd _DATA
51
  wget -q https://www.cs.utexas.edu/~pavlakos/hamer/data/hamer_demo_data.tar.gz
52
+ tar -xzf hamer_demo_data.tar.gz 2>&1 | tee -a "$LOG_FILE"
53
  rm hamer_demo_data.tar.gz
54
  fi
55
 
56
+ touch /tmp/.hamer_installed
57
+ log "✅ HaMeR完成"
58
+ fi
59
+
60
+ # 3. MMCV-Full
61
+ if [ ! -f "/tmp/.mmcv_installed" ]; then
62
+ log "📦 安装MMCV-Full..."
63
+ pip install -q mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu121/torch2.1/index.html 2>&1 | tee -a "$LOG_FILE"
64
+ touch /tmp/.mmcv_installed
65
+ log "✅ MMCV完成"
66
  fi
67
 
68
+ # 4. Robosuite
69
+ if [ ! -f "/tmp/.robosuite_installed" ]; then
70
+ log "📦 安装Robosuite..."
71
+ cd "$PHANTOM_DIR/submodules/phantom-robosuite"
72
+ pip install -q -e . 2>&1 | tee -a "$LOG_FILE"
73
+ touch /tmp/.robosuite_installed
74
+ log "✅ Robosuite完成"
75
  fi
76
 
77
+ # 5. Robomimic
78
+ if [ ! -f "/tmp/.robomimic_installed" ]; then
79
+ log "📦 安装Robomimic..."
80
+ cd "$PHANTOM_DIR/submodules/phantom-robomimic"
81
+ pip install -q -e . 2>&1 | tee -a "$LOG_FILE"
82
+ touch /tmp/.robomimic_installed
83
+ log "✅ Robomimic完成"
84
  fi
85
 
86
+ # 6. E2FGVI权重
87
+ if [ ! -f "/tmp/.e2fgvi_weights" ]; then
88
+ log "📥 下载E2FGVI权重..."
89
+ cd "$PHANTOM_DIR/submodules/phantom-E2FGVI/E2FGVI/release_model"
90
+ if [ ! -f "E2FGVI-HQ.pth" ]; then
91
+ gdown --fuzzy https://drive.google.com/file/d/10wGdKSUOie0XmCr8SQ2A2FeDe-mfn5w3/view?usp=sharing 2>&1 | tee -a "$LOG_FILE"
92
+ fi
93
+ touch /tmp/.e2fgvi_weights
94
+ log "✅ E2FGVI权重完成"
95
  fi
96
 
97
+ # 7. E2FGVI
98
+ if [ ! -f "/tmp/.e2fgvi_installed" ]; then
99
+ log "📦 安装E2FGVI..."
100
+ cd "$PHANTOM_DIR/submodules/phantom-E2FGVI"
101
+ pip install -q -e . 2>&1 | tee -a "$LOG_FILE"
102
+ touch /tmp/.e2fgvi_installed
103
+ log "✅ E2FGVI完成"
104
  fi
105
 
106
+ # 8. Phantom主包
107
+ if [ ! -f "/tmp/.phantom_installed" ]; then
108
+ log "📦 安装Phantom主包..."
109
+ cd "$PHANTOM_DIR"
110
+ pip install -q -e . 2>&1 | tee -a "$LOG_FILE"
111
+ touch /tmp/.phantom_installed
112
+ log "✅ Phantom主包完成"
113
  fi
114
 
115
+ # 9. 验证MANO模型
116
+ MANO_DIR="$PHANTOM_DIR/submodules/phantom-hamer/_DATA/data/mano"
117
+ if [ -f "$MANO_DIR/MANO_LEFT.pkl" ] && [ -f "$MANO_DIR/MANO_RIGHT.pkl" ]; then
118
+ log "✅ MANO模型已就绪"
119
+ else
120
+ log "⚠️ MANO模型缺失,请上传"
121
+ fi
122
+
123
+ log "🎉 Phantom环境配置完成"
124
+ log "日志文件: $LOG_FILE"
125
+
126
+ # 标记完成
127
+ touch /tmp/.phantom_ready