Spaces:
Sleeping
Sleeping
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import osimport torchimport gradio as grimport spacesfrom transformers import AutoModelForCausalLM, AutoTokenizer
# ZeroGPU loads the model globally on CPU memory first; it moves to VRAM inside the decorated functionMODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading tokenizer and model configuration...")tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16, trust_remote_code=True)
@spaces.GPU(duration=60) # Requests dynamic GPU allocation from ZeroGPUdef generate_vector_icon(prompt_text): """ Takes a UI text prompt, structures it for minimalist monochrome layout, and runs inference on OmniSVG. """ # Strict UI System/Prompt engineering constraints built into the server system_rules = ( "You are an expert UI/UX line icon designer. Generate a clean, 24x24 minimalist icon. " "Strictly outline style, zero background. Use fill='none'. Use stroke='#000000'. " "Set stroke-width='2', stroke-linecap='round', stroke-linejoin='round'. " "Output raw SVG xml code ONLY." ) full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:" # Move model dynamically to GPU inside the ZeroGPU boundary context device = "cuda" if torch.cuda.is_available() else "cpu" model.to(device) inputs = tokenizer(full_prompt, return_tensors="pt").to(device) with torch.no_grad(): generated_ids = model.generate( **inputs, max_new_tokens=1024, do_sample=False, # Deterministic, sharp paths temperature=0.0 ) # Strip the input tokens out of the generation payload generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, generated_ids)] response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0].strip() # Save code to a transient local file path so Gradio can serve it back as a downloadable asset output_filename = "generated_icon.svg" with open(output_filename, "w", encoding="utf-8") as f: f.write(response) return output_filename
# Expose a minimal UI endpoint that acts as the API matrix for your local Macdemo = gr.Interface( fn=generate_vector_icon, inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"), outputs=gr.File(label="Download Raw Vector (.svg)"), title="OmniSVG UI Icon Engine Backend")
if __name__ == "__main__": demo.launch()app.pyAdded+67