saritha5 commited on
Commit
1e8c6a7
·
1 Parent(s): f637ac1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py CHANGED
@@ -35,6 +35,35 @@ datafile_path = "fine_food_reviews_with_embeddings_1k.csv"
35
  df = pd.read_csv(datafile_path)
36
  df["embedding"] = df.embedding.apply(eval).apply(np.array)
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  prompt = input("What do you want to search for? : ")
39
  top_n = int(input("How many results do you want to see? : "))
40
  print()
 
35
  df = pd.read_csv(datafile_path)
36
  df["embedding"] = df.embedding.apply(eval).apply(np.array)
37
 
38
+ # search through the reviews for a specific product
39
+ def search_reviews(df, product_description, n=3, pprint=True):
40
+ product_embedding = get_embedding(
41
+ product_description,
42
+ engine="text-embedding-ada-002"
43
+ )
44
+ df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, product_embedding))
45
+
46
+ results = (
47
+ df.sort_values("similarity", ascending=False)
48
+ .head(n)
49
+ .combined.str.replace("Title: ", "")
50
+ .str.replace("; Content:", ": ")
51
+ )
52
+
53
+ product = (
54
+ df.sort_values("similarity", ascending=False)
55
+ .head(n)
56
+ .ProductId
57
+ )
58
+
59
+ if pprint:
60
+ for r in range(n):
61
+ idx = results.index[r]
62
+ print("Product : ",product[idx])
63
+ print(results[idx])
64
+ print()
65
+ return results,product
66
+
67
  prompt = input("What do you want to search for? : ")
68
  top_n = int(input("How many results do you want to see? : "))
69
  print()