Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line23456789101112131415161718192021222324252 unmodified lines28293031323334358 unmodified lines4445464748495051525354555657585960616263642 unmodified lines676869707172731 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)8 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"2 unmodified linesreturn output_filename# Mount Gradio frameworkdemo = gr.Interface(fn=generate_vector_icon,inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),1 unmodified line2345678910111213141516171819202122232425262728292 unmodified lines32333435363738398 unmodified lines4849505152535455565758596061626364656667682 unmodified lines717273747576771 unmodified lineimport torchimport gradio as grimport spacesfrom transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizerMODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading customized architecture layers...")# Force the config to trick transformers into treating this as a causal text modelconfig = AutoConfig.from_pretrained(MODEL_ID, trust_remote_code=True)config.architectures = ["Qwen2ForCausalLM"] # Bypasses the strict Qwen2_5_VL validation logictokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(MODEL_ID,config=config,dtype=torch.bfloat16, # Fixed the deprecated parameter warningtrust_remote_code=True)@spaces.GPU(duration=60)def generate_vector_icon(prompt_text):"""Processes the minimalist text prompt through the OmniSVG tokenization pathto 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.")# Correct string template structure for text-driven SVG generatorsfull_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)8 unmodified linestemperature=0.0)# Strip prompt prefix array IDsgenerated_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).strip()# Strip residual codeblock fences if generated by the modelif "```xml" in response:response = response.split("```xml")[1].split("```")[0].strip()elif "```html" in response:response = response.split("```html")[1].split("```")[0].strip()elif "```" in response:response = response.split("```")[1].split("```")[0].strip()output_filename = "generated_icon.svg"2 unmodified linesreturn output_filename# Expose Gradio GUIdemo = gr.Interface(fn=generate_vector_icon,inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),
app.py+19-15