Spaces:
Runtime error
Runtime error
import pickle | |
from tensorflow.keras.models import load_model | |
from scipy import spatial | |
import gradio as gr | |
model = load_model('word2vec_65_epochs.h5') | |
model.pop() | |
with open('text_to_seq.pkl', 'rb') as f: | |
tokenizer = pickle.load(f) | |
with open('inference_dict_for_word2vec.pkl', 'rb') as f: | |
inference_dict = pickle.load(f) | |
def get_answer(text): | |
similarities = [] | |
ans = [] | |
seq = tokenizer.texts_to_sequences([text]) | |
pred = model.predict(seq) | |
for j,i in enumerate(model.layers[0].get_weights()[0]): | |
similarities.append([(1 - spatial.distance.cosine(pred[0], i)),j]) | |
similarities = sorted(similarities,key=lambda x: x[0],reverse=True) | |
for i in range(10): | |
ans.append((inference_dict[similarities[i][1]],similarities[i][0])) | |
return f"{ans}" | |
demo = gr.Interface(fn=get_answer, inputs="text", outputs="label") | |
demo.launch() |