Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -36,6 +36,8 @@ def tag_image(pil_image: Image.Image, output_format: str) -> str:
|
|
36 |
results_by_cat = {} # to store tags per category (for verbose output)
|
37 |
prompt_tags = [] # to store tags for prompt-style output
|
38 |
# Collect tags above thresholds
|
|
|
|
|
39 |
for idx, prob in enumerate(probs):
|
40 |
tag = idx_to_tag[str(idx)]
|
41 |
cat = tag_to_category.get(tag, "unknown")
|
@@ -43,14 +45,21 @@ def tag_image(pil_image: Image.Image, output_format: str) -> str:
|
|
43 |
if float(prob) >= thresh:
|
44 |
# add to category dictionary
|
45 |
results_by_cat.setdefault(cat, []).append((tag, float(prob)))
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
48 |
if output_format == "Prompt-style Tags":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if not prompt_tags:
|
50 |
return "No tags predicted."
|
51 |
-
# Join tags with commas (sorted by probability for relevance)
|
52 |
-
# Sort prompt_tags by probability from results_by_cat (for better prompts ordering)
|
53 |
-
prompt_tags.sort(key=lambda t: max([p for (tg, p) in results_by_cat[tag_to_category.get(t.replace(' ', '_'), 'unknown')] if tg == t.replace(' ', '_')]), reverse=True)
|
54 |
return ", ".join(prompt_tags)
|
55 |
else: # Detailed output
|
56 |
if not results_by_cat:
|
@@ -87,7 +96,7 @@ with demo:
|
|
87 |
# Link the button click to the function
|
88 |
tag_button.click(fn=tag_image, inputs=[image_in, format_choice], outputs=output_box)
|
89 |
# Footer/Info
|
90 |
-
gr.Markdown("----\n**Model:** [Camie Tagger ONNX](https://huggingface.co/AngelBottomless/camie-tagger-onnxruntime)
|
91 |
|
92 |
# Launch the app (automatically handled in Spaces)
|
93 |
-
demo.launch()
|
|
|
36 |
results_by_cat = {} # to store tags per category (for verbose output)
|
37 |
prompt_tags = [] # to store tags for prompt-style output
|
38 |
# Collect tags above thresholds
|
39 |
+
artist_tags_with_probs = []
|
40 |
+
non_artist_tags_with_probs = []
|
41 |
for idx, prob in enumerate(probs):
|
42 |
tag = idx_to_tag[str(idx)]
|
43 |
cat = tag_to_category.get(tag, "unknown")
|
|
|
45 |
if float(prob) >= thresh:
|
46 |
# add to category dictionary
|
47 |
results_by_cat.setdefault(cat, []).append((tag, float(prob)))
|
48 |
+
if cat == 'artist':
|
49 |
+
artist_tags_with_probs.append((tag, float(prob)))
|
50 |
+
else:
|
51 |
+
non_artist_tags_with_probs.append((tag, float(prob)))
|
52 |
+
|
53 |
if output_format == "Prompt-style Tags":
|
54 |
+
artist_tags_with_probs.sort(key=lambda x: x[1], reverse=True)
|
55 |
+
non_artist_tags_with_probs.sort(key=lambda x: x[1], reverse=True)
|
56 |
+
|
57 |
+
artist_prompt_tags = [tag.replace("_", " ") for tag, prob in artist_tags_with_probs]
|
58 |
+
non_artist_prompt_tags = [tag.replace("_", " ") for tag, prob in non_artist_tags_with_probs]
|
59 |
+
prompt_tags = artist_prompt_tags + non_artist_prompt_tags
|
60 |
+
|
61 |
if not prompt_tags:
|
62 |
return "No tags predicted."
|
|
|
|
|
|
|
63 |
return ", ".join(prompt_tags)
|
64 |
else: # Detailed output
|
65 |
if not results_by_cat:
|
|
|
96 |
# Link the button click to the function
|
97 |
tag_button.click(fn=tag_image, inputs=[image_in, format_choice], outputs=output_box)
|
98 |
# Footer/Info
|
99 |
+
gr.Markdown("----\n**Model:** [Camie Tagger ONNX](https://huggingface.co/AngelBottomless/camie-tagger-onnxruntime) • **Base Model:** Camais03/camie-tagger (61% F1 on 70k tags) • **ONNX Runtime:** for efficient CPU inference:contentReference[oaicite:6]{index=6} • *Demo built with Gradio Blocks.*")
|
100 |
|
101 |
# Launch the app (automatically handled in Spaces)
|
102 |
+
demo.launch()
|