# App v3.0 import pandas as pd import pickle import gradio as gr with open(r'vect_index.pickle', 'rb') as file: vect_index = pickle.load(file) with open(r'vect_values.pickle', 'rb') as file: vect_values = pickle.load(file) df = pd.read_excel('perfume_database_cleaned.xlsx') brand_options = list(df['brand'].unique()) perfume_options = list(df['perfume'].unique()) def perfume_change(brand): names = list(df.query(f"brand=='{brand}'")['perfume']) return gr.update(choices=names, value=None) def filter_by_perfume(selected_brand, selected_perfume): perfume_index = df.query(f"brand=='{selected_brand}' & perfume=='{selected_perfume}'").index[0] df_filter = df.iloc[vect_index[perfume_index], :].reset_index(drop=True) df_filter['similarity'] = vect_values[perfume_index] df_filter['similarity'] = df_filter['similarity'].map("{:.2%}".format) df_filter['notes'] = df_filter['notes'].str.split(',').apply(sorted, 1).str.join(',').str.strip(',') df_filter = df_filter[['brand', 'perfume', 'similarity', 'notes']] return df_filter with gr.Blocks(theme='freddyaboulton/dracula_revamped') as demo: gr.Markdown( """ # Perfume Recommendation Select brand and perfume below to find top 5 most similar perfumes based on notes """) with gr.Row(): brand_dropdown = gr.Dropdown(choices=brand_options, value='Hugo Boss', label="Brand") perfume_dropdown = gr.Dropdown(choices=perfume_options, value='Boss Soul', label="Perfume Name") brand_dropdown.change(fn=perfume_change, inputs=[brand_dropdown], outputs=[perfume_dropdown]) btn = gr.Button(value="Search", scale=.1) with gr.Row(): output_df = gr.Dataframe() btn.click(filter_by_perfume, inputs=[brand_dropdown, perfume_dropdown], outputs=[output_df]) demo.launch(share = True)