FoodDesert commited on
Commit
611defa
1 Parent(s): 632e91e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -25
app.py CHANGED
@@ -15,17 +15,28 @@ from lark import Token
15
 
16
 
17
  faq_content="""
18
- # Frequently Asked Questions (FAQs)
19
 
20
- Technically I am writing this before anyone but me has used the tool, so no one has asked questions yet. But if they did, here are the questions I think they might ask:
 
 
 
 
 
21
 
22
  ## Does input order matter?
23
 
24
  No
25
 
26
- ## Should I use underscores in the input tags?
 
 
27
 
28
- It doesn't matter. The application handles tags either way.
 
 
 
 
29
 
30
  ## Why are some valid tags marked as "unseen", and why don't some artists ever get returned?
31
 
@@ -36,15 +47,18 @@ If an artist or tag is too infrequent, we might not think we have enough data to
36
 
37
  Yes. We normalized the favorite counts of each image to a range of 0-9, with 0 being the lowest favcount, and 9 being the highest.
38
  You can include any of these special tags: "score:0", "score:1", "score:2", "score:3", "score:4", "score:5", "score:6", "score:7", "score:8", "score:9"
39
- in your list to bias the output toward artists with higher or lower scoring images.
40
 
41
  ## Are there any other special tricks?
42
 
43
  Yes. If you want to more strongly bias the artist output toward a specific tag, you can just list it multiple times.
44
  So for example, the query "red fox, red fox, red fox, score:7" will yield a list of artists who are more strongly associated with the tag "red fox"
45
- than the query "red fox, score:7".
 
 
 
46
 
47
- ## What calculation is this thing actually performing?
48
 
49
  Each artist is represented by a "pseudo-document" composed of all the tags from their uploaded images, treating these tags similarly to words in a text document.
50
  Similarly, when you input a set of tags, the system creates a pseudo-document for your query out of all the tags.
@@ -52,6 +66,15 @@ It then uses a technique called cosine similarity to compare your tags against e
52
  This method helps identify artists whose work is closely aligned with the themes or elements you're interested in.
53
  For those curious about the underlying mechanics of comparing text-like data, we employ the TF-IDF (Term Frequency-Inverse Document Frequency) method, a standard approach in information retrieval.
54
  You can read more about TF-IDF on its [Wikipedia page](https://en.wikipedia.org/wiki/Tf%E2%80%93idf).
 
 
 
 
 
 
 
 
 
55
  """
56
 
57
 
@@ -169,25 +192,29 @@ def find_similar_tags(test_tags):
169
  return results_data # Return list of lists for Dataframe
170
 
171
  def find_similar_artists(new_tags_string, top_n):
172
- # Parse the prompt
173
- parsed = parser.parse(new_tags_string)
174
- # Extract tags from the parsed tree
175
- new_image_tags = extract_tags(parsed)
176
- new_image_tags = [tag.replace('_', ' ').strip() for tag in new_image_tags]
177
-
178
- ###unseen_tags = list(set(OrderedDict.fromkeys(new_image_tags)) - set(vectorizer.vocabulary_.keys()))
179
- unseen_tags_data = find_similar_tags(new_image_tags)
 
 
180
 
181
- X_new_image = vectorizer.transform([','.join(new_image_tags)])
182
- similarities = cosine_similarity(X_new_image, X_artist)[0]
183
-
184
- top_artist_indices = np.argsort(similarities)[-top_n:][::-1]
185
- top_artists = [(artist_names[i], similarities[i]) for i in top_artist_indices]
186
-
187
- top_artists_str = "\n".join([f"{rank+1}. {artist[3:]} ({score:.4f})" for rank, (artist, score) in enumerate(top_artists)])
188
- dynamic_prompts_formatted_artists = "{" + "|".join([artist for artist, _ in top_artists]) + "}"
189
-
190
- return unseen_tags_data, top_artists_str, dynamic_prompts_formatted_artists
 
 
191
 
192
 
