Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234565678910111112131314151516171812 unmodified lines31323334343536371 unmodified line3940414242434445464748484950514 unmodified lines565758595960616263646566666768686970711 unmodified lineimport torchimport gradio as grimport spaces# Use AutoModelForVision2Seq instead of AutoModelForCausalLMfrom transformers import AutoModelForVision2Seq, AutoProcessor# Corrected imports for vision-language generation modelsfrom transformers import AutoModelForConditionalGeneration, AutoProcessor
MODEL_ID = "OmniSVG/OmniSVG1.1_8B"print("Loading processor and vision-language model configuration...")
# Qwen2.5-VL architectures use a Processor instead of a basic Tokenizer# Initialize using the proper Auto classesprocessor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)model = AutoModelForVision2Seq.from_pretrained(model = AutoModelForConditionalGeneration.from_pretrained( MODEL_ID, dtype=torch.bfloat16, # Fixed deprecated parameter warning dtype=torch.bfloat16, trust_remote_code=True)
12 unmodified lines full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:" # Standard format structure for Qwen VL models # Standard messaging array format structure for Qwen VL backbones messages = [ { "role": "user",1 unmodified line } ] # Generate the proper chat template string # Render the input structure with chat templates text = 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 pipeline # Package input dictionaries onto the runtime device inputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device) with torch.no_grad():4 unmodified lines temperature=0.0 ) # Strip prompt prefix IDs to extract just the new SVG tokens # Strip prompt prefix IDs to extract just the generated SVG text payload 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 )[0].strip() ).strip() # Save code locally so Gradio can stream it to your Mac client # Write the response block to an asset file so Gradio client can fetch it output_filename = "generated_icon.svg" with open(output_filename, "w", encoding="utf-8") as f: f.write(response)app.py+11-11