Spaces:
Paused
Paused
File size: 1,121 Bytes
7fb6157 8c7e0b8 7fb6157 8c7e0b8 7fb6157 a989a97 7fb6157 |
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 |
import json
import numpy as np
import httpx
import os
from constants import MUBERT_TAGS, MUBERT_MODE, MUBERT_LICENSE
def get_mubert_tags_embeddings(w2v_model):
return w2v_model.encode(MUBERT_TAGS)
def find_similar(em, embeddings, method='cosine'):
scores = []
for ref in embeddings:
if method == 'cosine':
scores.append(1 - np.dot(ref, em) / (np.linalg.norm(ref) * np.linalg.norm(em)))
if method == 'norm':
scores.append(np.linalg.norm(ref - em))
return np.array(scores), np.argsort(scores)
def get_tags_for_prompts(w2v_model, mubert_tags_embeddings, prompts, top_n=3, debug=False):
prompts_embeddings = w2v_model.encode(prompts)
ret = []
for i, pe in enumerate(prompts_embeddings):
scores, idxs = find_similar(pe, mubert_tags_embeddings)
top_tags = MUBERT_TAGS[idxs[:top_n]]
top_prob = 1 - scores[idxs[:top_n]]
if debug:
print(f"Prompt: {prompts[i]}\nTags: {', '.join(top_tags)}\nScores: {top_prob}\n\n\n")
ret.append((prompts[i], list(top_tags)))
print("ret: " + ret)
return ret |