Update app.py
Browse files1 unmodified line234556789101112131415166 unmodified lines23242523262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110212 unmodified lines1151161175711811912012112212312412512612712812913013113213313413513613713813915 unmodified lines155156157861588889159160161162163951641659816616716816917017117217317415 unmodified lines1901911921241931941951961971981992002012022032042051311321331341351361372062072082092102112122131391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422142152162172182192202472482492502512522212222232242252262272542552562282292302312322332602612342352362372381 unmodified lineimport loggingfrom datetime import datetimefrom pathlib import Path
import 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 FeaturesUtilsimport tempfile
# 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 lines
return net, feature_utils, seq_cfg
net, 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 translation return text
def 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 lines cfg_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): prompt = translate_prompt(prompt) negative_prompt = translate_prompt(negative_prompt)
rng = torch.Generator(device=device) rng.manual_seed(seed)15 unmodified lines
audio_save_path = tempfile.NamedTemporaryFile(delete=False, suffix='.flac').name torchaudio.save(audio_save_path, audio, seq_cfg.sampling_rate) log.info(f'Saved audio to {audio_save_path}') 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(), 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), 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', 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, ], ]) outputs="playable_video", css=custom_css)
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), 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', cache_examples=False, title='MMAudio β Text-to-Audio Synthesis', outputs="audio", css=custom_css)
# λ©μΈ μ€νif __name__ == "__main__": gr.TabbedInterface([video_to_audio_tab, text_to_audio_tab], ['Video-to-Audio', 'Text-to-Audio']).launch(allowed_paths=[output_dir]) 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