Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import vtracer
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def convert_svg_to_ai(svg_path):
|
6 |
"""SVG๋ฅผ AI ํ์ผ๋ก ๋ณํ"""
|
@@ -14,7 +27,7 @@ def convert_svg_to_ai(svg_path):
|
|
14 |
ai_header = """%!PS-Adobe-3.0
|
15 |
%%Creator: Adobe Illustrator(TM) SVG Converter
|
16 |
%%AI8_CreatorVersion: 24.0.0
|
17 |
-
%%For:
|
18 |
%%Title: Generated AI File
|
19 |
%%CreationDate: %(date)s
|
20 |
%%BoundingBox: 0 0 800 600
|
@@ -34,10 +47,7 @@ def convert_svg_to_ai(svg_path):
|
|
34 |
|
35 |
# AI ํ์ผ ์์ฑ
|
36 |
with open(ai_path, 'w', encoding='utf-8') as ai_file:
|
37 |
-
# ํค๋ ์์ฑ
|
38 |
ai_file.write(ai_header)
|
39 |
-
|
40 |
-
# SVG ๋ด์ฉ์ AI ํ์ผ์ ํฌํจ
|
41 |
ai_file.write("\n%%BeginDocument\n")
|
42 |
ai_file.write("/SVGContent\n<<\n")
|
43 |
ai_file.write("/Type /SVG\n")
|
@@ -47,11 +57,12 @@ def convert_svg_to_ai(svg_path):
|
|
47 |
ai_file.write("\n]\n")
|
48 |
ai_file.write(">>\ndef\n")
|
49 |
ai_file.write("\n%%EndDocument\n")
|
50 |
-
|
51 |
-
# ํ์ผ ์ข
๋ฃ
|
52 |
ai_file.write("\n%%Trailer\n%%EOF\n")
|
53 |
|
54 |
-
|
|
|
|
|
|
|
55 |
|
56 |
def convert_to_vector(
|
57 |
image,
|
@@ -70,15 +81,16 @@ def convert_to_vector(
|
|
70 |
path_precision=3
|
71 |
):
|
72 |
if not (save_svg or save_ai):
|
73 |
-
return None, None, None #
|
74 |
-
|
75 |
if image is None:
|
76 |
-
return None, None, None
|
77 |
|
78 |
input_path = "temp_input.jpg"
|
79 |
svg_path = "svg_output.svg"
|
80 |
outputs = []
|
81 |
preview = None
|
|
|
82 |
|
83 |
try:
|
84 |
# ์
๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
@@ -110,17 +122,24 @@ def convert_to_vector(
|
|
110 |
|
111 |
# AI ํ์ผ ์ฒ๋ฆฌ
|
112 |
if save_ai:
|
113 |
-
ai_path = convert_svg_to_ai(svg_path)
|
114 |
outputs.append(ai_path)
|
|
|
|
|
115 |
|
116 |
if not save_svg: # SVG๊ฐ ์ ํ๋์ง ์์๋ค๋ฉด ์์ ํ์ผ ์ญ์
|
117 |
os.remove(svg_path)
|
118 |
|
119 |
-
return
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
except Exception as e:
|
122 |
print(f"Error during conversion: {str(e)}")
|
123 |
-
return None, None, None
|
124 |
|
125 |
finally:
|
126 |
# ์์ ํ์ผ ์ ๋ฆฌ
|
@@ -144,15 +163,34 @@ css = """
|
|
144 |
transform: translateY(-2px);
|
145 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
146 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
"""
|
148 |
|
149 |
# Gradio ์ธํฐํ์ด์ค ์ ์
|
150 |
with gr.Blocks(css=css) as app:
|
151 |
with gr.Column(elem_id="col-container"):
|
152 |
gr.HTML("""
|
153 |
-
<div
|
154 |
-
<
|
155 |
-
<p>
|
|
|
156 |
</div>
|
157 |
""")
|
158 |
with gr.Row():
|
@@ -181,9 +219,13 @@ with gr.Blocks(css=css) as app:
|
|
181 |
convert_button = gr.Button("โจ Convert", variant='primary', elem_classes=["generate-btn"])
|
182 |
|
183 |
with gr.Column():
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
|
|
187 |
|
188 |
examples = [
|
189 |
["examples/11.jpg", True, False],
|
@@ -199,7 +241,7 @@ with gr.Blocks(css=css) as app:
|
|
199 |
save_svg,
|
200 |
save_ai
|
201 |
],
|
202 |
-
outputs=[preview, svg_output, ai_output],
|
203 |
cache_examples=False,
|
204 |
run_on_click=True
|
205 |
)
|
@@ -209,18 +251,23 @@ with gr.Blocks(css=css) as app:
|
|
209 |
return [None] * 12
|
210 |
|
211 |
def update_output_visibility(save_svg, save_ai):
|
212 |
-
return
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
# ์ฒดํฌ๋ฐ์ค ์ํ์ ๋ฐ๋ฅธ ์ถ๋ ฅ ํ์ผ ์ปดํฌ๋ํธ ํ์/์จ๊น
|
215 |
save_svg.change(
|
216 |
update_output_visibility,
|
217 |
inputs=[save_svg, save_ai],
|
218 |
-
outputs=[svg_output, ai_output]
|
219 |
)
|
220 |
save_ai.change(
|
221 |
update_output_visibility,
|
222 |
inputs=[save_svg, save_ai],
|
223 |
-
outputs=[svg_output, ai_output]
|
224 |
)
|
225 |
|
226 |
# ๋ณํ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
@@ -242,7 +289,7 @@ with gr.Blocks(css=css) as app:
|
|
242 |
splice_threshold,
|
243 |
path_precision
|
244 |
],
|
245 |
-
outputs=[preview, svg_output, ai_output]
|
246 |
)
|
247 |
|
248 |
# Clear ๋ฒํผ ์ด๋ฒคํธ
|
|
|
1 |
import gradio as gr
|
2 |
import vtracer
|
3 |
import os
|
4 |
+
from svglib.svglib import svg2rlg
|
5 |
+
from reportlab.graphics import renderPDF
|
6 |
+
|
7 |
+
def create_pdf_preview(svg_path):
|
8 |
+
"""SVG๋ฅผ PDF๋ก ๋ณํํ์ฌ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ฉ PDF ์์ฑ"""
|
9 |
+
try:
|
10 |
+
drawing = svg2rlg(svg_path)
|
11 |
+
pdf_path = svg_path.replace('.svg', '_preview.pdf')
|
12 |
+
renderPDF.drawToFile(drawing, pdf_path)
|
13 |
+
return pdf_path
|
14 |
+
except Exception as e:
|
15 |
+
print(f"PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์์ฑ ์คํจ: {str(e)}")
|
16 |
+
return None
|
17 |
|
18 |
def convert_svg_to_ai(svg_path):
|
19 |
"""SVG๋ฅผ AI ํ์ผ๋ก ๋ณํ"""
|
|
|
27 |
ai_header = """%!PS-Adobe-3.0
|
28 |
%%Creator: Adobe Illustrator(TM) SVG Converter
|
29 |
%%AI8_CreatorVersion: 24.0.0
|
30 |
+
%%For: VectorFlow
|
31 |
%%Title: Generated AI File
|
32 |
%%CreationDate: %(date)s
|
33 |
%%BoundingBox: 0 0 800 600
|
|
|
47 |
|
48 |
# AI ํ์ผ ์์ฑ
|
49 |
with open(ai_path, 'w', encoding='utf-8') as ai_file:
|
|
|
50 |
ai_file.write(ai_header)
|
|
|
|
|
51 |
ai_file.write("\n%%BeginDocument\n")
|
52 |
ai_file.write("/SVGContent\n<<\n")
|
53 |
ai_file.write("/Type /SVG\n")
|
|
|
57 |
ai_file.write("\n]\n")
|
58 |
ai_file.write(">>\ndef\n")
|
59 |
ai_file.write("\n%%EndDocument\n")
|
|
|
|
|
60 |
ai_file.write("\n%%Trailer\n%%EOF\n")
|
61 |
|
62 |
+
# PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์์ฑ
|
63 |
+
pdf_preview = create_pdf_preview(svg_path)
|
64 |
+
|
65 |
+
return ai_path, pdf_preview
|
66 |
|
67 |
def convert_to_vector(
|
68 |
image,
|
|
|
81 |
path_precision=3
|
82 |
):
|
83 |
if not (save_svg or save_ai):
|
84 |
+
return None, None, None, None # Preview, SVG output, AI output, AI preview
|
85 |
+
|
86 |
if image is None:
|
87 |
+
return None, None, None, None
|
88 |
|
89 |
input_path = "temp_input.jpg"
|
90 |
svg_path = "svg_output.svg"
|
91 |
outputs = []
|
92 |
preview = None
|
93 |
+
ai_preview = None
|
94 |
|
95 |
try:
|
96 |
# ์
๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
|
|
122 |
|
123 |
# AI ํ์ผ ์ฒ๋ฆฌ
|
124 |
if save_ai:
|
125 |
+
ai_path, pdf_preview = convert_svg_to_ai(svg_path)
|
126 |
outputs.append(ai_path)
|
127 |
+
if pdf_preview:
|
128 |
+
ai_preview = pdf_preview
|
129 |
|
130 |
if not save_svg: # SVG๊ฐ ์ ํ๋์ง ์์๋ค๋ฉด ์์ ํ์ผ ์ญ์
|
131 |
os.remove(svg_path)
|
132 |
|
133 |
+
return (
|
134 |
+
preview,
|
135 |
+
outputs[0] if outputs else None,
|
136 |
+
outputs[1] if len(outputs) > 1 else None,
|
137 |
+
ai_preview
|
138 |
+
)
|
139 |
|
140 |
except Exception as e:
|
141 |
print(f"Error during conversion: {str(e)}")
|
142 |
+
return None, None, None, None
|
143 |
|
144 |
finally:
|
145 |
# ์์ ํ์ผ ์ ๋ฆฌ
|
|
|
163 |
transform: translateY(-2px);
|
164 |
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
165 |
}
|
166 |
+
.container {
|
167 |
+
max-width: 1200px;
|
168 |
+
margin: 0 auto;
|
169 |
+
padding: 20px;
|
170 |
+
}
|
171 |
+
.header {
|
172 |
+
text-align: center;
|
173 |
+
margin-bottom: 40px;
|
174 |
+
}
|
175 |
+
.header h1 {
|
176 |
+
font-size: 2.5em;
|
177 |
+
color: #2c3e50;
|
178 |
+
margin-bottom: 10px;
|
179 |
+
}
|
180 |
+
.header p {
|
181 |
+
color: #7f8c8d;
|
182 |
+
font-size: 1.2em;
|
183 |
+
}
|
184 |
"""
|
185 |
|
186 |
# Gradio ์ธํฐํ์ด์ค ์ ์
|
187 |
with gr.Blocks(css=css) as app:
|
188 |
with gr.Column(elem_id="col-container"):
|
189 |
gr.HTML("""
|
190 |
+
<div class="header">
|
191 |
+
<h1>VectorFlow โก</h1>
|
192 |
+
<p>Transform your images into professional vector graphics</p>
|
193 |
+
<p style="font-size: 0.9em; color: #95a5a6;">Supports JPG, PNG, WEBP โ SVG, AI</p>
|
194 |
</div>
|
195 |
""")
|
196 |
with gr.Row():
|
|
|
219 |
convert_button = gr.Button("โจ Convert", variant='primary', elem_classes=["generate-btn"])
|
220 |
|
221 |
with gr.Column():
|
222 |
+
with gr.Tabs():
|
223 |
+
with gr.Tab("SVG Preview"):
|
224 |
+
preview = gr.HTML(label="SVG Preview")
|
225 |
+
svg_output = gr.File(label="Download SVG", visible=True)
|
226 |
+
with gr.Tab("AI Preview"):
|
227 |
+
ai_preview = gr.PDF(label="AI Preview")
|
228 |
+
ai_output = gr.File(label="Download AI", visible=True)
|
229 |
|
230 |
examples = [
|
231 |
["examples/11.jpg", True, False],
|
|
|
241 |
save_svg,
|
242 |
save_ai
|
243 |
],
|
244 |
+
outputs=[preview, svg_output, ai_output, ai_preview],
|
245 |
cache_examples=False,
|
246 |
run_on_click=True
|
247 |
)
|
|
|
251 |
return [None] * 12
|
252 |
|
253 |
def update_output_visibility(save_svg, save_ai):
|
254 |
+
return (
|
255 |
+
gr.update(visible=save_svg),
|
256 |
+
gr.update(visible=save_ai),
|
257 |
+
gr.update(visible=save_svg),
|
258 |
+
gr.update(visible=save_ai)
|
259 |
+
)
|
260 |
|
261 |
# ์ฒดํฌ๋ฐ์ค ์ํ์ ๋ฐ๋ฅธ ์ถ๋ ฅ ํ์ผ ์ปดํฌ๋ํธ ํ์/์จ๊น
|
262 |
save_svg.change(
|
263 |
update_output_visibility,
|
264 |
inputs=[save_svg, save_ai],
|
265 |
+
outputs=[preview, ai_preview, svg_output, ai_output]
|
266 |
)
|
267 |
save_ai.change(
|
268 |
update_output_visibility,
|
269 |
inputs=[save_svg, save_ai],
|
270 |
+
outputs=[preview, ai_preview, svg_output, ai_output]
|
271 |
)
|
272 |
|
273 |
# ๋ณํ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
|
|
289 |
splice_threshold,
|
290 |
path_precision
|
291 |
],
|
292 |
+
outputs=[preview, svg_output, ai_output, ai_preview]
|
293 |
)
|
294 |
|
295 |
# Clear ๋ฒํผ ์ด๋ฒคํธ
|