Spaces:
Running on Zero
Running on Zero
Download zero_gpu.py from dodoyy/Motion-SVG-Vector: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/dodoyy/Motion-SVG-Vector/resolve/main/zero_gpu.py
- Command line
-
hf download hf://spaces/dodoyy/Motion-SVG-Vector/zero_gpu.py
-
curl -L -o zero_gpu.py https://huggingface.co/spaces/dodoyy/Motion-SVG-Vector/resolve/main/zero_gpu.py
1.37 kB
| import spaces | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| MODEL_ID = "Qwen/Qwen3-4B-Instruct-2507" | |
| MODEL_REVISION = "cdbee75f17c01a7cc42f958dc650907174af0554" | |
| GPU_DURATION = 60 | |
| MAX_NEW_TOKENS = 2400 | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, revision=MODEL_REVISION) | |
| # ZeroGPU captures CUDA allocations during startup for use by the GPU worker. | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| revision=MODEL_REVISION, | |
| dtype=torch.bfloat16, | |
| device_map={"": "cuda"}, | |
| use_safetensors=True, | |
| ).eval() | |
| def complete(system: str, prompt: str, *, max_tokens: int) -> str: | |
| inputs = tokenizer.apply_chat_template( | |
| [ | |
| {"role": "system", "content": system}, | |
| {"role": "user", "content": prompt}, | |
| ], | |
| add_generation_prompt=True, | |
| tokenize=True, | |
| return_dict=True, | |
| return_tensors="pt", | |
| ).to(model.device) | |
| with torch.inference_mode(): | |
| result = model.generate( | |
| **inputs, | |
| max_new_tokens=min(max_tokens, MAX_NEW_TOKENS), | |
| max_time=45, | |
| do_sample=True, | |
| temperature=0.4, | |
| top_p=0.9, | |
| ) | |
| generated = result[0, inputs["input_ids"].shape[-1]:].tolist() | |
| return tokenizer.decode(generated, skip_special_tokens=True).strip() | |