Spaces:
Sleeping
Sleeping
Update app.py
Browse files2 unmodified lines34567891011121314151617181920212223242526272829301 unmodified line323334353637389 unmodified lines484950515253542 unmodified lines575859606162637 unmodified lines7172737475767778798081822 unmodified linesimport 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. ""Strictly outline style, zero background. Use fill='none'. Use stroke='#000000'. "1 unmodified line"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"9 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)]2 unmodified linesgenerated_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:7 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"),outputs=gr.File(label="Download Raw Vector (.svg)"),title="OmniSVG UI Icon Engine Backend")if __name__ == "__main__":2 unmodified lines3456789101112131415161718192021222324252627282930313233343536373839404142431 unmodified line4546474849509 unmodified lines6061626364652 unmodified lines6869707172737 unmodified lines81828384858687888990912 unmodified linesimport gradio as grimport spacesfrom transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizerfrom huggingface_hub import snapshot_download# 1. Configuration# Use "/data/model_weights" if you have Persistent Storage enabled in Space SettingsLOCAL_PATH = "./model_weights"REPO_ID = "OmniSVG/OmniSVG1.1_8B"# 2. Automatic Download Checkif not os.path.exists(LOCAL_PATH) or not os.listdir(LOCAL_PATH):print(f"Local weights not found at {LOCAL_PATH}. Downloading now...")snapshot_download(repo_id=REPO_ID,local_dir=LOCAL_PATH,local_dir_use_symlinks=False)print("Download finished.")else:print(f"Found local weights at {LOCAL_PATH}. Loading directly...")print("Loading architecture from local storage...")# 3. Load from the LOCAL_PATH instead of REPO_IDconfig = AutoConfig.from_pretrained(LOCAL_PATH, trust_remote_code=True)config.architectures = ["Qwen2ForCausalLM"]tokenizer = AutoTokenizer.from_pretrained(LOCAL_PATH, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(LOCAL_PATH, # <--- Points to local folderconfig=config,dtype=torch.bfloat16,trust_remote_code=True)@spaces.GPU(duration=60)def generate_vector_icon(prompt_text):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'. "1 unmodified line"Output raw SVG xml code ONLY.")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"9 unmodified linestemperature=0.0)generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]2 unmodified linesgenerated_ids_trimmed, skip_special_tokens=True).strip()if "```xml" in response:response = response.split("```xml")[1].split("```")[0].strip()elif "```html" in response:7 unmodified linesreturn output_filenamedemo = 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 (Local Storage)")if __name__ == "__main__":
app.py+26-17