Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425263 unmodified lines30313233343536373839404142434445464748495051525354555657585960611 unmodified lineimport 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 serversystem_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'. "3 unmodified linesfull_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:"# Move model dynamically to GPU inside the ZeroGPU boundary contextdevice = "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 pathstemperature=0.0)# Strip the input tokens out of the generation payloadgenerated_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 assetoutput_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"),1 unmodified line234567891011121314151617181920212223242526273 unmodified lines3132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677781 unmodified lineimport torchimport gradio as grimport spaces# Use AutoModelForVision2Seq instead of AutoModelForCausalLMfrom transformers import AutoModelForVision2Seq, AutoProcessorMODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading processor and vision-language model configuration...")# Qwen2.5-VL architectures use a Processor instead of a basic Tokenizerprocessor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)model = AutoModelForVision2Seq.from_pretrained(MODEL_ID,dtype=torch.bfloat16, # Fixed deprecated parameter warningtrust_remote_code=True)@spaces.GPU(duration=60)def generate_vector_icon(prompt_text):"""Processes the minimalist text prompt through the Qwen2.5-VL backboneto output valid SVG path coordinates."""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'. "3 unmodified linesfull_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:"# Standard format structure for Qwen VL modelsmessages = [{"role": "user","content": [{"type": "text", "text": full_prompt}]}]# Generate the proper chat template stringtext = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)device = "cuda" if torch.cuda.is_available() else "cpu"model.to(device)# Process inputs properly for the vision-text pipelineinputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device)with torch.no_grad():generated_ids = model.generate(**inputs,max_new_tokens=1024,do_sample=False,temperature=0.0)# Strip prompt prefix IDs to extract just the new SVG tokensgenerated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]response = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0].strip()# Save code locally so Gradio can stream it to your Mac clientoutput_filename = "generated_icon.svg"with open(output_filename, "w", encoding="utf-8") as f:f.write(response)return output_filename# Expose the API matrix interfacedemo = gr.Interface(fn=generate_vector_icon,inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),
app.py+35-18