Spaces:
Running on Zero
Running on Zero
Build the model once in the worker, size the GPU reservation by the length, and check requests before the GPU is reserved.
Browse files43 unmodified lines4445464748495051529 unmodified lines626364656667686970717273747576775 unmodified lines838485868788898 unmodified lines989910010110210343 unmodified linesreturn os.path.join("/tmp", f"t2m_{time_ns()}_{uuid4().hex[:8]}{ext}")# Core Text -> MIDIdef generate_midi(prompt: str, temperature: float = 0.9, max_len: int = 500) -> str:device = "cuda" if torch.cuda.is_available() else "cpu"# Load REMI vocab/tokenizer (pickle dict used by the provided model)with open(TOKENIZER_PATH, "rb") as f:9 unmodified lines1024, # max_seq_lenFalse, # use_rotary8, # rotary_dimdevice=device # device)model.load_state_dict(torch.load(MODEL_PATH, map_location=device))model.eval()tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)input_ids = nn.utils.rnn.pad_sequence(inputs.input_ids, batch_first=True, padding_value=0).to(device)attention_mask = nn.utils.rnn.pad_sequence(inputs.attention_mask, batch_first=True, padding_value=0).to(device)with torch.no_grad():output = model.generate(input_ids, attention_mask, max_len=max_len, temperature=temperature)5 unmodified linesgenerated_midi.dump_midi(midi_path)return midi_path@spaces.GPU(duration=120)def process_fn(prompt: str, temperature: float, max_length: int):"""Generate a MIDI from a text description.8 unmodified linesmidi_path (str): The generated MIDI."""try:return generate_midi(prompt, float(temperature), int(max_length))except Exception as error:43 unmodified lines4445464748495051525354555657589 unmodified lines686970717273747576777879808182838485868788899091929394955 unmodified lines1011021031041051061071081091108 unmodified lines11912012112212312412512612712812913013113213343 unmodified linesreturn os.path.join("/tmp", f"t2m_{time_ns()}_{uuid4().hex[:8]}{ext}")DEVICE = "cuda" if torch.cuda.is_available() else "cpu"model = Noner_tokenizer = Nonetokenizer = Nonedef load_model():"""Builds the model on the CPU, then places it on DEVICE."""global model, r_tokenizer, tokenizer# Load REMI vocab/tokenizer (pickle dict used by the provided model)with open(TOKENIZER_PATH, "rb") as f:9 unmodified lines1024, # max_seq_lenFalse, # use_rotary8, # rotary_dimdevice="cpu" # device)model.load_state_dict(torch.load(MODEL_PATH, map_location="cpu"))model.eval()model.to(DEVICE)tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-base")# Built in the worker, which runs process_fn, on its main thread, where ZeroGPU's emulation records# the CUDA placement, so that no request spends the caller's allowance on building it. The server# never runs process_fn.if __name__ != "__main__":load_model()# Core Text -> MIDIdef generate_midi(prompt: str, temperature: float = 0.9, max_len: int = 500) -> str:inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)input_ids = nn.utils.rnn.pad_sequence(inputs.input_ids, batch_first=True, padding_value=0).to(DEVICE)attention_mask = nn.utils.rnn.pad_sequence(inputs.attention_mask, batch_first=True, padding_value=0).to(DEVICE)with torch.no_grad():output = model.generate(input_ids, attention_mask, max_len=max_len, temperature=temperature)5 unmodified linesgenerated_midi.dump_midi(midi_path)return midi_pathdef gpu_seconds(prompt: str, temperature: float, max_length: int) -> float:return 15 + 0.1 * int(max_length)def process_fn(prompt: str, temperature: float, max_length: int):"""Generate a MIDI from a text description.8 unmodified linesmidi_path (str): The generated MIDI."""# Checked before the GPU is reserved, so a request the model cannot take spends none of the# caller's ZeroGPU allowanceif not (prompt or "").strip():raise gr.Error("Describe the music to generate.")return compose(prompt, temperature, max_length)@spaces.GPU(duration=gpu_seconds)def compose(prompt: str, temperature: float, max_length: int) -> str:try:return generate_midi(prompt, float(temperature), int(max_length))except Exception as error:
app.py+38-8