Spaces:
Runtime error
Runtime error
add audio channels context
Browse files
app.py
CHANGED
@@ -16,6 +16,9 @@ from utils import format_bash_command
|
|
16 |
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
17 |
openai.api_key = OPENAI_API_KEY
|
18 |
|
|
|
|
|
|
|
19 |
|
20 |
def get_files_infos(files):
|
21 |
results = []
|
@@ -29,13 +32,17 @@ def get_files_infos(files):
|
|
29 |
if file_extension in (".mp4", ".avi", ".mkv", ".mov"):
|
30 |
info["type"] = "video"
|
31 |
video = VideoFileClip(file.name)
|
32 |
-
info["duration"] =
|
33 |
info["dimensions"] = "{}x{}".format(video.size[0], video.size[1])
|
|
|
|
|
|
|
34 |
video.close()
|
35 |
elif file_extension in (".mp3", ".wav"):
|
36 |
info["type"] = "audio"
|
37 |
audio = AudioFileClip(file.name)
|
38 |
-
info["duration"] =
|
|
|
39 |
audio.close()
|
40 |
elif file_extension in (
|
41 |
".png",
|
@@ -55,7 +62,15 @@ def get_files_infos(files):
|
|
55 |
|
56 |
def get_completion(prompt, files_info, top_p, temperature):
|
57 |
|
58 |
-
files_info_string = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
messages = [
|
60 |
{
|
61 |
"role": "system",
|
@@ -73,13 +88,16 @@ Always output the media a video/mp4 and output file "output.mp4". Provide only t
|
|
73 |
|
74 |
The current assets and objective follow. Reply with the FFMPEG command:
|
75 |
|
76 |
-
AVAILABLE ASSETS LIST:
|
|
|
|
|
|
|
77 |
OBJECTIVE: {prompt}
|
78 |
YOUR FFMPEG COMMAND:""",
|
79 |
}
|
80 |
]
|
81 |
|
82 |
-
print(messages)
|
83 |
|
84 |
try:
|
85 |
completion = openai.ChatCompletion.create(model="gpt-4",
|
@@ -106,10 +124,9 @@ def update(files, prompt, top_p=1, temperature=1):
|
|
106 |
# disable this if you're running the app locally or on your own server
|
107 |
for file_info in files_info:
|
108 |
if file_info["type"] == "video":
|
109 |
-
|
110 |
-
if duration > 60:
|
111 |
raise gr.Error(
|
112 |
-
"Please make sure all videos are less than
|
113 |
)
|
114 |
if file_info["size"] > 10000000:
|
115 |
raise gr.Error(
|
@@ -118,7 +135,7 @@ def update(files, prompt, top_p=1, temperature=1):
|
|
118 |
try:
|
119 |
command_string = get_completion(prompt, files_info, top_p, temperature)
|
120 |
print(
|
121 |
-
f"""\n\n/// START OF COMMAND ///:\n{command_string}\n/// END OF COMMAND ///\n\n""")
|
122 |
|
123 |
# split command string into list of arguments
|
124 |
args = shlex.split(command_string)
|
@@ -175,7 +192,8 @@ with gr.Blocks(css=css) as demo:
|
|
175 |
with gr.Row():
|
176 |
with gr.Column():
|
177 |
user_files = gr.File(
|
178 |
-
file_count="multiple", label="Media files", keep_filename=True
|
|
|
179 |
)
|
180 |
user_prompt = gr.Textbox(
|
181 |
placeholder="I want to convert to a gif under 15mb",
|
@@ -207,8 +225,9 @@ with gr.Blocks(css=css) as demo:
|
|
207 |
"./examples/cat4.jpeg",
|
208 |
"./examples/cat5.jpeg",
|
209 |
"./examples/cat6.jpeg",
|
210 |
-
"./examples/cat7.jpeg"
|
211 |
-
|
|
|
212 |
0, 0
|
213 |
],
|
214 |
[
|
@@ -231,7 +250,7 @@ with gr.Blocks(css=css) as demo:
|
|
231 |
inputs=[user_files, user_prompt, top_p, temperature],
|
232 |
outputs=[generated_video, generated_command],
|
233 |
fn=update,
|
234 |
-
cache_examples=
|
235 |
)
|
236 |
|
237 |
with gr.Row():
|
|
|
16 |
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
17 |
openai.api_key = OPENAI_API_KEY
|
18 |
|
19 |
+
allowed_medias = [".png", ".jpg", ".jpeg", ".tiff", ".bmp", ".gif", ".svg", ".mp3", ".wav", ".ogg", ".mp4",
|
20 |
+
".avi", ".mov", ".mkv", ".flv", ".wmv", ".webm", ".mpg", ".mpeg", ".m4v", ".3gp", ".3g2", ".3gpp"]
|
21 |
+
|
22 |
|
23 |
def get_files_infos(files):
|
24 |
results = []
|
|
|
32 |
if file_extension in (".mp4", ".avi", ".mkv", ".mov"):
|
33 |
info["type"] = "video"
|
34 |
video = VideoFileClip(file.name)
|
35 |
+
info["duration"] = video.duration
|
36 |
info["dimensions"] = "{}x{}".format(video.size[0], video.size[1])
|
37 |
+
if video.audio:
|
38 |
+
info["type"] = "video/audio"
|
39 |
+
info["audio_channels"] = video.audio.nchannels
|
40 |
video.close()
|
41 |
elif file_extension in (".mp3", ".wav"):
|
42 |
info["type"] = "audio"
|
43 |
audio = AudioFileClip(file.name)
|
44 |
+
info["duration"] = audio.duration
|
45 |
+
info["audio_channels"] = audio.nchannels
|
46 |
audio.close()
|
47 |
elif file_extension in (
|
48 |
".png",
|
|
|
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"]} name {file_info["name"]}"""
|
68 |
+
if file_info["type"] == "video" or file_info["type"] == "image":
|
69 |
+
files_info_string += f""" {file_info["dimensions"]}"""
|
70 |
+
if file_info["type"] == "video" or file_info["type"] == "audio" or file_info["type"] == "video/audio":
|
71 |
+
files_info_string += f""" {file_info["duration"]}s with {file_info["audio_channels"]} audio channels"""
|
72 |
+
files_info_string += "\n"
|
73 |
+
|
74 |
messages = [
|
75 |
{
|
76 |
"role": "system",
|
|
|
88 |
|
89 |
The current assets and objective follow. Reply with the FFMPEG command:
|
90 |
|
91 |
+
AVAILABLE ASSETS LIST:
|
92 |
+
|
93 |
+
{files_info_string}
|
94 |
+
|
95 |
OBJECTIVE: {prompt}
|
96 |
YOUR FFMPEG COMMAND:""",
|
97 |
}
|
98 |
]
|
99 |
|
100 |
+
print(messages[0]["content"])
|
101 |
|
102 |
try:
|
103 |
completion = openai.ChatCompletion.create(model="gpt-4",
|
|
|
124 |
# disable this if you're running the app locally or on your own server
|
125 |
for file_info in files_info:
|
126 |
if file_info["type"] == "video":
|
127 |
+
if file_info["duration"] > 120:
|
|
|
128 |
raise gr.Error(
|
129 |
+
"Please make sure all videos are less than 2 minute long."
|
130 |
)
|
131 |
if file_info["size"] > 10000000:
|
132 |
raise gr.Error(
|
|
|
135 |
try:
|
136 |
command_string = get_completion(prompt, files_info, top_p, temperature)
|
137 |
print(
|
138 |
+
f"""\n\n/// START OF COMMAND ///:\n\n{command_string}\n\n/// END OF COMMAND ///\n\n""")
|
139 |
|
140 |
# split command string into list of arguments
|
141 |
args = shlex.split(command_string)
|
|
|
192 |
with gr.Row():
|
193 |
with gr.Column():
|
194 |
user_files = gr.File(
|
195 |
+
file_count="multiple", label="Media files", keep_filename=True,
|
196 |
+
file_types=allowed_medias
|
197 |
)
|
198 |
user_prompt = gr.Textbox(
|
199 |
placeholder="I want to convert to a gif under 15mb",
|
|
|
225 |
"./examples/cat4.jpeg",
|
226 |
"./examples/cat5.jpeg",
|
227 |
"./examples/cat6.jpeg",
|
228 |
+
"./examples/cat7.jpeg",
|
229 |
+
"./examples/heat-wave.mp3"],
|
230 |
+
"make a video gif each image 1s loop and audio as background",
|
231 |
0, 0
|
232 |
],
|
233 |
[
|
|
|
250 |
inputs=[user_files, user_prompt, top_p, temperature],
|
251 |
outputs=[generated_video, generated_command],
|
252 |
fn=update,
|
253 |
+
cache_examples=False,
|
254 |
)
|
255 |
|
256 |
with gr.Row():
|