File size: 849 Bytes
4da19d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pandas as pd
import numpy as np
from sentence_transformers import SentenceTransformer
from scipy.spatial.distance import cdist
import gradio as gr


df=pd.read_csv("english_idioms.csv")
meaning=list(df.meaning)
idioms= list(df.idioms)

model = SentenceTransformer("all-mpnet-base-v2")
idiom_meaning_embeddings=vectors =  np.load("vectors.npy")

def get_best(query):
  query_embedding = model.encode([query])
  distances = cdist(query_embedding, idiom_meaning_embeddings, "cosine")[0]
  ind = np.argsort(distances, axis=0)
  return idioms[ind[0]],  distances[ind[0]], meaning[ind[0]]

gr.Interface(fn=get_best, inputs=[gr.Text(label="Enter a descriptive sentence for the idiom you're looking for",placeholder="I feel sick!"  )], outputs=[gr.Text(label="Idiom"),gr.Number(label="Distance Score"), gr.Text(label="Idiom Explanation")]).launch()