File size: 1,465 Bytes
4e74356
0820f13
 
 
4e74356
0820f13
4e74356
0820f13
4e74356
0820f13
 
 
 
 
4e74356
0820f13
 
 
 
4e74356
0820f13
 
4e74356
0820f13
 
 
4e74356
0820f13
4e74356
0820f13
 
4e74356
0820f13
 
 
4e74356
0820f13
 
 
 
 
 
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
import os
os.system('pip install openpyxl')
os.system('pip install sentence-transformers')
import pandas as pd
import gradio as gr
from sentence_transformers import SentenceTransformer

model = SentenceTransformer('all-mpnet-base-v2') #all-MiniLM-L6-v2 #all-mpnet-base-v2

df = pd.read_parquet('df_encoded.parquet')
df.columns = [['name', 'description', 'year', 'target', 'size', 'stage', 'raised', 'tags', 'text_vector_']]
#if parsing from a parquet, I have a list of array that does not want to get changed 
df_knn = [x[0].tolist() for x in df['text_vector_'].values.tolist()]
df = df.reset_index(drop=True)

from sklearn.neighbors import NearestNeighbors
import numpy as np
import pandas as pd
from sentence_transformers import SentenceTransformer

#prepare model
nbrs = NearestNeighbors(n_neighbors=5, algorithm='ball_tree').fit(df_knn)

def search(query):
    product = model.encode(query).tolist()
    # product = df.iloc[0]['text_vector_'] #use one of the products as sample

    distances, indices = nbrs.kneighbors([product]) #input the vector of the reference object

    #print out the description of every recommended product
    return df.iloc[list(indices)[0]][['name', 'description', 'year', 'target', 'size', 'stage', 'raised', 'tags']]

#the first module becomes text1, the second module file1
def greet(text1): 
    return search(text1)

iface = gr.Interface(
    fn=greet,
    inputs=['text'],
    outputs=["dataframe"]
)
iface.launch(share=True)