Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345678910111213141516171819202122232425262 unmodified lines2930313233343536373839404142434445464748495051523 unmodified lines56575859606162636465666768697071727374757677781 unmodified lineimport torchimport gradio as grimport spaces# Use the explicit model class for Qwen2.5-VL architecturesfrom transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessorMODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading processor and Qwen2.5-VL vision-language model architecture...")# Initialize the processor and the model cleanlyprocessor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)model = Qwen2_5_VLForConditionalGeneration.from_pretrained(MODEL_ID,dtype=torch.bfloat16,trust_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. "2 unmodified lines"Output raw SVG xml code ONLY.")full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:"# Structure text formatting using Qwen-VL chat message guidelinesmessages = [{"role": "user","content": [{"type": "text", "text": full_prompt}]}]# Process text layout templatestext = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)device = "cuda" if torch.cuda.is_available() else "cpu"model.to(device)# Parse through the multi-modal text pipelineinputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device)with torch.no_grad():generated_ids = model.generate(3 unmodified linestemperature=0.0)# Strip layout prefix arraysgenerated_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).strip()# Write structural stream data to file assetsoutput_filename = "generated_icon.svg"with open(output_filename, "w", encoding="utf-8") as f:f.write(response)return output_filename# Expose API endpoint layersdemo = gr.Interface(fn=generate_vector_icon,inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),1 unmodified line23456789101112131415161718192021222324252 unmodified lines282930313233343536373839403 unmodified lines4445464748495051525354555657585960616263646566676869707172731 unmodified lineimport torchimport gradio as grimport spacesfrom transformers import AutoModelForCausalLM, AutoTokenizerMODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading tokenizer and model layers...")# Explicitly load using the standard text stack to avoid missing vision preprocessor issuestokenizer = 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)def generate_vector_icon(prompt_text):"""Processes text inputs through the OmniSVG tokenization pathto generate raw mathematical code blocks."""system_rules = ("You are an expert UI/UX line icon designer. Generate a clean, 24x24 minimalist icon. "2 unmodified lines"Output raw SVG xml code ONLY.")# Standard prompt injection format for text-space SVG backendsfull_prompt = f"<|im_start|>system\n{system_rules}<|im_end|>\n<|im_start|>user\nIcon Request: {prompt_text}<|im_end|>\n<|im_start|>assistant\nSVG Output:"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(3 unmodified linestemperature=0.0)# Exclude prompt vectors from final token generation streamgenerated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]response = tokenizer.batch_decode(generated_ids_trimmed, skip_special_tokens=True)[0].strip()# Strip residual markdown ticks if the model returns themif response.startswith("```xml"):response = response.split("```xml")[1].split("```")[0].strip()elif response.startswith("```html"):response = response.split("```html")[1].split("```")[0].strip()elif response.startswith("```"):response = response.split("```")[1].split("```")[0].strip()output_filename = "generated_icon.svg"with open(output_filename, "w", encoding="utf-8") as f:f.write(response)return output_filename# Mount Gradio frameworkdemo = gr.Interface(fn=generate_vector_icon,inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),
app.py+24-29