Spaces:
Runtime error
Runtime error
disable api, bump req versions
Browse files- README.md +1 -1
- app.py +82 -42
- requirements.txt +2 -2
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🏞
|
|
4 |
colorFrom: red
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: red
|
5 |
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.41.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -16,8 +16,31 @@ from utils import format_bash_command
|
|
16 |
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
17 |
openai.api_key = OPENAI_API_KEY
|
18 |
|
19 |
-
allowed_medias = [
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
|
23 |
def get_files_infos(files):
|
@@ -61,7 +84,6 @@ def get_files_infos(files):
|
|
61 |
|
62 |
|
63 |
def get_completion(prompt, files_info, top_p, temperature):
|
64 |
-
|
65 |
files_info_string = ""
|
66 |
for file_info in files_info:
|
67 |
files_info_string += f"""{file_info["type"]} {file_info["name"]}"""
|
@@ -102,10 +124,9 @@ YOUR FFMPEG COMMAND:""",
|
|
102 |
print(messages[0]["content"])
|
103 |
|
104 |
try:
|
105 |
-
completion = openai.ChatCompletion.create(
|
106 |
-
|
107 |
-
|
108 |
-
temperature=temperature)
|
109 |
|
110 |
command = completion.choices[0].message.content.replace("\n", "")
|
111 |
|
@@ -131,17 +152,16 @@ def update(files, prompt, top_p=1, temperature=1):
|
|
131 |
"Please make sure all videos are less than 2 minute long."
|
132 |
)
|
133 |
if file_info["size"] > 10000000:
|
134 |
-
raise gr.Error(
|
135 |
-
"Please make sure all files are less than 10MB in size."
|
136 |
-
)
|
137 |
try:
|
138 |
command_string = get_completion(prompt, files_info, top_p, temperature)
|
139 |
print(
|
140 |
-
f"""\n\n/// START OF COMMAND ///:\n\n{command_string}\n\n/// END OF COMMAND ///\n\n"""
|
|
|
141 |
|
142 |
# split command string into list of arguments
|
143 |
args = shlex.split(command_string)
|
144 |
-
if
|
145 |
raise Exception("Command does not start with ffmpeg")
|
146 |
temp_dir = tempfile.mkdtemp()
|
147 |
# copy files to temp dir
|
@@ -151,16 +171,16 @@ def update(files, prompt, top_p=1, temperature=1):
|
|
151 |
|
152 |
# test if ffmpeg command is valid dry run
|
153 |
ffmpg_dry_run = subprocess.run(
|
154 |
-
args + ["-f", "null", "-"], stderr=subprocess.PIPE, text=True, cwd=temp_dir
|
|
|
155 |
if ffmpg_dry_run.returncode == 0:
|
156 |
print("Command is valid.")
|
157 |
else:
|
158 |
print("Command is not valid. Error output:")
|
159 |
print(ffmpg_dry_run.stderr)
|
160 |
-
raise Exception(
|
161 |
-
"FFMPEG generated command is not valid. Please try again.")
|
162 |
|
163 |
-
output_file_name = f
|
164 |
output_file_path = str((Path(temp_dir) / output_file_name).resolve())
|
165 |
subprocess.run(args + ["-y", output_file_path], cwd=temp_dir)
|
166 |
generated_command = f"### Generated Command\n```bash\n{format_bash_command(args)}\n -y output.mp4\n```"
|
@@ -194,8 +214,10 @@ with gr.Blocks(css=css) as demo:
|
|
194 |
with gr.Row():
|
195 |
with gr.Column():
|
196 |
user_files = gr.File(
|
197 |
-
file_count="multiple",
|
198 |
-
|
|
|
|
|
199 |
)
|
200 |
user_prompt = gr.Textbox(
|
201 |
placeholder="I want to convert to a gif under 15mb",
|
@@ -203,10 +225,22 @@ with gr.Blocks(css=css) as demo:
|
|
203 |
)
|
204 |
btn = gr.Button("Run", label="Run")
|
205 |
with gr.Accordion("Parameters", open=False):
|
206 |
-
top_p = gr.Slider(
|
207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
temperature = gr.Slider(
|
209 |
-
minimum=-0,
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
with gr.Column():
|
211 |
generated_video = gr.Video(
|
212 |
interactive=False, label="Generated Video", include_audio=True
|
@@ -214,39 +248,46 @@ with gr.Blocks(css=css) as demo:
|
|
214 |
generated_command = gr.Markdown()
|
215 |
|
216 |
btn.click(
|
217 |
-
fn=update,
|
218 |
-
|
|
|
219 |
)
|
220 |
with gr.Row():
|
221 |
gr.Examples(
|
222 |
examples=[
|
223 |
-
[
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
|
|
|
|
|
|
232 |
"make a video gif, each image with 1s loop and add the audio as background",
|
233 |
-
0,
|
234 |
-
|
|
|
235 |
[
|
236 |
["./examples/example.mp4"],
|
237 |
"please encode this video 10 times faster",
|
238 |
-
0,
|
|
|
239 |
],
|
240 |
[
|
241 |
["./examples/heat-wave.mp3", "./examples/square-image.png"],
|
242 |
"Make a 720x720 video with a white waveform of the audio taking all screen space, also add the image as the background",
|
243 |
-
0,
|
|
|
244 |
],
|
245 |
[
|
246 |
-
["./examples/waterfall-overlay.png",
|
247 |
-
"./examples/waterfall.mp4"],
|
248 |
"Add the overlay to the video.",
|
249 |
-
0,
|
|
|
250 |
],
|
251 |
],
|
252 |
inputs=[user_files, user_prompt, top_p, temperature],
|
@@ -256,7 +297,6 @@ with gr.Blocks(css=css) as demo:
|
|
256 |
)
|
257 |
|
258 |
with gr.Row():
|
259 |
-
|
260 |
gr.Markdown(
|
261 |
"""
|
262 |
If you have idea to improve this please open a PR:
|
@@ -264,5 +304,5 @@ with gr.Blocks(css=css) as demo:
|
|
264 |
[![Open a Pull Request](https://huggingface.co/datasets/huggingface/badges/raw/main/open-a-pr-lg-light.svg)](https://huggingface.co/spaces/huggingface-projects/video-composer-gpt4/discussions)
|
265 |
""",
|
266 |
)
|
267 |
-
|
268 |
-
demo.launch()
|
|
|
16 |
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
17 |
openai.api_key = OPENAI_API_KEY
|
18 |
|
19 |
+
allowed_medias = [
|
20 |
+
".png",
|
21 |
+
".jpg",
|
22 |
+
".jpeg",
|
23 |
+
".tiff",
|
24 |
+
".bmp",
|
25 |
+
".gif",
|
26 |
+
".svg",
|
27 |
+
".mp3",
|
28 |
+
".wav",
|
29 |
+
".ogg",
|
30 |
+
".mp4",
|
31 |
+
".avi",
|
32 |
+
".mov",
|
33 |
+
".mkv",
|
34 |
+
".flv",
|
35 |
+
".wmv",
|
36 |
+
".webm",
|
37 |
+
".mpg",
|
38 |
+
".mpeg",
|
39 |
+
".m4v",
|
40 |
+
".3gp",
|
41 |
+
".3g2",
|
42 |
+
".3gpp",
|
43 |
+
]
|
44 |
|
45 |
|
46 |
def get_files_infos(files):
|
|
|
84 |
|
85 |
|
86 |
def get_completion(prompt, files_info, top_p, temperature):
|
|
|
87 |
files_info_string = ""
|
88 |
for file_info in files_info:
|
89 |
files_info_string += f"""{file_info["type"]} {file_info["name"]}"""
|
|
|
124 |
print(messages[0]["content"])
|
125 |
|
126 |
try:
|
127 |
+
completion = openai.ChatCompletion.create(
|
128 |
+
model="gpt-4", messages=messages, top_p=top_p, temperature=temperature
|
129 |
+
)
|
|
|
130 |
|
131 |
command = completion.choices[0].message.content.replace("\n", "")
|
132 |
|
|
|
152 |
"Please make sure all videos are less than 2 minute long."
|
153 |
)
|
154 |
if file_info["size"] > 10000000:
|
155 |
+
raise gr.Error("Please make sure all files are less than 10MB in size.")
|
|
|
|
|
156 |
try:
|
157 |
command_string = get_completion(prompt, files_info, top_p, temperature)
|
158 |
print(
|
159 |
+
f"""\n\n/// START OF COMMAND ///:\n\n{command_string}\n\n/// END OF COMMAND ///\n\n"""
|
160 |
+
)
|
161 |
|
162 |
# split command string into list of arguments
|
163 |
args = shlex.split(command_string)
|
164 |
+
if args[0] != "ffmpeg":
|
165 |
raise Exception("Command does not start with ffmpeg")
|
166 |
temp_dir = tempfile.mkdtemp()
|
167 |
# copy files to temp dir
|
|
|
171 |
|
172 |
# test if ffmpeg command is valid dry run
|
173 |
ffmpg_dry_run = subprocess.run(
|
174 |
+
args + ["-f", "null", "-"], stderr=subprocess.PIPE, text=True, cwd=temp_dir
|
175 |
+
)
|
176 |
if ffmpg_dry_run.returncode == 0:
|
177 |
print("Command is valid.")
|
178 |
else:
|
179 |
print("Command is not valid. Error output:")
|
180 |
print(ffmpg_dry_run.stderr)
|
181 |
+
raise Exception("FFMPEG generated command is not valid. Please try again.")
|
|
|
182 |
|
183 |
+
output_file_name = f"output_{uuid.uuid4()}.mp4"
|
184 |
output_file_path = str((Path(temp_dir) / output_file_name).resolve())
|
185 |
subprocess.run(args + ["-y", output_file_path], cwd=temp_dir)
|
186 |
generated_command = f"### Generated Command\n```bash\n{format_bash_command(args)}\n -y output.mp4\n```"
|
|
|
214 |
with gr.Row():
|
215 |
with gr.Column():
|
216 |
user_files = gr.File(
|
217 |
+
file_count="multiple",
|
218 |
+
label="Media files",
|
219 |
+
keep_filename=True,
|
220 |
+
file_types=allowed_medias,
|
221 |
)
|
222 |
user_prompt = gr.Textbox(
|
223 |
placeholder="I want to convert to a gif under 15mb",
|
|
|
225 |
)
|
226 |
btn = gr.Button("Run", label="Run")
|
227 |
with gr.Accordion("Parameters", open=False):
|
228 |
+
top_p = gr.Slider(
|
229 |
+
minimum=-0,
|
230 |
+
maximum=1.0,
|
231 |
+
value=1.0,
|
232 |
+
step=0.05,
|
233 |
+
interactive=True,
|
234 |
+
label="Top-p (nucleus sampling)",
|
235 |
+
)
|
236 |
temperature = gr.Slider(
|
237 |
+
minimum=-0,
|
238 |
+
maximum=5.0,
|
239 |
+
value=1.0,
|
240 |
+
step=0.1,
|
241 |
+
interactive=True,
|
242 |
+
label="Temperature",
|
243 |
+
)
|
244 |
with gr.Column():
|
245 |
generated_video = gr.Video(
|
246 |
interactive=False, label="Generated Video", include_audio=True
|
|
|
248 |
generated_command = gr.Markdown()
|
249 |
|
250 |
btn.click(
|
251 |
+
fn=update,
|
252 |
+
inputs=[user_files, user_prompt, top_p, temperature],
|
253 |
+
outputs=[generated_video, generated_command],
|
254 |
)
|
255 |
with gr.Row():
|
256 |
gr.Examples(
|
257 |
examples=[
|
258 |
+
[
|
259 |
+
[
|
260 |
+
"./examples/cat8.jpeg",
|
261 |
+
"./examples/cat1.jpeg",
|
262 |
+
"./examples/cat2.jpeg",
|
263 |
+
"./examples/cat3.jpeg",
|
264 |
+
"./examples/cat4.jpeg",
|
265 |
+
"./examples/cat5.jpeg",
|
266 |
+
"./examples/cat6.jpeg",
|
267 |
+
"./examples/cat7.jpeg",
|
268 |
+
"./examples/heat-wave.mp3",
|
269 |
+
],
|
270 |
"make a video gif, each image with 1s loop and add the audio as background",
|
271 |
+
0,
|
272 |
+
0,
|
273 |
+
],
|
274 |
[
|
275 |
["./examples/example.mp4"],
|
276 |
"please encode this video 10 times faster",
|
277 |
+
0,
|
278 |
+
0,
|
279 |
],
|
280 |
[
|
281 |
["./examples/heat-wave.mp3", "./examples/square-image.png"],
|
282 |
"Make a 720x720 video with a white waveform of the audio taking all screen space, also add the image as the background",
|
283 |
+
0,
|
284 |
+
0,
|
285 |
],
|
286 |
[
|
287 |
+
["./examples/waterfall-overlay.png", "./examples/waterfall.mp4"],
|
|
|
288 |
"Add the overlay to the video.",
|
289 |
+
0,
|
290 |
+
0,
|
291 |
],
|
292 |
],
|
293 |
inputs=[user_files, user_prompt, top_p, temperature],
|
|
|
297 |
)
|
298 |
|
299 |
with gr.Row():
|
|
|
300 |
gr.Markdown(
|
301 |
"""
|
302 |
If you have idea to improve this please open a PR:
|
|
|
304 |
[![Open a Pull Request](https://huggingface.co/datasets/huggingface/badges/raw/main/open-a-pr-lg-light.svg)](https://huggingface.co/spaces/huggingface-projects/video-composer-gpt4/discussions)
|
305 |
""",
|
306 |
)
|
307 |
+
demo.queue(api_open=False)
|
308 |
+
demo.launch(show_api=False)
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
openai==0.27
|
2 |
-
gradio==3.
|
3 |
moviepy==1
|
|
|
1 |
+
openai==0.27.10
|
2 |
+
gradio==3.41.2
|
3 |
moviepy==1
|