Tom Beer commited on
Commit
6459725
·
1 Parent(s): d1b37d6

expose prompt

Browse files
Files changed (2) hide show
  1. app.py +10 -7
  2. data.py +57 -0
app.py CHANGED
@@ -1,15 +1,18 @@
1
- import gradio as gr
 
2
 
 
3
 
4
- def greet(city, preferences):
5
- return "Recommending a hotel in city " + city + "according to preferences " + ", ".join(preferences)
6
 
 
 
7
 
8
- demo = gr.Interface(
9
- fn=greet,
 
10
  inputs=[
11
- gr.Dropdown(["cat", "dog", "bird"], label="City"),
12
- gr.CheckboxGroup(["Kid friendly", "other option"], label="Preferences"),
13
  ],
14
  outputs="text"
15
  )
 
1
+ from gradio import Interface, Dropdown, CheckboxGroup
2
+ from data import get_prompt, get_cities
3
 
4
+ cities = get_cities()
5
 
 
 
6
 
7
+ def hotel_recommender(city, preferences) -> dict:
8
+ return get_prompt(city, preferences)
9
 
10
+
11
+ demo = Interface(
12
+ fn=hotel_recommender,
13
  inputs=[
14
+ Dropdown(cities, label="City"),
15
+ CheckboxGroup(["Kid friendly", "other option"], label="Preferences"),
16
  ],
17
  outputs="text"
18
  )
data.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from json import load as load_json
3
+ from numpy.random import permutation as perm
4
+
5
+ data_dir = Path(__file__).parent / "data" / "cmu" / "processed"
6
+
7
+
8
+ def get_cities():
9
+ with open(data_dir / "cities.json", "r") as f:
10
+ return load_json(f)
11
+
12
+
13
+ def get_score_threshold_per_city():
14
+ with open(data_dir / "score_threshold_per_city.json", "r") as f:
15
+ return load_json(f)
16
+
17
+
18
+ def get_city_to_hotel_id_map():
19
+ with open(data_dir / "city_to_hotel_id_map.json", "r") as f:
20
+ return load_json(f)
21
+
22
+
23
+ def get_hotel_id_to_name_map():
24
+ with open(data_dir / "hotel_id_to_name_map.json", "r") as f:
25
+ return load_json(f)
26
+
27
+
28
+ def get_hotel_id_to_review_map():
29
+ with open(data_dir / "hotel_id_to_review_map.json", "r") as f:
30
+ return load_json(f)
31
+
32
+
33
+ score_threshold_per_city = get_score_threshold_per_city()
34
+ city_to_hotel_id_map = get_city_to_hotel_id_map()
35
+ hotel_id_to_name_map = get_hotel_id_to_name_map()
36
+ hotel_id_to_review_map = get_hotel_id_to_review_map()
37
+
38
+
39
+ def get_prompt(city, preferences) -> dict:
40
+
41
+ score_threshold = score_threshold_per_city[city]
42
+ for hotel_id in perm(city_to_hotel_id_map[city]):
43
+ hotel_id = str(hotel_id)
44
+ res = {"hotel_name": hotel_id_to_name_map[hotel_id], 'positive': [], 'negative': []}
45
+ if hotel_id_to_review_map[hotel_id]['average_score'] >= score_threshold:
46
+ hotel_reviews = hotel_id_to_review_map[hotel_id]['reviews']
47
+ if len(hotel_reviews) > 100:
48
+ for review in perm(hotel_reviews):
49
+ if review['num_helpful'] > 10:
50
+ if (review['score'] == 5) & (len(res['positive']) < 3):
51
+ res['positive'].append(review)
52
+ if (review['score'] <= 2) & (len(res['negative']) < 1):
53
+ res['negative'].append(review)
54
+ if (len(res['positive']) >= 3) & (len(res['negative']) >= 1):
55
+ break
56
+
57
+ return res