Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, VisionEncoderDecoderModel
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# 加载模型和处理器
|
7 |
+
model_id = "starvector/starvector-8b-im2svg"
|
8 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
9 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_id)
|
10 |
+
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model.to(device)
|
13 |
+
|
14 |
+
# 定义推理函数
|
15 |
+
def im2svg(image):
|
16 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
17 |
+
outputs = model.generate(**inputs, max_new_tokens=1024)
|
18 |
+
generated_svg = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
19 |
+
return generated_svg
|
20 |
+
|
21 |
+
# 创建 Gradio 界面
|
22 |
+
demo = gr.Interface(
|
23 |
+
fn=im2svg,
|
24 |
+
inputs=gr.Image(type="pil"),
|
25 |
+
outputs="text",
|
26 |
+
title="StarVector 8B - Image to SVG",
|
27 |
+
description="上传图像,将其转化为 SVG 矢量代码。",
|
28 |
+
allow_flagging="never"
|
29 |
+
)
|
30 |
+
|
31 |
+
demo.launch()
|