furusu commited on
Commit
1f99e57
1 Parent(s): fdeeb58

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import json
3
+ import gradio as gr
4
+
5
+ with open("num_to_token.json", "r") as f:
6
+ num_to_token = json.load(f)
7
+ token_to_num = {v:k for k,v in num_to_token.items()}
8
+ token_embeddings = torch.load("token_embeddings.pt")
9
+
10
+ tags = sorted(list(num_to_token.values()))
11
+
12
+ def predict(target_tag, sort_by="descend"):
13
+ if sort_by == "descending":
14
+ multiplier = 1
15
+ else:
16
+ multiplier = -1
17
+ target_embedding = token_embeddings[int(token_to_num[target_tag])].unsqueeze(0)
18
+ sims = torch.cosine_similarity(target_embedding, token_embeddings, dim=1)
19
+ results = {num_to_token[str(i)]:sims[i].item() * multiplier for i in range(len(num_to_token))}
20
+
21
+ return results
22
+
23
+ demo = gr.Interface(
24
+ fn=predict,
25
+ inputs=[
26
+ gr.Dropdown(choices=tags, label="Target tag", value="otoko no ko"),
27
+ gr.Dropdown(choices=["ascending", "descending"], label="Sort by", value="descending")
28
+ ],
29
+ outputs=gr.Label(num_top_classes=50),
30
+ )
31
+
32
+ demo.launch()