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 from sklearn.neighbors import NearestNeighbors import numpy as np import pandas as pd from sentence_transformers import SentenceTransformer model = SentenceTransformer('all-mpnet-base-v2') #all-MiniLM-L6-v2 #all-mpnet-base-v2 # os.chdir(os.path.dirname(__file__)) df = pd.read_parquet('df_encoded.parquet') #prepare model nbrs = NearestNeighbors(n_neighbors=4, algorithm='ball_tree').fit(df['text_vector_'].values.tolist()) def search(df, 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]][['Description', 'UnitPrice', 'Country']] import gradio as gr import os #the first module becomes text1, the second module file1 def greet(text1): return search(df, text1) with gr.Blocks(theme=gr.themes.Soft(primary_hue='amber', secondary_hue='gray', neutral_hue='amber')) as demo: gr.Markdown( """ # Try our DEMO!!! """ ) txt = gr.Textbox(value='A Christmas presentšŸŽ„for my 5 years old Kid!!!', label='What are you looking for?') btn = gr.Button(value="Search for Product") state = gr.Dataframe() # btn.click(greet, inputs='text', outputs=['dataframe']) btn.click(greet, [txt], [state]) demo.launch(share=False) # iface = gr.Interface( # fn=greet, # inputs=[ # gr.Textbox(value='A Christmas presentšŸŽ„for my 5 years old Kid!!!', label='Describe the product to search, then press submit') # ], # outputs=["dataframe"], # title='DEMO: Ecommerce Product Recommendation' # ) # iface.launch(share=False)