Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -50,51 +50,7 @@ filetypes = {
|
|
50 |
"Audio": [".wav", ".mp3", ".flac", ".aac"],
|
51 |
}
|
52 |
|
53 |
-
def
|
54 |
-
gap = len(idxs) / n
|
55 |
-
return [idxs[int(i * gap + gap / 2)] for i in range(n)]
|
56 |
-
|
57 |
-
def build_omni_chunks(path, prefix, instruction, sr=AUDIO_SR, seconds_per_unit=1):
|
58 |
-
clip = VideoFileClip(path, audio_fps=sr)
|
59 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
60 |
-
clip.audio.write_audiofile(tmp.name, fps=sr, codec="pcm_s16le", verbose=False, logger=None)
|
61 |
-
audio_np, _ = librosa.load(tmp.name, sr=sr, mono=True)
|
62 |
-
total_units = math.ceil(clip.duration / seconds_per_unit)
|
63 |
-
content = []
|
64 |
-
for i in range(total_units):
|
65 |
-
t = min(i * seconds_per_unit, clip.duration - 1e-3)
|
66 |
-
frame = Image.fromarray(clip.get_frame(t).astype("uint8")).convert("RGB")
|
67 |
-
audio_chunk = audio_np[sr * i * seconds_per_unit : sr * (i + 1) * seconds_per_unit]
|
68 |
-
content.extend(["<unit>", frame, audio_chunk])
|
69 |
-
clip.close()
|
70 |
-
os.remove(tmp.name)
|
71 |
-
content.append(prefix + instruction)
|
72 |
-
return content
|
73 |
-
|
74 |
-
def build_image_omni(path, prefix, instruction):
|
75 |
-
image = Image.open(path).convert("RGB")
|
76 |
-
return ["<unit>", image, prefix + instruction]
|
77 |
-
|
78 |
-
def encode_gif(path):
|
79 |
-
img = Image.open(path)
|
80 |
-
frames = [frame.copy().convert("RGB") for frame in ImageSequence.Iterator(img)]
|
81 |
-
if len(frames) > MAX_FRAMES:
|
82 |
-
frames = uniform_sample(frames, MAX_FRAMES)
|
83 |
-
return frames
|
84 |
-
|
85 |
-
def build_gif_omni(path, prefix, instruction):
|
86 |
-
frames = encode_gif(path)
|
87 |
-
content = []
|
88 |
-
for f in frames:
|
89 |
-
content.extend(["<unit>", f])
|
90 |
-
content.append(prefix + instruction)
|
91 |
-
return content
|
92 |
-
|
93 |
-
def build_audio_omni(path, prefix, instruction, sr=AUDIO_SR):
|
94 |
-
audio_np, _ = librosa.load(path, sr=sr, mono=True)
|
95 |
-
return ["<unit>", audio_np, prefix + instruction]
|
96 |
-
|
97 |
-
ef infer_filetype(ext):
|
98 |
return next((k for k, v in filetypes.items() if ext in v), None)
|
99 |
|
100 |
|
|
|
50 |
"Audio": [".wav", ".mp3", ".flac", ".aac"],
|
51 |
}
|
52 |
|
53 |
+
def infer_filetype(ext):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
return next((k for k, v in filetypes.items() if ext in v), None)
|
55 |
|
56 |
|