MathObsession commited on
Commit
a3c0cb4
·
verified ·
1 Parent(s): 48353a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -29
app.py CHANGED
@@ -2,25 +2,24 @@ import os
2
  import torch
3
  import gradio as gr
4
  import spaces
5
- # Use the explicit model class for Qwen2.5-VL architectures
6
- from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
7
 
8
  MODEL_ID = "OmniSVG/OmniSVG1.1_8B"
9
- print("Loading processor and Qwen2.5-VL vision-language model architecture...")
10
 
11
- # Initialize the processor and the model cleanly
12
- processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
13
- model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
14
  MODEL_ID,
15
- dtype=torch.bfloat16,
16
  trust_remote_code=True
17
  )
18
 
19
  @spaces.GPU(duration=60)
20
  def generate_vector_icon(prompt_text):
21
  """
22
- Processes the minimalist text prompt through the Qwen2.5-VL backbone
23
- to output valid SVG path coordinates.
24
  """
25
  system_rules = (
26
  "You are an expert UI/UX line icon designer. Generate a clean, 24x24 minimalist icon. "
@@ -29,24 +28,13 @@ def generate_vector_icon(prompt_text):
29
  "Output raw SVG xml code ONLY."
30
  )
31
 
32
- full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:"
33
-
34
- # Structure text formatting using Qwen-VL chat message guidelines
35
- messages = [
36
- {
37
- "role": "user",
38
- "content": [{"type": "text", "text": full_prompt}]
39
- }
40
- ]
41
-
42
- # Process text layout templates
43
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
44
 
45
  device = "cuda" if torch.cuda.is_available() else "cpu"
46
  model.to(device)
47
 
48
- # Parse through the multi-modal text pipeline
49
- inputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device)
50
 
51
  with torch.no_grad():
52
  generated_ids = model.generate(
@@ -56,23 +44,30 @@ def generate_vector_icon(prompt_text):
56
  temperature=0.0
57
  )
58
 
59
- # Strip layout prefix arrays
60
  generated_ids_trimmed = [
61
  out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
62
  ]
63
 
64
- response = processor.batch_decode(
65
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
66
- ).strip()
67
 
68
- # Write structural stream data to file assets
 
 
 
 
 
 
 
69
  output_filename = "generated_icon.svg"
70
  with open(output_filename, "w", encoding="utf-8") as f:
71
  f.write(response)
72
 
73
  return output_filename
74
 
75
- # Expose API endpoint layers
76
  demo = gr.Interface(
77
  fn=generate_vector_icon,
78
  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 AutoModelForCausalLM, AutoTokenizer
 
6
 
7
  MODEL_ID = "OmniSVG/OmniSVG1.1_8B"
8
+ print("Loading tokenizer and model layers...")
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
+ torch_dtype=torch.bfloat16,
15
  trust_remote_code=True
16
  )
17
 
18
  @spaces.GPU(duration=60)
19
  def generate_vector_icon(prompt_text):
20
  """
21
+ Processes text inputs through the OmniSVG tokenization path
22
+ to generate raw mathematical code blocks.
23
  """
24
  system_rules = (
25
  "You are an expert UI/UX line icon designer. Generate a clean, 24x24 minimalist icon. "
 
28
  "Output raw SVG xml code ONLY."
29
  )
30
 
31
+ # Standard prompt injection format for text-space SVG backends
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)
36
 
37
+ inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
 
38
 
39
  with torch.no_grad():
40
  generated_ids = model.generate(
 
44
  temperature=0.0
45
  )
46
 
47
+ # Exclude prompt vectors from final token generation stream
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
+ )[0].strip()
55
 
56
+ # Strip residual markdown ticks if the model returns them
57
+ if response.startswith("```xml"):
58
+ response = response.split("```xml")[1].split("```")[0].strip()
59
+ elif response.startswith("```html"):
60
+ response = response.split("```html")[1].split("```")[0].strip()
61
+ elif response.startswith("```"):
62
+ response = response.split("```")[1].split("```")[0].strip()
63
+
64
  output_filename = "generated_icon.svg"
65
  with open(output_filename, "w", encoding="utf-8") as f:
66
  f.write(response)
67
 
68
  return output_filename
69
 
70
+ # Mount Gradio framework
71
  demo = gr.Interface(
72
  fn=generate_vector_icon,
73
  inputs=gr.Textbox(label="Icon Prompt Description", placeholder="e.g., download arrow tray"),