193
  iface = gr.Interface(
 
15
 
16
 
17
  faq_content="""
18
+ # Questions:
19
 
20
+ ## What is the purpose of this tool?
21
+
22
+ When you enter a txt2img prompt prompt and press the "submit" button, the Tagset Completer parses your prompt and checks that all your tags are valid e621 tags.
23
+ If it finds any that are not, it recommends some valid e621 tags you can use to replace them in the "Unseen Tags" table.
24
+ Additionally, in the "Top Artists" text box, it lists the artists who would most likely draw an image having the set of tags you provided,
25
+ in case you want to look them up to get more ideas.
26
 
27
  ## Does input order matter?
28
 
29
  No
30
 
31
+ ## Should I use underscores or spaces in the input tags?
32
+
33
+ Spaces are preferred, but it will still work if you use underscores. The Unseen Tags table will just complain at you.
34
 
35
+ ## Can I use parentheses or weights as in the Stable Diffusion Automatic1111 WebUI?
36
+
37
+ Yes, but only '(' and ')' and numerical weights, and all of these things are ignored in all calculations. The main benefit of this is that you can copy/paste prompts from one program to another with minimal editing.
38
+ An example that illustrates acceptable parentheses and weight formatting is:
39
+ ((sunset over the mountains)), (clear sky:1.5), ((eagle flying high:2.0)), river, (fish swimming in the river:1.2), (campfire, (marshmallows:2.1):1.3), stars in the sky, ((full moon:1.8)), (wolf howling:1.7)
40
 
41
  ## Why are some valid tags marked as "unseen", and why don't some artists ever get returned?
42
 
 
47
 
48
  Yes. We normalized the favorite counts of each image to a range of 0-9, with 0 being the lowest favcount, and 9 being the highest.
49
  You can include any of these special tags: "score:0", "score:1", "score:2", "score:3", "score:4", "score:5", "score:6", "score:7", "score:8", "score:9"
50
+ in your list to bias the output toward artists with higher or lower scoring images. Since they are not real tags, the Unseen Tags section will complain, but you can ignore that.
51
 
52
  ## Are there any other special tricks?
53
 
54
  Yes. If you want to more strongly bias the artist output toward a specific tag, you can just list it multiple times.
55
  So for example, the query "red fox, red fox, red fox, score:7" will yield a list of artists who are more strongly associated with the tag "red fox"
56
+ than the query "red fox, score:7".
57
+
58
+ ## Why is this space tagged "not-for-all-audience"
59
+ The "not-for-all-audience" tag informs users that this tool's text output is derived from e621.net data for tag prediction and completion. This measure underscores a commitment to responsible content sharing.
60
 
61
+ ## How is the artist list calculated?
62
 
63
  Each artist is represented by a "pseudo-document" composed of all the tags from their uploaded images, treating these tags similarly to words in a text document.
64
  Similarly, when you input a set of tags, the system creates a pseudo-document for your query out of all the tags.
 
66
  This method helps identify artists whose work is closely aligned with the themes or elements you're interested in.
67
  For those curious about the underlying mechanics of comparing text-like data, we employ the TF-IDF (Term Frequency-Inverse Document Frequency) method, a standard approach in information retrieval.
68
  You can read more about TF-IDF on its [Wikipedia page](https://en.wikipedia.org/wiki/Tf%E2%80%93idf).
69
+
70
+ ## How does the tag corrector work?
71
+
72
+ We collected the tag sets from over 4 million e621 posts, treating the tag set from each image as an individual document.
73
+ We then randomly replace about 10% of the tags in each document with a randomly selected alias from e621's list of aliases for the tag
74
+ (e.g. "canine" gets replaced with one of {k9,canines,mongrel,cannine,cnaine,feral_canine,anthro_canine}).
75
+ We then train a FastText (https://fasttext.cc/) model on the documents. The result of this training is a function that maps arbitrary words to vectors such that
76
+ the vector for a tag and the vectors for its aliases are all close together (because the model has seen them in similar contexts).
77
+ Since the lists of aliases contain misspellings and rephrasings of tags, the model should be robust to these kinds of problems.
78
  """
79
 
80
 
 
192
  return results_data # Return list of lists for Dataframe
193
 
194
  def find_similar_artists(new_tags_string, top_n):
195
+ try:
196
+ new_tags_string = new_tags_string.lower()
197
+ # Parse the prompt
198
+ parsed = parser.parse(new_tags_string)
199
+ # Extract tags from the parsed tree
200
+ new_image_tags = extract_tags(parsed)
201
+ new_image_tags = [tag.replace('_', ' ').strip() for tag in new_image_tags]
202
+
203
+ ###unseen_tags = list(set(OrderedDict.fromkeys(new_image_tags)) - set(vectorizer.vocabulary_.keys())) #We may want this line again later. These are the tags that were not used to calculate the artists list.
204
+ unseen_tags_data = find_similar_tags(new_image_tags)
205
 
206
+ X_new_image = vectorizer.transform([','.join(new_image_tags)])
207
+ similarities = cosine_similarity(X_new_image, X_artist)[0]
208
+
209
+ top_artist_indices = np.argsort(similarities)[-top_n:][::-1]
210
+ top_artists = [(artist_names[i], similarities[i]) for i in top_artist_indices]
211
+
212
+ top_artists_str = "\n".join([f"{rank+1}. {artist[3:]} ({score:.4f})" for rank, (artist, score) in enumerate(top_artists)])
213
+ dynamic_prompts_formatted_artists = "{" + "|".join([artist for artist, _ in top_artists]) + "}"
214
+
215
+ return unseen_tags_data, top_artists_str, dynamic_prompts_formatted_artists
216
+ except ParseError as e:
217
+ return [], "Parse Error: Check for mismatched parentheses or something", ""
218
 
219
 
220
  iface = gr.Interface(