victor HF staff commited on
Commit
8b5b445
·
1 Parent(s): ce6c967

refactor: Enhance FFMPEG command generation with step-by-step thinking and improved command extraction

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -132,6 +132,7 @@ You are given:
132
  Your objective is to generate the SIMPLEST POSSIBLE single ffmpeg command to create the requested video.
133
 
134
  Key requirements:
 
135
  - Use the absolute minimum number of ffmpeg options needed
136
  - Avoid complex filter chains or filter_complex if possible
137
  - Prefer simple concatenation, scaling, and basic filters
@@ -147,15 +148,19 @@ Remember: Simpler is better. Only use advanced ffmpeg features if absolutely nec
147
  },
148
  {
149
  "role": "user",
150
- "content": f"""Always output the media as video/mp4 and output file with "output.mp4". Provide only the shell command without any explanations.
151
- The current assets and objective follow. Reply with the FFMPEG command:
152
 
153
  AVAILABLE ASSETS LIST:
154
 
155
  {files_info_string}
156
 
157
  OBJECTIVE: {prompt} and output at "output.mp4"
158
- YOUR FFMPEG COMMAND:
 
 
 
 
159
  """,
160
  },
161
  ]
@@ -188,13 +193,23 @@ YOUR FFMPEG COMMAND:
188
  # Find content between ```sh or ```bash and the next ```
189
  import re
190
 
191
- command = re.search(r"```(?:sh|bash)?\n(.*?)\n```", content, re.DOTALL)
192
- if command:
193
- command = command.group(1).strip()
194
  else:
195
- command = content.replace("\n", "")
 
 
 
 
 
196
  else:
197
- command = content.replace("\n", "")
 
 
 
 
 
198
 
199
  # remove output.mp4 with the actual output file path
200
  command = command.replace("output.mp4", "")
@@ -270,7 +285,11 @@ def update(
270
  f"\n=== EXECUTING FFMPEG COMMAND ===\nffmpeg {' '.join(final_command[1:])}\n"
271
  )
272
  subprocess.run(final_command, cwd=temp_dir)
273
- generated_command = f"### Generated Command\n```bash\nffmpeg {' '.join(args[1:])} -y output.mp4\n```"
 
 
 
 
274
  return output_file_path, gr.update(value=generated_command)
275
  except Exception as e:
276
  attempts += 1
 
132
  Your objective is to generate the SIMPLEST POSSIBLE single ffmpeg command to create the requested video.
133
 
134
  Key requirements:
135
+ - First, think step-by-step about what the user is asking for and reformulate it into a clear technical specification
136
  - Use the absolute minimum number of ffmpeg options needed
137
  - Avoid complex filter chains or filter_complex if possible
138
  - Prefer simple concatenation, scaling, and basic filters
 
148
  },
149
  {
150
  "role": "user",
151
+ "content": f"""Always output the media as video/mp4 and output file with "output.mp4".
152
+ The current assets and objective follow.
153
 
154
  AVAILABLE ASSETS LIST:
155
 
156
  {files_info_string}
157
 
158
  OBJECTIVE: {prompt} and output at "output.mp4"
159
+
160
+ First, think step-by-step about what I'm asking for and reformulate it into a clear technical specification.
161
+ Then provide the FFMPEG command that will accomplish this task.
162
+
163
+ YOUR RESPONSE:
164
  """,
165
  },
166
  ]
 
193
  # Find content between ```sh or ```bash and the next ```
194
  import re
195
 
196
+ command_match = re.search(r"```(?:sh|bash)?\n(.*?)\n```", content, re.DOTALL)
197
+ if command_match:
198
+ command = command_match.group(1).strip()
199
  else:
200
+ # Try to find a line that starts with ffmpeg
201
+ ffmpeg_lines = [line.strip() for line in content.split('\n') if line.strip().startswith('ffmpeg')]
202
+ if ffmpeg_lines:
203
+ command = ffmpeg_lines[0]
204
+ else:
205
+ command = content.replace("\n", "")
206
  else:
207
+ # Try to find a line that starts with ffmpeg
208
+ ffmpeg_lines = [line.strip() for line in content.split('\n') if line.strip().startswith('ffmpeg')]
209
+ if ffmpeg_lines:
210
+ command = ffmpeg_lines[0]
211
+ else:
212
+ command = content.replace("\n", "")
213
 
214
  # remove output.mp4 with the actual output file path
215
  command = command.replace("output.mp4", "")
 
285
  f"\n=== EXECUTING FFMPEG COMMAND ===\nffmpeg {' '.join(final_command[1:])}\n"
286
  )
287
  subprocess.run(final_command, cwd=temp_dir)
288
+ # Store the full model response for display
289
+ model_response = content.strip()
290
+ # Extract just the command for display
291
+ command_for_display = f"ffmpeg {' '.join(args[1:])} -y output.mp4"
292
+ generated_command = f"### Model Analysis\n{model_response}\n\n### Generated Command\n```bash\n{command_for_display}\n```"
293
  return output_file_path, gr.update(value=generated_command)
294
  except Exception as e:
295
  attempts += 1