Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from rapidfuzz import process, fuzz
|
| 4 |
+
|
| 5 |
+
# 1) 데이터셋 스트리밍 로드(메타만 메모리에 보관)
|
| 6 |
+
ds = load_dataset("nyuuzyou/clker-svg", split="train", streaming=True)
|
| 7 |
+
|
| 8 |
+
records = []
|
| 9 |
+
for ex in ds:
|
| 10 |
+
records.append(
|
| 11 |
+
{
|
| 12 |
+
"id": ex["id"],
|
| 13 |
+
"title": ex["title"] or "",
|
| 14 |
+
"tags": " ".join(ex["tags"] or []),
|
| 15 |
+
"svg": ex["svg_content"],
|
| 16 |
+
"url": ex["download_url"],
|
| 17 |
+
}
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# 2) 검색 함수
|
| 21 |
+
def search_svg(query, top_k):
|
| 22 |
+
if not query:
|
| 23 |
+
return "⚠️ 검색어를 입력하세요.", None
|
| 24 |
+
|
| 25 |
+
# title+tags 스트링 하나로 결합
|
| 26 |
+
choices = {i: r["title"] + " " + r["tags"] for i, r in enumerate(records)}
|
| 27 |
+
matched = process.extract(
|
| 28 |
+
query,
|
| 29 |
+
choices,
|
| 30 |
+
scorer=fuzz.WRatio,
|
| 31 |
+
limit=top_k,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Rapidfuzz 결과 → SVG HTML 변환
|
| 35 |
+
html_snippets = []
|
| 36 |
+
for idx, score, _ in matched:
|
| 37 |
+
r = records[idx]
|
| 38 |
+
svg_html = (
|
| 39 |
+
f"<div style='border:1px solid #ddd;margin:8px;padding:8px'>"
|
| 40 |
+
f"<strong>{r['title']}</strong> "
|
| 41 |
+
f"(score {score})<br>"
|
| 42 |
+
f"<em>{r['tags']}</em><br>"
|
| 43 |
+
f"<a href='{r['url']}' target='_blank'>원본 다운로드</a><br>"
|
| 44 |
+
f"{r['svg']}" # raw SVG → 브라우저가 바로 렌더
|
| 45 |
+
f"</div>"
|
| 46 |
+
)
|
| 47 |
+
html_snippets.append(svg_html)
|
| 48 |
+
|
| 49 |
+
return "", "\n".join(html_snippets)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(title="Clker SVG 검색") as demo:
|
| 53 |
+
gr.Markdown("## 🔍 Clker.com SVG Public-Domain Clipart 검색")
|
| 54 |
+
with gr.Row():
|
| 55 |
+
query = gr.Textbox(label="검색어", placeholder="예: cat, tree, …")
|
| 56 |
+
top_k = gr.Slider(1, 50, value=10, step=1, label="결과 개수")
|
| 57 |
+
warn = gr.Markdown()
|
| 58 |
+
out = gr.HTML()
|
| 59 |
+
|
| 60 |
+
query.submit(
|
| 61 |
+
fn=search_svg,
|
| 62 |
+
inputs=[query, top_k],
|
| 63 |
+
outputs=[warn, out],
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|