-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
49 lines (40 loc) · 1.66 KB
/
app.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
from fastapi import FastAPI
import numpy as np
import pickle
import pandas as pd
import tensorflow as tf
app = FastAPI()
# LoadData
with open('./models/word2int.pkl', 'rb') as f:
word2int = pickle.load(f)
with open('./models/vectors.pkl', 'rb') as f:
vectors = pickle.load(f)
# Assuming you have a target_word_vector representing the input word vector
target_word_vector = vectors[word2int['career']]
# Normalize the target_word_vector
normalized_target = tf.nn.l2_normalize(target_word_vector, axis=0)
# Normalize all vectors
normalized_vectors = tf.nn.l2_normalize(vectors, axis=1)
# Calculate cosine similarity
cos_sim = tf.matmul(normalized_vectors, tf.expand_dims(normalized_target, axis=1))
@app.get("/")
def read_root():
return {"message": "Success Bro!", "status": "OK", "code": "200"}
# Endpoint method
@app.get("/api/searching/{input_word}")
def get_similar_words(input_word: str):
target_word_vector = vectors[word2int[input_word]]
normalized_target = tf.nn.l2_normalize(target_word_vector, axis=0)
normalized_vectors = tf.nn.l2_normalize(vectors, axis=1)
cos_sim = tf.matmul(normalized_vectors, tf.expand_dims(normalized_target, axis=1))
cos_sim_values = cos_sim.numpy()
int2word = {v: k for k, v in word2int.items()}
k = 100 # Number of similar words to display
most_similar_indices = np.argsort(-cos_sim_values[:, 0])[:k]
similar_words = []
for idx in most_similar_indices:
if idx in int2word:
word = int2word[idx]
similarity = (cos_sim_values[idx, 0] + 1) / 2
similar_words.append({"Word": word, "Cosine Similarity": similarity})
return {"similar_words": similar_words}