Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -341,7 +341,7 @@ def geometric_mean_given_words(target_word, context_words, co_occurrence_matrix,
|
|
| 341 |
|
| 342 |
def create_html_tables_for_tags(tag, result, tag2count, tag2idwiki):
|
| 343 |
# Wrap the tag part in a <span> with styles for bold and larger font
|
| 344 |
-
html_str = f"<div style='display: inline-block; margin:
|
| 345 |
# Loop through the results and add table rows for each
|
| 346 |
for word, sim in result:
|
| 347 |
word_with_underscores = word.replace(' ', '_')
|
|
@@ -361,6 +361,33 @@ def create_html_tables_for_tags(tag, result, tag2count, tag2idwiki):
|
|
| 361 |
|
| 362 |
html_str += "</tbody></table></div>"
|
| 363 |
return html_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
|
| 366 |
def find_similar_tags(test_tags, similarity_weight, allow_nsfw_tags):
|
|
@@ -380,7 +407,9 @@ def find_similar_tags(test_tags, similarity_weight, allow_nsfw_tags):
|
|
| 380 |
transformed_tags = [tag.replace(' ', '_') for tag in test_tags]
|
| 381 |
|
| 382 |
# Find similar tags and prepare data for tables
|
| 383 |
-
html_content = ""
|
|
|
|
|
|
|
| 384 |
for tag in test_tags:
|
| 385 |
if tag in special_tags:
|
| 386 |
continue
|
|
@@ -422,9 +451,10 @@ def find_similar_tags(test_tags, similarity_weight, allow_nsfw_tags):
|
|
| 422 |
|
| 423 |
result = sorted(result, key=lambda x: x[1], reverse=True)[:10]
|
| 424 |
html_content += create_html_tables_for_tags(tag, result, find_similar_tags.tag2count, find_similar_tags.tag2idwiki)
|
|
|
|
| 425 |
# If no tags were processed, add a message
|
| 426 |
-
if not
|
| 427 |
-
html_content = "
|
| 428 |
|
| 429 |
return html_content # Return list of lists for Dataframe
|
| 430 |
|
|
@@ -446,10 +476,11 @@ def find_similar_artists(new_tags_string, top_n, similarity_weight, allow_nsfw_t
|
|
| 446 |
X_new_image = vectorizer.transform([','.join(new_image_tags + removed_tags)])
|
| 447 |
similarities = cosine_similarity(X_new_image, X_artist)[0]
|
| 448 |
|
| 449 |
-
top_artist_indices = np.argsort(similarities)[-top_n:][::-1]
|
| 450 |
-
top_artists = [(artist_names[i], similarities[i]) for i in top_artist_indices]
|
| 451 |
-
|
| 452 |
-
top_artists_str = "\n".join([f"{rank+1}. {artist[3:]} ({score:.4f})" for rank, (artist, score) in enumerate(top_artists)])
|
|
|
|
| 453 |
dynamic_prompts_formatted_artists = "{" + "|".join([artist for artist, _ in top_artists]) + "}"
|
| 454 |
|
| 455 |
image_galleries = []
|
|
@@ -474,12 +505,11 @@ with gr.Blocks() as app:
|
|
| 474 |
num_artists = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Number of artists")
|
| 475 |
allow_nsfw = gr.Checkbox(label="Allow NSFW Tags", value=False)
|
| 476 |
with gr.Row():
|
| 477 |
-
with gr.Column():
|
| 478 |
-
top_artists = gr.
|
| 479 |
-
with gr.Column():
|
| 480 |
dynamic_prompts = gr.Textbox(label="Dynamic Prompts Format", info="For if you're using the Automatic1111 webui (https://github.com/AUTOMATIC1111/stable-diffusion-webui) with the Dynamic Prompts extension activated (https://github.com/adieyal/sd-dynamic-prompts) and want to try them all individually.")
|
| 481 |
-
|
| 482 |
-
|
| 483 |
galleries = []
|
| 484 |
for root, dirs, files in os.walk(sample_images_directory_path):
|
| 485 |
for name in dirs:
|
|
|
|
| 341 |
|
| 342 |
def create_html_tables_for_tags(tag, result, tag2count, tag2idwiki):
|
| 343 |
# Wrap the tag part in a <span> with styles for bold and larger font
|
| 344 |
+
html_str = f"<div style='display: inline-block; margin: 10px; vertical-align: top;'><table><thead><tr><th colspan='3' style='text-align: center; padding-bottom: 10px;'><span style='font-weight: bold; font-size: 20px;'>{tag}</span></th></tr></thead><tbody><tr style='border-bottom: 1px solid #000;'><th>Corrected Tag</th><th>Similarity</th><th>Count</th></tr>"
|
| 345 |
# Loop through the results and add table rows for each
|
| 346 |
for word, sim in result:
|
| 347 |
word_with_underscores = word.replace(' ', '_')
|
|
|
|
| 361 |
|
| 362 |
html_str += "</tbody></table></div>"
|
| 363 |
return html_str
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
def create_top_artists_table(top_artists):
|
| 367 |
+
# Add a heading above the table
|
| 368 |
+
html_str = "<div style='display: inline-block; margin: 20px; text-align: center;'>"
|
| 369 |
+
html_str += "<h1>Top Artists</h1>" # Heading for the table
|
| 370 |
+
# Start the table with increased font size and no borders between rows
|
| 371 |
+
html_str += "<table style='font-size: 20px; border-collapse: collapse;'>"
|
| 372 |
+
html_str += "<thead><tr><th>Artist</th><th>Similarity</th></tr></thead><tbody>"
|
| 373 |
+
# Loop through the top artists and add a row for each without the rank and without borders between rows
|
| 374 |
+
for artist, score in top_artists:
|
| 375 |
+
artist_name = artist[3:] if artist.startswith("by ") else artist # Remove "by " prefix
|
| 376 |
+
similarity_percentage = "{:.1f}%".format(score * 100) # Convert score to percentage string with one decimal
|
| 377 |
+
html_str += f"<td style='padding: 3px 20px; border: none;'>{artist_name}</td><td style='padding: 3px 20px; border: none;'>{similarity_percentage}</td></tr>"
|
| 378 |
+
|
| 379 |
+
# Close the table HTML
|
| 380 |
+
html_str += "</tbody></table></div>"
|
| 381 |
+
|
| 382 |
+
return html_str
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
def create_html_placeholder(title="", placeholder_height=400, placeholder_width="100%"):
|
| 386 |
+
# Include a title in the same style as the top artists table heading
|
| 387 |
+
html_placeholder = f"<div style='text-align: center;'><h1>{title}</h1></div>"
|
| 388 |
+
# Add the placeholder div with specified height and width
|
| 389 |
+
html_placeholder += f"<div style='height: {placeholder_height}px; width: {placeholder_width}; margin: 20px; background: transparent;'></div>"
|
| 390 |
+
return html_placeholder
|
| 391 |
|
| 392 |
|
| 393 |
def find_similar_tags(test_tags, similarity_weight, allow_nsfw_tags):
|
|
|
|
| 407 |
transformed_tags = [tag.replace(' ', '_') for tag in test_tags]
|
| 408 |
|
| 409 |
# Find similar tags and prepare data for tables
|
| 410 |
+
html_content = "<div style='display: inline-block; margin: 20px; text-align: center;'>"
|
| 411 |
+
html_content += "<h1>Unknown Tags</h1>" # Heading for the table
|
| 412 |
+
tags_added = False
|
| 413 |
for tag in test_tags:
|
| 414 |
if tag in special_tags:
|
| 415 |
continue
|
|
|
|
| 451 |
|
| 452 |
result = sorted(result, key=lambda x: x[1], reverse=True)[:10]
|
| 453 |
html_content += create_html_tables_for_tags(tag, result, find_similar_tags.tag2count, find_similar_tags.tag2idwiki)
|
| 454 |
+
tags_added=True
|
| 455 |
# If no tags were processed, add a message
|
| 456 |
+
if not tags_added:
|
| 457 |
+
html_content = create_html_placeholder(title="Unknown Tags")
|
| 458 |
|
| 459 |
return html_content # Return list of lists for Dataframe
|
| 460 |
|
|
|
|
| 476 |
X_new_image = vectorizer.transform([','.join(new_image_tags + removed_tags)])
|
| 477 |
similarities = cosine_similarity(X_new_image, X_artist)[0]
|
| 478 |
|
| 479 |
+
top_artist_indices = np.argsort(similarities)[-(top_n + 1):][::-1]
|
| 480 |
+
top_artists = [(artist_names[i], similarities[i]) for i in top_artist_indices if artist_names[i].lower() != "by conditional dnp"][:top_n]
|
| 481 |
+
|
| 482 |
+
#top_artists_str = "\n".join([f"{rank+1}. {artist[3:]} ({score:.4f})" for rank, (artist, score) in enumerate(top_artists)])
|
| 483 |
+
top_artists_str = create_top_artists_table(top_artists)
|
| 484 |
dynamic_prompts_formatted_artists = "{" + "|".join([artist for artist, _ in top_artists]) + "}"
|
| 485 |
|
| 486 |
image_galleries = []
|
|
|
|
| 505 |
num_artists = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Number of artists")
|
| 506 |
allow_nsfw = gr.Checkbox(label="Allow NSFW Tags", value=False)
|
| 507 |
with gr.Row():
|
| 508 |
+
with gr.Column(scale=1):
|
| 509 |
+
top_artists = gr.HTML(label="Top Artists", value=create_html_placeholder(title="Top Artists"))
|
|
|
|
| 510 |
dynamic_prompts = gr.Textbox(label="Dynamic Prompts Format", info="For if you're using the Automatic1111 webui (https://github.com/AUTOMATIC1111/stable-diffusion-webui) with the Dynamic Prompts extension activated (https://github.com/adieyal/sd-dynamic-prompts) and want to try them all individually.")
|
| 511 |
+
with gr.Column(scale=3):
|
| 512 |
+
unseen_tags = gr.HTML(label="Unknown Tags", value=create_html_placeholder(title="Unknown Tags"))
|
| 513 |
galleries = []
|
| 514 |
for root, dirs, files in os.walk(sample_images_directory_path):
|
| 515 |
for name in dirs:
|