Spaces:
Paused
Paused
์ด ํ ๋ฆฌํ์คํธ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ ์ค๋์ค ์ธ๋๊ฐ ์๋ก ๋ค๋ฅผ ์ ์์ต๋๋ค./This PR makes each generation different
17a3bc3 verified | import random | |
| import torch | |
| import torchaudio | |
| from einops import rearrange | |
| import gradio as gr | |
| import spaces | |
| import os | |
| import uuid | |
| from transformers import pipeline | |
| # Importing the model-related functions | |
| from stable_audio_tools import get_pretrained_model | |
| from stable_audio_tools.inference.generation import generate_diffusion_cond | |
| # Load the model outside of the GPU-decorated function | |
| def load_model(): | |
| print("Loading model...") | |
| model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0") | |
| print("Model loaded successfully.") | |
| return model, model_config | |
| # ๋ฒ์ญ ๋ชจ๋ธ ๋ก๋ | |
| translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en") | |
| # Function to set up, generate, and process the audio | |
| # Allocate GPU only when this function is called | |
| def generate_audio(prompt, seconds_total=30, steps=100, cfg_scale=7): | |
| print(f"Original Prompt: {prompt}") | |
| # ํ๊ธ ํ ์คํธ๋ฅผ ์์ด๋ก ๋ฒ์ญ | |
| translated_prompt = translator(prompt, max_length=512)[0]['translation_text'] | |
| print(f"Translated Prompt: {translated_prompt}") | |
| seed = random.randint(0, 2**63 - 1) | |
| random.seed(seed) | |
| torch.manual_seed(seed) | |
| print(f"Using seed: {seed}") | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print(f"Using device: {device}") | |
| # Fetch the Hugging Face token from the environment variable | |
| hf_token = os.getenv('HF_TOKEN') | |
| print(f"Hugging Face token: {hf_token}") | |
| # Use pre-loaded model and configuration | |
| model, model_config = load_model() | |
| sample_rate = model_config["sample_rate"] | |
| sample_size = model_config["sample_size"] | |
| print(f"Sample rate: {sample_rate}, Sample size: {sample_size}") | |
| model = model.to(device) | |
| print("Model moved to device.") | |
| # Set up text and timing conditioning | |
| conditioning = [{ | |
| "prompt": translated_prompt, | |
| "seconds_start": 0, | |
| "seconds_total": seconds_total | |
| }] | |
| print(f"Conditioning: {conditioning}") | |
| # Generate stereo audio | |
| print("Generating audio...") | |
| output = generate_diffusion_cond( | |
| model, | |
| steps=steps, | |
| cfg_scale=cfg_scale, | |
| conditioning=conditioning, | |
| sample_size=sample_size, | |
| sigma_min=0.3, | |
| sigma_max=500, | |
| sampler_type="dpmpp-3m-sde", | |
| device=device | |
| ) | |
| print("Audio generated.") | |
| # Rearrange audio batch to a single sequence | |
| output = rearrange(output, "b d n -> d (b n)") | |
| print("Audio rearranged.") | |
| # Peak normalize, clip, convert to int16 | |
| output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu() | |
| print("Audio normalized and converted.") | |
| # Generate a unique filename for the output | |
| unique_filename = f"output_{uuid.uuid4().hex}.wav" | |
| print(f"Saving audio to file: {unique_filename}") | |
| # Save to file | |
| torchaudio.save(unique_filename, output, sample_rate) | |
| print(f"Audio saved: {unique_filename}") | |
| # Return the path to the generated audio file | |
| return unique_filename | |
| css = """ | |
| footer { | |
| visibility: hidden; | |
| } | |
| """ | |
| # Setting up the Gradio Interface | |
| interface = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css, | |
| fn=generate_audio, | |
| inputs=[ | |
| gr.Textbox(label="ํ๋กฌํํธ", placeholder="์ฌ๊ธฐ์ ํ ์คํธ ํ๋กฌํํธ๋ฅผ ์ ๋ ฅํ์ธ์"), | |
| gr.Slider(0, 47, value=30, label="์ค๋์ค ๊ธธ์ด (์ด)"), | |
| gr.Slider(10, 150, value=100, step=10, label="๋ํจ์ ๋จ๊ณ ์"), | |
| gr.Slider(1, 15, value=7, step=0.1, label="CFG ์ค์ผ์ผ") | |
| ], | |
| outputs=gr.Audio(type="filepath", label="์์ฑ๋ ์ค๋์ค"), | |
| ) | |
| # Pre-load the model to avoid multiprocessing issues | |
| model, model_config = load_model() | |
| # Launch the Interface | |
| interface.launch() | |