Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line23456567989111213101112131514151617181920222321222324252 unmodified lines2829303233343536373839404142433132333435364849373839403 unmodified lines4445465947484950516465665253545568565758596061626364656667686975707172731 unmodified lineimport torchimport gradio as grimport spaces# Use the explicit model class for Qwen2.5-VL architecturesfrom transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessorfrom transformers import AutoModelForCausalLM, AutoTokenizer
MODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading processor and Qwen2.5-VL vision-language model architecture...")print("Loading tokenizer and model layers...")
# Initialize the processor and the model cleanlyprocessor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)model = Qwen2_5_VLForConditionalGeneration.from_pretrained(# 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, dtype=torch.bfloat16, torch_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 backbone to output valid SVG path coordinates. Processes text inputs through the OmniSVG tokenization path to 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." ) full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:" # Structure text formatting using Qwen-VL chat message guidelines messages = [ { "role": "user", "content": [{"type": "text", "text": full_prompt}] } ] # Process text layout templates text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) # Standard prompt injection format for text-space SVG backends full_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) # Parse through the multi-modal text pipeline inputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device) inputs = tokenizer(full_prompt, return_tensors="pt").to(device) with torch.no_grad(): generated_ids = model.generate(3 unmodified lines temperature=0.0 ) # Strip layout prefix arrays # Exclude prompt vectors from final token generation stream generated_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() response = tokenizer.batch_decode( generated_ids_trimmed, skip_special_tokens=True )[0].strip() # Write structural stream data to file assets # Strip residual markdown ticks if the model returns them if 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
# Expose API endpoint layers# 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