megalado commited on
Commit
16bcf38
·
1 Parent(s): b1e240a

Use the correct sample.generate module for MDM

Browse files
Files changed (1) hide show
  1. app.py +49 -69
app.py CHANGED
@@ -7,10 +7,6 @@ from pathlib import Path
7
  import traceback
8
  import subprocess
9
 
10
- # Add the motion-diffusion-model repository to the path if it exists
11
- if Path("motion-diffusion-model").exists():
12
- sys.path.append("./motion-diffusion-model")
13
-
14
  def ensure_mdm_repo():
15
  """Ensure the MDM repository is cloned and set up"""
16
  if not Path("motion-diffusion-model").exists():
@@ -21,7 +17,7 @@ def ensure_mdm_repo():
21
  print("Setting up the repository...")
22
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
23
 
24
- # Download other necessary files
25
  os.chdir("motion-diffusion-model")
26
  subprocess.run(["bash", "prepare/download_smpl_files.sh"])
27
  subprocess.run(["bash", "prepare/download_glove.sh"])
@@ -32,61 +28,6 @@ def ensure_mdm_repo():
32
  if "./motion-diffusion-model" not in sys.path:
33
  sys.path.append("./motion-diffusion-model")
34
 
35
- def run_mdm_generation(text_prompt, motion_length, seed, checkpoint_path):
36
- """Run MDM generation using the provided checkpoint"""
37
- # Print the current directory and check that checkpoint exists
38
- print(f"Current directory: {os.getcwd()}")
39
- print(f"Checking checkpoint path: {checkpoint_path}, exists: {os.path.exists(checkpoint_path)}")
40
-
41
- # Change to the MDM repository directory
42
- original_dir = os.getcwd()
43
- os.chdir("motion-diffusion-model")
44
-
45
- # Prepare output directory
46
- os.makedirs("../output", exist_ok=True)
47
- output_path = f"../output/output_{abs(hash(text_prompt) % 10000)}_{int(motion_length)}_{seed}.mp4"
48
-
49
- # Create a command to run the MDM sample script with all necessary parameters
50
- cmd = [
51
- "python", "-m", "sample.sample_text",
52
- "--model_path", checkpoint_path,
53
- "--text_prompt", text_prompt,
54
- "--motion_length", str(motion_length),
55
- "--seed", str(int(seed)),
56
- "--num_samples", "1",
57
- "--dataset", "humanml", # Specify the dataset
58
- "--guidance_scale", "2.5", # Higher values follow the text more closely
59
- "--num_repetitions", "1",
60
- ]
61
-
62
- print(f"Running command: {' '.join(cmd)}")
63
- result = subprocess.run(cmd, capture_output=True, text=True)
64
-
65
- print("Command output:", result.stdout)
66
- if result.stderr:
67
- print("Command error:", result.stderr)
68
-
69
- # Check for output files - assume they're saved to samples directory
70
- output_files = []
71
- if os.path.exists("samples"):
72
- for file in os.listdir("samples"):
73
- if file.endswith(".mp4"):
74
- output_files.append(os.path.join("samples", file))
75
- print(f"Found output file: {file}")
76
-
77
- # Return to the original directory
78
- os.chdir(original_dir)
79
-
80
- # If we found output files, copy the first one to our output path
81
- if output_files:
82
- os.makedirs(os.path.dirname(output_path), exist_ok=True)
83
- subprocess.run(["cp", os.path.join("motion-diffusion-model", output_files[0]), output_path])
84
- print(f"Copied output to {output_path}")
85
- return output_path
86
-
87
- print("No output files found.")
88
- return None
89
-
90
  def text_to_motion(text_prompt, motion_length=3.0, seed=0):
91
  """Generate motion from text prompt using MDM"""
92
  try:
@@ -95,19 +36,58 @@ def text_to_motion(text_prompt, motion_length=3.0, seed=0):
95
  # Ensure the MDM repository is set up
96
  ensure_mdm_repo()
97
 
98
- # Use the uploaded checkpoint with absolute path
 
 
 
99
  checkpoint_path = os.path.abspath("checkpoints/opt000750000.pt")
100
  print(f"Using checkpoint: {checkpoint_path}")
101
 
