Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,28 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
|
| 7 |
MODEL_ID = "OmniSVG/OmniSVG1.1_8B"
|
| 8 |
-
print("Loading
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Explicitly load using the standard text stack to avoid missing vision preprocessor issues
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
MODEL_ID,
|
| 14 |
-
|
|
|
|
| 15 |
trust_remote_code=True
|
| 16 |
)
|
| 17 |
|
| 18 |
@spaces.GPU(duration=60)
|
| 19 |
def generate_vector_icon(prompt_text):
|
| 20 |
"""
|
| 21 |
-
Processes text
|
| 22 |
-
to
|
| 23 |
"""
|
| 24 |
system_rules = (
|
| 25 |
"You are an expert UI/UX line icon designer. Generate a clean, 24x24 minimalist icon. "
|
|
@@ -28,8 +32,8 @@ def generate_vector_icon(prompt_text):
|
|
| 28 |
"Output raw SVG xml code ONLY."
|
| 29 |
)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
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:"
|
| 33 |
|
| 34 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
model.to(device)
|
|
@@ -44,21 +48,21 @@ def generate_vector_icon(prompt_text):
|
|
| 44 |
temperature=0.0
|
| 45 |
)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
generated_ids_trimmed = [
|
| 49 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 50 |
]
|
| 51 |
|
| 52 |
response = tokenizer.batch_decode(
|
| 53 |
generated_ids_trimmed, skip_special_tokens=True
|
| 54 |
-
)
|
| 55 |
|
| 56 |
-
# Strip residual
|
| 57 |
-
if
|
| 58 |
response = response.split("```xml")[1].split("```")[0].strip()
|
| 59 |
-
elif
|
| 60 |
response = response.split("```html")[1].split("```")[0].strip()
|
| 61 |
-
elif
|
| 62 |
response = response.split("```")[1].split("```")[0].strip()
|
| 63 |
|
| 64 |
output_filename = "generated_icon.svg"
|
|
@@ -67,7 +71,7 @@ def generate_vector_icon(prompt_text):
|
|
| 67 |
|
| 68 |
return output_filename
|
| 69 |
|
| 70 |
-
#
|
| 71 |
demo = gr.Interface(
|
| 72 |
fn=generate_vector_icon,
|
| 73 |
inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 6 |
|
| 7 |
MODEL_ID = "OmniSVG/OmniSVG1.1_8B"
|
| 8 |
+
print("Loading customized architecture layers...")
|
| 9 |
+
|
| 10 |
+
# Force the config to trick transformers into treating this as a causal text model
|
| 11 |
+
config = AutoConfig.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 12 |
+
config.architectures = ["Qwen2ForCausalLM"] # Bypasses the strict Qwen2_5_VL validation logic
|
| 13 |
|
|
|
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
MODEL_ID,
|
| 17 |
+
config=config,
|
| 18 |
+
dtype=torch.bfloat16, # Fixed the deprecated parameter warning
|
| 19 |
trust_remote_code=True
|
| 20 |
)
|
| 21 |
|
| 22 |
@spaces.GPU(duration=60)
|
| 23 |
def generate_vector_icon(prompt_text):
|
| 24 |
"""
|
| 25 |
+
Processes the minimalist text prompt through the OmniSVG tokenization path
|
| 26 |
+
to output valid SVG path coordinates.
|
| 27 |
"""
|
| 28 |
system_rules = (
|
| 29 |
"You are an expert UI/UX line icon designer. Generate a clean, 24x24 minimalist icon. "
|
|
|
|
| 32 |
"Output raw SVG xml code ONLY."
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Correct string template structure for text-driven SVG generators
|
| 36 |
+
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:"
|
| 37 |
|
| 38 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 39 |
model.to(device)
|
|
|
|
| 48 |
temperature=0.0
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# Strip prompt prefix array IDs
|
| 52 |
generated_ids_trimmed = [
|
| 53 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 54 |
]
|
| 55 |
|
| 56 |
response = tokenizer.batch_decode(
|
| 57 |
generated_ids_trimmed, skip_special_tokens=True
|
| 58 |
+
).strip()
|
| 59 |
|
| 60 |
+
# Strip residual codeblock fences if generated by the model
|
| 61 |
+
if "```xml" in response:
|
| 62 |
response = response.split("```xml")[1].split("```")[0].strip()
|
| 63 |
+
elif "```html" in response:
|
| 64 |
response = response.split("```html")[1].split("```")[0].strip()
|
| 65 |
+
elif "```" in response:
|
| 66 |
response = response.split("```")[1].split("```")[0].strip()
|
| 67 |
|
| 68 |
output_filename = "generated_icon.svg"
|
|
|
|
| 71 |
|
| 72 |
return output_filename
|
| 73 |
|
| 74 |
+
# Expose Gradio GUI
|
| 75 |
demo = gr.Interface(
|
| 76 |
fn=generate_vector_icon,
|
| 77 |
inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),
|