radames HF staff commited on
Commit
b19f57b
β€’
1 Parent(s): e75d696

add command output

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +35 -23
  3. utils.py +9 -0
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: ChatGPT-4 Video Composer
3
  emoji: 🐠
4
  colorFrom: red
5
  colorTo: yellow
1
  ---
2
+ title: GPT-4 Video Composer
3
  emoji: 🐠
4
  colorFrom: red
5
  colorTo: yellow
app.py CHANGED
@@ -11,6 +11,7 @@ import uuid
11
  import tempfile
12
  import shlex
13
  import shutil
 
14
 
15
  OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
16
  openai.api_key = OPENAI_API_KEY
@@ -52,7 +53,7 @@ def get_files_infos(files):
52
  return results
53
 
54
 
55
- def get_completion(prompt, files_info):
56
 
57
  files_info_string = "".join(str(x) for x in files_info)
58
  messages = [
@@ -81,8 +82,10 @@ YOUR FFMPEG COMMAND:""",
81
  print(messages)
82
 
83
  try:
84
- completion = openai.ChatCompletion.create(
85
- model="gpt-4", messages=messages)
 
 
86
 
87
  command = completion.choices[0].message.content.replace("\n", "")
88
 
@@ -95,14 +98,14 @@ YOUR FFMPEG COMMAND:""",
95
  raise Exception("OpenAI API error")
96
 
97
 
98
- def update(files, prompt):
99
  if prompt == "":
100
  raise gr.Error("Please enter a prompt.")
101
 
102
  files_info = get_files_infos(files)
103
 
104
  try:
105
- command_string = get_completion(prompt, files_info)
106
  print(
107
  f"""\n\n/// START OF COMMAND ///:\n{command_string}\n/// END OF COMMAND ///\n\n""")
108
 
@@ -130,8 +133,8 @@ def update(files, prompt):
130
  output_file_name = f'output_{uuid.uuid4()}.mp4'
131
  output_file_path = str((Path(temp_dir) / output_file_name).resolve())
132
  subprocess.run(args + ["-y", output_file_path], cwd=temp_dir)
133
-
134
- return output_file_path
135
  except Exception as e:
136
  print("FROM UPDATE", e)
137
  raise gr.Error(e)
@@ -153,7 +156,7 @@ css = """
153
  with gr.Blocks(css=css) as demo:
154
  gr.Markdown(
155
  """
156
- # <span style="margin-right: 0.3rem;">🏞</span> Video Composer
157
  Add video, image and audio assets and ask ChatGPT to compose a new video.
158
  """,
159
  elem_id="header",
@@ -168,45 +171,54 @@ with gr.Blocks(css=css) as demo:
168
  label="Instructions",
169
  )
170
  btn = gr.Button("Run", label="Run")
 
 
 
 
 
171
  with gr.Column():
172
  generated_video = gr.Video(
173
  interactive=False, label="Generated Video", include_audio=True
174
  )
 
175
 
176
  btn.click(
177
- fn=update, inputs=[user_files,
178
- user_prompt], outputs=[generated_video]
179
  )
180
  with gr.Row():
181
  gr.Examples(
182
  examples=[
183
- [[
184
- "./examples/cat8.jpeg",
185
- "./examples/cat1.jpeg",
186
- "./examples/cat2.jpeg",
187
- "./examples/cat3.jpeg",
188
- "./examples/cat4.jpeg",
189
- "./examples/cat5.jpeg",
190
- "./examples/cat6.jpeg",
191
- "./examples/cat7.jpeg"],
192
- "make a video gif given each image 1s loop"
193
  ],
194
  [
195
  ["./examples/example.mp4"],
196
- "please encode this video 10 times faster"
 
197
  ],
198
  [
199
  ["./examples/heat-wave.mp3", "./examples/square-image.png"],
200
  "Make a 720x720 video with a white waveform of the audio taking all screen space, also add the image as the background",
 
201
  ],
202
  [
203
  ["./examples/waterfall-overlay.png",
204
  "./examples/waterfall.mp4"],
205
  "Add the overlay to the video.",
 
206
  ],
207
  ],
208
- inputs=[user_files, user_prompt],
209
- outputs=generated_video,
210
  fn=update,
211
  cache_examples=True,
212
  )
11
  import tempfile
12
  import shlex
13
  import shutil
14
+ from utils import format_bash_command
15
 
16
  OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
17
  openai.api_key = OPENAI_API_KEY
53
  return results
54
 
55
 
56
+ def get_completion(prompt, files_info, top_p, temperature):
57
 
58
  files_info_string = "".join(str(x) for x in files_info)
59
  messages = [
82
  print(messages)
83
 
84
  try:
85
+ completion = openai.ChatCompletion.create(model="gpt-4",
86
+ messages=messages,
87
+ top_p=top_p,
88
+ temperature=temperature)
89
 
90
  command = completion.choices[0].message.content.replace("\n", "")
91
 
98
  raise Exception("OpenAI API error")
99
 
100
 
101
+ def update(files, prompt, top_p=1, temperature=1):
102
  if prompt == "":
103
  raise gr.Error("Please enter a prompt.")
104
 
105
  files_info = get_files_infos(files)
106
 
107
  try:
108
+ command_string = get_completion(prompt, files_info, top_p, temperature)
109
  print(
110
  f"""\n\n/// START OF COMMAND ///:\n{command_string}\n/// END OF COMMAND ///\n\n""")
111
 
133
  output_file_name = f'output_{uuid.uuid4()}.mp4'
134
  output_file_path = str((Path(temp_dir) / output_file_name).resolve())
135
  subprocess.run(args + ["-y", output_file_path], cwd=temp_dir)
136
+ generated_command = f"### Generated Command\n```bash\n{format_bash_command(args)}\n -y output.mp4\n```"
137
+ return output_file_path, gr.update(value=generated_command)
138
  except Exception as e:
139
  print("FROM UPDATE", e)
140
  raise gr.Error(e)
156
  with gr.Blocks(css=css) as demo:
157
  gr.Markdown(
158
  """
159
+ # <span style="margin-right: 0.3rem;">🏞</span>GPT-4 Video Composer
160
  Add video, image and audio assets and ask ChatGPT to compose a new video.
161
  """,
162
  elem_id="header",
171
  label="Instructions",
172
  )
173
  btn = gr.Button("Run", label="Run")
174
+ with gr.Accordion("Parameters", open=False):
175
+ top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.05,
176
+ interactive=True, label="Top-p (nucleus sampling)")
177
+ temperature = gr.Slider(
178
+ minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
179
  with gr.Column():
180
  generated_video = gr.Video(
181
  interactive=False, label="Generated Video", include_audio=True
182
  )
183
+ generated_command = gr.Markdown()
184
 
185
  btn.click(
186
+ fn=update, inputs=[user_files, user_prompt, top_p, temperature],
187
+ outputs=[generated_video, generated_command]
188
  )
189
  with gr.Row():
190
  gr.Examples(
191
  examples=[
192
+ [["./examples/cat8.jpeg",
193
+ "./examples/cat1.jpeg",
194
+ "./examples/cat2.jpeg",
195
+ "./examples/cat3.jpeg",
196
+ "./examples/cat4.jpeg",
197
+ "./examples/cat5.jpeg",
198
+ "./examples/cat6.jpeg",
199
+ "./examples/cat7.jpeg"],
200
+ "make a video gif given each image 1s loop",
201
+ 0, 0
202
  ],
203
  [
204
  ["./examples/example.mp4"],
205
+ "please encode this video 10 times faster",
206
+ 0, 0
207
  ],
208
  [
209
  ["./examples/heat-wave.mp3", "./examples/square-image.png"],
210
  "Make a 720x720 video with a white waveform of the audio taking all screen space, also add the image as the background",
211
+ 0, 0
212
  ],
213
  [
214
  ["./examples/waterfall-overlay.png",
215
  "./examples/waterfall.mp4"],
216
  "Add the overlay to the video.",
217
+ 0, 0
218
  ],
219
  ],
220
+ inputs=[user_files, user_prompt, top_p, temperature],
221
+ outputs=[generated_video, generated_command],
222
  fn=update,
223
  cache_examples=True,
224
  )
utils.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ def format_bash_command(tokens):
2
+ formatted_command = []
3
+ indent = " " * 4
4
+ for token in tokens:
5
+ if token.startswith("-"):
6
+ formatted_command.append("\n" + indent + token)
7
+ else:
8
+ formatted_command.append(" " + token)
9
+ return "".join(formatted_command)