102
- # Try to run MDM generation
103
- output_path = run_mdm_generation(text_prompt, motion_length, seed, checkpoint_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- if output_path and os.path.exists(output_path):
106
- print(f"Successfully generated motion at {output_path}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  return output_path
108
- else:
109
- print("MDM generation failed, no output file produced")
110
- return None
111
 
112
  except Exception as e:
113
  print(f"Error generating motion: {str(e)}")
@@ -124,7 +104,7 @@ demo = gr.Interface(
124
  ],
125
  outputs=gr.Video(label="Generated Motion"),
126
  title="Motion Diffusion Model Demo",
127
- description="Generate human motions from text descriptions using the opt000750000.pt checkpoint model."
128
  )
129
 
130
  # Launch the app
 
7
  import traceback
8
  import subprocess
9
 
 
 
 
 
10
  def ensure_mdm_repo():
11
  """Ensure the MDM repository is cloned and set up"""
12
  if not Path("motion-diffusion-model").exists():
 
17
  print("Setting up the repository...")
18
  subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
19
 
20
+ # Add necessary files
21
  os.chdir("motion-diffusion-model")
22
  subprocess.run(["bash", "prepare/download_smpl_files.sh"])
23
  subprocess.run(["bash", "prepare/download_glove.sh"])
 
28
  if "./motion-diffusion-model" not in sys.path:
29
  sys.path.append("./motion-diffusion-model")
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def text_to_motion(text_prompt, motion_length=3.0, seed=0):
32
  """Generate motion from text prompt using MDM"""
33
  try:
 
36
  # Ensure the MDM repository is set up
37
  ensure_mdm_repo()
38
 
39
+ # Create output directory
40
+ os.makedirs("output", exist_ok=True)
41
+
42
+ # Get absolute path to the checkpoint
43
  checkpoint_path = os.path.abspath("checkpoints/opt000750000.pt")
44
  print(f"Using checkpoint: {checkpoint_path}")
45
 
46
+ # Change to the MDM repository directory
47
+ original_dir = os.getcwd()
48
+ os.chdir("motion-diffusion-model")
49
+
50
+ # Create the command to run - based on official examples
51
+ cmd = [
52
+ "python",
53
+ "-m", "sample.generate", # The correct entry point
54
+ "--model_path", checkpoint_path,
55
+ "--text_prompt", text_prompt,
56
+ "--motion_length", str(motion_length),
57
+ "--seed", str(int(seed)),
58
+ "--num_samples", "1", # Generate just one sample
59
+ "--num_repetitions", "1" # With one repetition
60
+ ]
61
+
62
+ print(f"Running command: {' '.join(cmd)}")
63
+ result = subprocess.run(cmd, capture_output=True, text=True)
64
 
65
+ # Print the output for debugging
66
+ print("Command output:", result.stdout)
67
+ if result.stderr:
68
+ print("Command error:", result.stderr)
69
+
70
+ # Check for output files - MDM saves samples in samples directory
71
+ output_mp4 = None
72
+ if os.path.exists("samples"):
73
+ for file in os.listdir("samples"):
74
+ if file.endswith(".mp4"):
75
+ output_mp4 = os.path.join("samples", file)
76
+ print(f"Found output file: {output_mp4}")
77
+ break
78
+
79
+ # Return to the original directory
80
+ os.chdir(original_dir)
81
+
82
+ # If we found an output file, copy it to our output directory
83
+ if output_mp4:
84
+ output_path = f"output/output_{abs(hash(text_prompt) % 10000)}_{int(motion_length)}_{seed}.mp4"
85
+ subprocess.run(["cp", os.path.join("motion-diffusion-model", output_mp4), output_path])
86
+ print(f"Copied output to {output_path}")
87
  return output_path
88
+
89
+ print("No output files found.")
90
+ return None
91
 
92
  except Exception as e:
93
  print(f"Error generating motion: {str(e)}")
 
104
  ],
105
  outputs=gr.Video(label="Generated Motion"),
106
  title="Motion Diffusion Model Demo",
107
+ description="Generate human motions from text descriptions using the opt000750000.pt checkpoint model. Try prompts like: 'A person walks forward, then turns left', 'A person jumps up and down', or 'A person dances energetically'."
108
  )
109
 
110
  # Launch the app