Spaces:
Runtime error
Runtime error
File size: 1,933 Bytes
cf172ac c4ee038 cf172ac f11cbf5 cf172ac c1c573b f11cbf5 39995f1 f11cbf5 d4ac261 f11cbf5 d4ac261 |
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 50 51 52 53 54 55 56 57 58 |
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) |