Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,17 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
-
#
|
| 6 |
-
from transformers import
|
| 7 |
|
| 8 |
MODEL_ID = "OmniSVG/OmniSVG1.1_8B"
|
| 9 |
print("Loading processor and vision-language model configuration...")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 13 |
-
model =
|
| 14 |
MODEL_ID,
|
| 15 |
-
dtype=torch.bfloat16,
|
| 16 |
trust_remote_code=True
|
| 17 |
)
|
| 18 |
|
|
@@ -31,7 +31,7 @@ def generate_vector_icon(prompt_text):
|
|
| 31 |
|
| 32 |
full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:"
|
| 33 |
|
| 34 |
-
# Standard format structure for Qwen VL
|
| 35 |
messages = [
|
| 36 |
{
|
| 37 |
"role": "user",
|
|
@@ -39,13 +39,13 @@ def generate_vector_icon(prompt_text):
|
|
| 39 |
}
|
| 40 |
]
|
| 41 |
|
| 42 |
-
#
|
| 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 |
-
#
|
| 49 |
inputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device)
|
| 50 |
|
| 51 |
with torch.no_grad():
|
|
@@ -56,16 +56,16 @@ def generate_vector_icon(prompt_text):
|
|
| 56 |
temperature=0.0
|
| 57 |
)
|
| 58 |
|
| 59 |
-
# Strip prompt prefix IDs to extract just the
|
| 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 |
-
)
|
| 67 |
|
| 68 |
-
#
|
| 69 |
output_filename = "generated_icon.svg"
|
| 70 |
with open(output_filename, "w", encoding="utf-8") as f:
|
| 71 |
f.write(response)
|
|
|
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
+
# Corrected imports for vision-language generation models
|
| 6 |
+
from transformers import AutoModelForConditionalGeneration, AutoProcessor
|
| 7 |
|
| 8 |
MODEL_ID = "OmniSVG/OmniSVG1.1_8B"
|
| 9 |
print("Loading processor and vision-language model configuration...")
|
| 10 |
|
| 11 |
+
# Initialize using the proper Auto classes
|
| 12 |
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 13 |
+
model = AutoModelForConditionalGeneration.from_pretrained(
|
| 14 |
MODEL_ID,
|
| 15 |
+
dtype=torch.bfloat16,
|
| 16 |
trust_remote_code=True
|
| 17 |
)
|
| 18 |
|
|
|
|
| 31 |
|
| 32 |
full_prompt = f"{system_rules}\n\nIcon Request: {prompt_text}\nSVG Output:"
|
| 33 |
|
| 34 |
+
# Standard messaging array format structure for Qwen VL backbones
|
| 35 |
messages = [
|
| 36 |
{
|
| 37 |
"role": "user",
|
|
|
|
| 39 |
}
|
| 40 |
]
|
| 41 |
|
| 42 |
+
# Render the input structure with chat 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 |
+
# Package input dictionaries onto the runtime device
|
| 49 |
inputs = processor(text=[text], images=None, padding=True, return_tensors="pt").to(device)
|
| 50 |
|
| 51 |
with torch.no_grad():
|
|
|
|
| 56 |
temperature=0.0
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Strip prompt prefix IDs to extract just the generated SVG text payload
|
| 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 the response block to an asset file so Gradio client can fetch it
|
| 69 |
output_filename = "generated_icon.svg"
|
| 70 |
with open(output_filename, "w", encoding="utf-8") as f:
|
| 71 |
f.write(response)
|