File size: 5,925 Bytes
f799edd c83bfa7 a19af11 c83bfa7 a19af11 c83bfa7 f799edd c83bfa7 a19af11 c83bfa7 a19af11 6c2404b a19af11 c83bfa7 6c2404b a19af11 f799edd c83bfa7 f799edd 6c2404b f799edd 6c2404b a19af11 f799edd 6c2404b a19af11 6c2404b a19af11 6c2404b f799edd 6c2404b a19af11 6c2404b f799edd c83bfa7 6c2404b a19af11 6c2404b f799edd 6c2404b a19af11 6c2404b a19af11 6c2404b a19af11 6c2404b c83bfa7 6c2404b c83bfa7 f799edd c83bfa7 f799edd a19af11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import gradio as gr
from datasets import load_dataset
from rapidfuzz import process, fuzz
# ──────────────────────────────────────────────────────────
# 1) Load dataset (streaming) ─ only metadata kept in memory
# ──────────────────────────────────────────────────────────
ds = load_dataset(
"nyuuzyou/clker-svg",
split="train",
streaming=True, # .jsonl.zst → streamed automatically
)
records = []
for ex in ds:
records.append(
{
"id": ex["id"],
"title": ex["title"] or "",
"tags": " ".join(ex["tags"] or []),
"svg": ex["svg_content"],
"url": ex["download_url"],
}
)
# ──────────────────────────────────────────────────────────
# 2) Search function
# ──────────────────────────────────────────────────────────
def search_svg(query: str, top_k: int):
if not query.strip():
return "⚠️ Please enter a search term.", None
# choices: index(int) ➜ single-line title+tags string
choices = {i: f"{r['title']} {r['tags']}" for i, r in enumerate(records)}
# Rapidfuzz: returns (choice_text, score, key)
matched = process.extract(
query,
choices,
scorer=fuzz.WRatio,
limit=int(top_k),
)
html_snippets = []
html_start = '<div class="gallery-grid">'
html_end = '</div>'
for _, score, idx in matched: # idx is actual list index
r = records[idx]
svg_html = (
'<div class="gallery-item">'
f'<div class="svg-container">{r["svg"]}</div>'
f'<div class="item-details">'
f'<h3>{r["title"]}</h3>'
f'<div class="score">Match score: {score}</div>'
f'<div class="tags">{r["tags"]}</div>'
f'<a href="{r["url"]}" target="_blank" class="download-link">Download original</a>'
f'</div>'
'</div>'
)
html_snippets.append(svg_html)
if not html_snippets:
return "No results found.", None
return "", html_start + ''.join(html_snippets) + html_end
# ──────────────────────────────────────────────────────────
# 3) Gradio UI
# ──────────────────────────────────────────────────────────
TITLE = "🔍 Clker SVG"
DESCRIPTION = """
This application lets you quickly search public-domain SVG clip art using the “nyuuzyou/clker-svg” dataset.
It finds similar items in titles and tags through fuzzy matching and shows them in a visual gallery.
"""
DISCORD_BADGE = """<p style="text-align:center; margin-top: -10px;"><a href="https://discord.gg/openfreeai" target="_blank"> <img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%230000ff&labelColor=%23800080&logo=discord&logoColor=white&style=for-the-badge" alt="badge"></a></p>"""
CSS = """
.gallery-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
margin-top: 20px;
}
.gallery-item {
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
transition: transform 0.3s;
background: white;
}
.gallery-item:hover {
transform: translateY(-5px);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.svg-container {
height: 180px;
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
background: #f9f9f9;
}
.svg-container svg {
max-width: 100%;
max-height: 160px;
}
.item-details {
padding: 15px;
}
.item-details h3 {
margin: 0 0 10px 0;
font-size: 16px;
color: #333;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.score {
font-size: 14px;
color: #666;
margin-bottom: 5px;
}
.tags {
font-size: 12px;
color: #888;
margin-bottom: 10px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.download-link {
display: inline-block;
padding: 5px 10px;
background: #4a90e2;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 12px;
}
.download-link:hover {
background: #3a7bc8;
}
"""
with gr.Blocks(title=TITLE, css=CSS) as demo:
gr.Markdown(f"# {TITLE}")
gr.Markdown(DESCRIPTION)
gr.HTML(DISCORD_BADGE)
with gr.Row():
with gr.Column(scale=4):
query_box = gr.Textbox(
label="Search term",
placeholder="e.g. cat, tree, house, computer, flower...",
show_label=True
)
with gr.Column(scale=1):
top_slider = gr.Slider(
minimum=1,
maximum=50,
value=12,
step=3,
label="Number of results"
)
with gr.Row():
search_button = gr.Button("Search", variant="primary")
warning_md = gr.Markdown()
output_html = gr.HTML()
search_button.click(
fn=search_svg,
inputs=[query_box, top_slider],
outputs=[warning_md, output_html],
)
query_box.submit(
fn=search_svg,
inputs=[query_box, top_slider],
outputs=[warning_md, output_html],
)
if __name__ == "__main__":
demo.launch()
|