File size: 1,848 Bytes
1410c49
 
 
 
 
 
 
 
11ee1d3
1410c49
 
 
11ee1d3
 
1410c49
11ee1d3
1410c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bc958
 
1410c49
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import pickle
from sentence_transformers import SentenceTransformer, util

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"]

def user_query_recommend(query, min_p, max_p):
    # 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['price'] =recommendations.price.apply(lambda x: re.findall("\d+", x.replace(",","").replace(".00","").replace("$",""))[0]).astype('int')
    recommendations['sim'] = sim_scores.T
    recommendations = recommendations.sort_values('sim', ascending=False)
    recommendations = recommendations.loc[(recommendations.price >= min_p) &
                                          (recommendations.price <= max_p),
                                          ['name', 'category', 'price', 'description', 'sim']]

    return recommendations
    
interface = gr.Interface(
    user_query_recommend, 
    inputs=[gr.inputs.Textbox(),
            gr.inputs.Slider(minimum=1, maximum=100, default=30, label='Min Price'),
            gr.inputs.Slider(minimum=1, maximum=1000, default=70, label='Max Price')],
    outputs=[
        gr.outputs.Textbox(label="Recommendations"),
    ],
    title = "Scotch Recommendation",
    examples=[["very sweet with lemons and oranges and marmalades"], 
              ["smoky peaty earthy and spicy"]],
    theme="huggingface",
)

interface.launch(
    enable_queue=True,
    cache_examples=True,
)