Update app.py
Browse files1 unmodified line23456789101112136 unmodified lines2021222324252627282930313233343536373839404112 unmodified lines545556575859606162636465666715 unmodified lines838485868788899091929394959697989910010110210310410515 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602611 unmodified lineimport loggingfrom datetime import datetimefrom pathlib import Pathimport gradio as grimport torchimport torchaudioimport ostry:import mmaudioexcept ImportError:6 unmodified linesfrom mmaudio.model.networks import MMAudio, get_my_mmaudiofrom mmaudio.model.sequence_config import SequenceConfigfrom mmaudio.model.utils.features_utils import FeaturesUtilsimport tempfiletorch.backends.cuda.matmul.allow_tf32 = Truetorch.backends.cudnn.allow_tf32 = Truelog = logging.getLogger()device = 'cuda'dtype = torch.bfloat16model: ModelConfig = all_model_cfg['large_44k_v2']model.download_if_needed()output_dir = Path('./output/gradio')setup_eval_logging()def get_model() -> tuple[MMAudio, FeaturesUtils, SequenceConfig]:seq_cfg = model.seq_cfg12 unmodified linesreturn net, feature_utils, seq_cfgnet, feature_utils, seq_cfg = get_model()@spaces.GPU@torch.inference_mode()def video_to_audio(video: gr.Video, prompt: str, negative_prompt: str, seed: int, num_steps: int,cfg_strength: float, duration: float):rng = torch.Generator(device=device)rng.manual_seed(seed)15 unmodified linescfg_strength=cfg_strength)audio = audios.float().cpu()[0]# current_time_string = datetime.now().strftime('%Y%m%d_%H%M%S')video_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name# output_dir.mkdir(exist_ok=True, parents=True)# video_save_path = output_dir / f'{current_time_string}.mp4'make_video(video,video_save_path,audio,sampling_rate=seq_cfg.sampling_rate,duration_sec=seq_cfg.duration)log.info(f'Saved video to {video_save_path}')return video_save_path@spaces.GPU@torch.inference_mode()def text_to_audio(prompt: str, negative_prompt: str, seed: int, num_steps: int, cfg_strength: float,duration: float):rng = torch.Generator(device=device)rng.manual_seed(seed)15 unmodified linesaudio_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.flac').nametorchaudio.save(audio_save_path, audio, seq_cfg.sampling_rate)log.info(f'Saved audio to {audio_save_path}')return audio_save_pathvideo_to_audio_tab = gr.Interface(fn=video_to_audio,inputs=[gr.Video(),gr.Text(label='Prompt'),gr.Text(label='Negative prompt', value='music'),gr.Number(label='Seed', value=0, precision=0, minimum=0),gr.Number(label='Num steps', value=25, precision=0, minimum=1),gr.Number(label='Guidance Strength', value=4.5, minimum=1),gr.Number(label='Duration (sec)', value=8, minimum=1),],outputs='playable_video',cache_examples=False,title='MMAudio โ Video-to-Audio Synthesis',examples=[['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_beach.mp4','waves, seagulls','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_serpent.mp4','','music',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_seahorse.mp4','bubbles','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_india.mp4','Indian holy music','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_galloping.mp4','galloping','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_kraken.mp4','waves, storm','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/sora_nyc.mp4','','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/mochi_storm.mp4','storm','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/hunyuan_spring.mp4','','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/hunyuan_typing.mp4','typing','',0,25,4.5,10,],['https://huggingface.co/hkchengrex/MMAudio/resolve/main/examples/hunyuan_wake_up.mp4','','',0,25,4.5,10,],])text_to_audio_tab = gr.Interface(fn=text_to_audio,inputs=[gr.Text(label='Prompt'),gr.Text(label='Negative prompt'),gr.Number(label='Seed', value=0, precision=0, minimum=0),gr.Number(label='Num steps', value=25, precision=0, minimum=1),gr.Number(label='Guidance Strength', value=4.5, minimum=1),gr.Number(label='Duration (sec)', value=8, minimum=1),],outputs='audio',cache_examples=False,title='MMAudio โ Text-to-Audio Synthesis',)if __name__ == "__main__":gr.TabbedInterface([video_to_audio_tab, text_to_audio_tab],['Video-to-Audio', 'Text-to-Audio']).launch(allowed_paths=[output_dir])1 unmodified line23456789101112131415166 unmodified lines232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110212 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813915 unmodified lines15515615715815916016116216316416516616716816917017117217317415 unmodified lines1901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372381 unmodified lineimport loggingfrom datetime import datetimefrom pathlib import Pathimport gradio as grimport torchimport torchaudioimport osfrom transformers import pipelinefrom pixabay import Image, Videoimport tempfile# ๊ธฐ๋ณธ ์ค์ try:import mmaudioexcept ImportError:6 unmodified linesfrom mmaudio.model.networks import MMAudio, get_my_mmaudiofrom mmaudio.model.sequence_config import SequenceConfigfrom mmaudio.model.utils.features_utils import FeaturesUtils# CUDA ์ค์ torch.backends.cuda.matmul.allow_tf32 = Truetorch.backends.cudnn.allow_tf32 = True# ๋ก๊น ์ค์ log = logging.getLogger()# ์ฅ์น ๋ฐ ๋ฐ์ดํฐ ํ์ ์ค์ device = 'cuda'dtype = torch.bfloat16# ๋ชจ๋ธ ์ค์ model: ModelConfig = all_model_cfg['large_44k_v2']model.download_if_needed()output_dir = Path('./output/gradio')setup_eval_logging()# ๋ฒ์ญ๊ธฐ ๋ฐ Pixabay API ์ค์ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")PIXABAY_API_KEY = "33492762-a28a596ec4f286f84cd328b17"pixabay_video = Video(PIXABAY_API_KEY)# CSS ์คํ์ผ ์ ์custom_css = """.gradio-container {background: linear-gradient(45deg, #1a1a1a, #2a2a2a);border-radius: 15px;box-shadow: 0 8px 32px rgba(0,0,0,0.3);}.input-container, .output-container {background: rgba(255,255,255,0.1);backdrop-filter: blur(10px);border-radius: 10px;padding: 20px;transform-style: preserve-3d;transition: transform 0.3s ease;}.input-container:hover {transform: translateZ(20px);}.gallery-item {transition: transform 0.3s ease;border-radius: 8px;overflow: hidden;}.gallery-item:hover {transform: scale(1.05);box-shadow: 0 4px 15px rgba(0,0,0,0.2);}.tabs {background: rgba(255,255,255,0.05);border-radius: 10px;padding: 10px;}button {background: linear-gradient(45deg, #4a90e2, #357abd);border: none;border-radius: 5px;transition: all 0.3s ease;}button:hover {transform: translateY(-2px);box-shadow: 0 4px 15px rgba(74,144,226,0.3);}"""def get_model() -> tuple[MMAudio, FeaturesUtils, SequenceConfig]:seq_cfg = model.seq_cfg12 unmodified linesreturn net, feature_utils, seq_cfgnet, feature_utils, seq_cfg = get_model()def translate_prompt(text):if text and any(ord(char) >= 0x3131 and ord(char) <= 0xD7A3 for char in text):translation = translator(text)[0]['translation_text']return translationreturn textdef search_videos(query):query = translate_prompt(query)videos = pixabay_video.search(q=query, per_page=80)return [video.video_large for video in videos['hits']]@spaces.GPU@torch.inference_mode()def video_to_audio(video: gr.Video, prompt: str, negative_prompt: str, seed: int, num_steps: int,cfg_strength: float, duration: float):prompt = translate_prompt(prompt)negative_prompt = translate_prompt(negative_prompt)rng = torch.Generator(device=device)rng.manual_seed(seed)15 unmodified linescfg_strength=cfg_strength)audio = audios.float().cpu()[0]video_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').namemake_video(video,video_save_path,audio,sampling_rate=seq_cfg.sampling_rate,duration_sec=seq_cfg.duration)return video_save_path@spaces.GPU@torch.inference_mode()def text_to_audio(prompt: str, negative_prompt: str, seed: int, num_steps: int, cfg_strength: float,duration: float):prompt = translate_prompt(prompt)negative_prompt = translate_prompt(negative_prompt)rng = torch.Generator(device=device)rng.manual_seed(seed)15 unmodified linesaudio_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.flac').nametorchaudio.save(audio_save_path, audio, seq_cfg.sampling_rate)return audio_save_path# ์ธํฐํ์ด์ค ์ ์video_search_tab = gr.Interface(fn=search_videos,inputs=gr.Textbox(label="๊ฒ์์ด ์ ๋ ฅ"),outputs=gr.Gallery(label="๊ฒ์ ๊ฒฐ๊ณผ", columns=4, rows=20),css=custom_css)video_to_audio_tab = gr.Interface(fn=video_to_audio,inputs=[gr.Video(label="๋น๋์ค"),gr.Textbox(label="ํ๋กฌํํธ"),gr.Textbox(label="๋ค๊ฑฐํฐ๋ธ ํ๋กฌํํธ", value="music"),gr.Number(label="์๋", value=0),gr.Number(label="์คํ ์", value=25),gr.Number(label="๊ฐ์ด๋ ๊ฐ๋", value=4.5),gr.Number(label="๊ธธ์ด(์ด)", value=8),],outputs="playable_video",css=custom_css)text_to_audio_tab = gr.Interface(fn=text_to_audio,inputs=[gr.Textbox(label="ํ๋กฌํํธ"),gr.Textbox(label="๋ค๊ฑฐํฐ๋ธ ํ๋กฌํํธ"),gr.Number(label="์๋", value=0),gr.Number(label="์คํ ์", value=25),gr.Number(label="๊ฐ์ด๋ ๊ฐ๋", value=4.5),gr.Number(label="๊ธธ์ด(์ด)", value=8),],outputs="audio",css=custom_css)# ๋ฉ์ธ ์คํif __name__ == "__main__":gr.TabbedInterface([video_search_tab, video_to_audio_tab, text_to_audio_tab],["๋น๋์ค ๊ฒ์", "๋น๋์ค-์ค๋์ค ๋ณํ", "ํ ์คํธ-์ค๋์ค ๋ณํ"],css=custom_css).launch(allowed_paths=[output_dir])
app.py+108-131