Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pandas as pd | |
import pickle | |
from sentence_transformers import SentenceTransformer, util | |
import re | |
mdl_name = 'sentence-transformers/all-distilroberta-v1' | |
model = SentenceTransformer(mdl_name) | |
embedding_cache_path = "scotch_embd_distilroberta.pkl" | |
with open(embedding_cache_path, "rb") as fIn: | |
cache_data = pickle.load(fIn) | |
embedding_table = cache_data["embeddings"] | |
reviews = cache_data["data"] | |
reviews['price'] = reviews.price.apply(lambda x: re.findall("\d+", x.replace(",","").replace(".00","").replace("$",""))[0]).astype('int') | |
def user_query_recommend(query, price_rng): | |
# Embed user query | |
embedding = model.encode(query) | |
# Calculate similarity with all reviews | |
sim_scores = util.cos_sim(embedding, embedding_table) | |
#print(sim_scores.shape) | |
# Recommend | |
recommendations = reviews.copy() | |
recommendations['sim'] = sim_scores.T | |
recommendations = recommendations.sort_values('sim', ascending=False) | |
if price_rng == "$0-$50": | |
min_p, max_p = 0, 50 | |
if price_rng == "$50-$100": | |
min_p, max_p = 50, 100 | |
if price_rng == "$100+": | |
min_p, max_p = 100, 10000 | |
recommendations = recommendations.loc[(recommendations.price >= min_p) & | |
(recommendations.price <= max_p), | |
['name', 'price', 'description']].head(5) | |
return recommendations.reset_index(drop=True) | |
interface = gr.Interface( | |
user_query_recommend, | |
inputs=[gr.inputs.Textbox(lines=5, label = "enter flavour profile"), | |
gr.inputs.Radio(choices = ["$0-$50", "$50-$100", "$100+"], default="$0-$50", type="value", label='Price range')], | |
outputs=gr.outputs.Dataframe(max_rows=1, overflow_row_behaviour="paginate", type="pandas", label="Scotch recommendations"), | |
title = "Scotch Recommendation", | |
description = "Looking for scotch recommendations and have some flavours in mind? \nGet recommendations at a preferred price range using semantic search :) ", | |
examples=[["very sweet with lemons and oranges and marmalades", "$0-$50"], | |
["smoky peaty earthy and spicy","$50-$100"], | |
["salty and spicy with exotic fruits", "$100+"], | |
["fragrant nose with chocolate, toffee, pudding and caramel", "$50-$100"], | |
], | |
theme="grass", | |
) | |
interface.launch( | |
enable_queue=True, | |
#cache_examples=True, | |
) |