Spaces:
Runtime error
Runtime error
Commit ·
e77fc2d
1
Parent(s): 686c4ae
Update app.py
Browse files19 unmodified lines202122232425267 unmodified lines34353637383977 unmodified lines1171181191201211221 unmodified line12412512612712812929 unmodified lines15916016116216316416516616716840 unmodified lines2092102112122132142152162172182192206 unmodified lines22722822923023123211 unmodified lines2442452462472482492502512522532544 unmodified lines25926026126226326485 unmodified lines35035135235319 unmodified linesimport avimport subprocessimport librosaimport reargs = {"model": "./ckpts/checkpoint.pth", "llama_type": "7B", "llama_dir": "./ckpts/LLaMA-2","mert_path": "m-a-p/MERT-v1-330M", "vit_path": "google/vit-base-patch16-224", "vivit_path": "google/vivit-b-16x2-kinetics400",7 unmodified linesargs = dotdict(args)llama_type = args.llama_typellama_ckpt_dir = os.path.join(args.llama_dir, llama_type)llama_tokenzier_path = args.llama_dir77 unmodified linesdef save_audio_to_local(audio, sec):if not os.path.exists('temp'):os.mkdir('temp')filename = os.path.join('temp', next(tempfile._get_candidate_names()) + '.wav')1 unmodified linescipy.io.wavfile.write(filename, rate=16000, data=audio[0])else:scipy.io.wavfile.write(filename, rate=model.generation_model.config.audio_encoder.sampling_rate, data=audio)return filename29 unmodified linesdef reset_dialog():return [], []def reset_state():return None, None, None, None, [], [], []40 unmodified linesdef get_audio_length(filename):return int(round(librosa.get_duration(path=filename)))def get_last_audio():for hist in history[::-1]:print(hist)if "<audio controls playsinline>" in hist[1]:return re.search('<audio controls playsinline><source src=\"\.\/file=(.*)\" type="audio\/wav"><\/audio>', hist[1]).group(1)return Nonedef predict(prompt_input,6 unmodified lineshistory,modality_cache,audio_length_in_s):prompts = [llama.format_prompt(prompt_input)]prompts = [model.tokenizer(x).input_ids for x in prompts]print(image_path, audio_path, video_path)11 unmodified linescontainer = av.open(video_path)indices = sample_frame_indices(clip_len=32, frame_sample_rate=1, seg_len=container.streams.video[0].frames)video = read_video_pyav(container=container, indices=indices)generated_audio_file = get_last_audio()if generated_audio_file is not None:audio_length_in_s = get_audio_length(generated_audio_file)sample_rate = 24000waveform, sr = torchaudio.load(generated_audio_file)if sample_rate != sr:waveform = torchaudio.functional.resample(waveform, orig_freq=sr, new_freq=sample_rate)audio = torch.mean(waveform, 0)4 unmodified linesprint(f"Video Length: {audio_length_in_s}")if audio_path is not None:audio_length_in_s = get_audio_length(audio_path)print(f"Audio Length: {audio_length_in_s}")print(image, video, audio)85 unmodified lines], show_progress=True)if __name__ == "__main__":demo.launch()19 unmodified lines2021222324257 unmodified lines333435363738394077 unmodified lines1181191201211221231241 unmodified line12612712812913013113229 unmodified lines16216316416516616716816917017117217317417540 unmodified lines2162172182192202216 unmodified lines22822923023123223323411 unmodified lines2462472482492502512522532542552564 unmodified lines26126226326426526626785 unmodified lines35335435535619 unmodified linesimport avimport subprocessimport librosaargs = {"model": "./ckpts/checkpoint.pth", "llama_type": "7B", "llama_dir": "./ckpts/LLaMA-2","mert_path": "m-a-p/MERT-v1-330M", "vit_path": "google/vit-base-patch16-224", "vivit_path": "google/vivit-b-16x2-kinetics400",7 unmodified linesargs = dotdict(args)generated_audio_files = []llama_type = args.llama_typellama_ckpt_dir = os.path.join(args.llama_dir, llama_type)llama_tokenzier_path = args.llama_dir77 unmodified linesdef save_audio_to_local(audio, sec):global generated_audio_filesif not os.path.exists('temp'):os.mkdir('temp')filename = os.path.join('temp', next(tempfile._get_candidate_names()) + '.wav')1 unmodified linescipy.io.wavfile.write(filename, rate=16000, data=audio[0])else:scipy.io.wavfile.write(filename, rate=model.generation_model.config.audio_encoder.sampling_rate, data=audio)generated_audio_files.append(filename)return filename29 unmodified linesdef reset_dialog():global generated_audio_filesgenerated_audio_files = []return [], []def reset_state():global generated_audio_filesgenerated_audio_files = []return None, None, None, None, [], [], []40 unmodified linesdef get_audio_length(filename):return int(round(librosa.get_duration(path=filename)))def predict(prompt_input,6 unmodified lineshistory,modality_cache,audio_length_in_s):global generated_audio_filesprompts = [llama.format_prompt(prompt_input)]prompts = [model.tokenizer(x).input_ids for x in prompts]print(image_path, audio_path, video_path)11 unmodified linescontainer = av.open(video_path)indices = sample_frame_indices(clip_len=32, frame_sample_rate=1, seg_len=container.streams.video[0].frames)video = read_video_pyav(container=container, indices=indices)if len(generated_audio_files) != 0:audio_length_in_s = get_audio_length(generated_audio_files[-1])sample_rate = 24000waveform, sr = torchaudio.load(generated_audio_files[-1])if sample_rate != sr:waveform = torchaudio.functional.resample(waveform, orig_freq=sr, new_freq=sample_rate)audio = torch.mean(waveform, 0)4 unmodified linesprint(f"Video Length: {audio_length_in_s}")if audio_path is not None:audio_length_in_s = get_audio_length(audio_path)generated_audio_files.append(audio_path)print(f"Audio Length: {audio_length_in_s}")print(image, video, audio)85 unmodified lines], show_progress=True)if __name__ == "__main__":demo.launch()
app.py+15-12