-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank.py
236 lines (186 loc) · 8.27 KB
/
rank.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import ollama
import json
import urllib.parse
import os
from pathlib import Path
OBJECTS_JSON_PATH = "out.json"
INCLUDE_IMAGES = False
# model="llama3",
# model="llama3.2:7b",
# model="llava:34b",
# model = "llava-llama3",
# MODEL = "llama3.1"
MODEL = "llama3.2"
# MODEL = "gemma2"
PROMPT_VERSION = "v3"
PROMPTS = {
"v2": """"
I want to go to organize an event for more then 25 of my friends.
Anything with lower capacity would need to be really amazing for us to consider. In general capacity around 30 places is ideal because we have more flexibility.
We are looking for accommodation and we needs something with a nice common room to play board games, therefore we need many chairs and tables.
We prefer not to have more than 5 people in one room.
We also love PC games so we need a place where to put the desktops and ideally a good internet connection.
Places where the owner stays with us are probably not great because we have long nights and that may be uncomfortable for the owner. So places like guesthouses (penzion in Czech) are not great.
Also apartments are a no-go for us we need to rent the whole property.
The descriptions I will provide will be in Czech but always reply in English.
Make sure to take the visitor reviews with a grain of salt mainly when there is not enough of them.
We already visited the following 2 accommodations with my friends and we really liked it.
The structured description of the first accommodation follows:
{}
The structured description of the second accommodation follows:
{}
The structured description of the accommodation which should be rated follows:
{}
Your task is to rate the described object based on our requirements in json format containing the following fields (and only that):
* "rating": which is a number between 0 and 1 where 1 means very suitable object for the event.
* "description": max one sentence description for the object. Examples: "Fancy wooden cottage with sauna.", "Moldy dump."
* "owner_in_house": boolean, if the owner is present in the house which may be mentioned in the equipment or visitor reviews.
* "explanation": Explain the motivation for the rating.
{}
Make sure to reply with only the valid JSON and nothing more and only in english!
""",
"v3": """"
I want to organize an event for more then 25 of my friends.
Anything with lower capacity would need to be really amazing for us to consider. In general capacity around 30 places is ideal because we have more flexibility.
We are looking for accommodation and we needs something with a nice common room to play board games, therefore we need many chairs and tables.
We prefer not to have more than 5 people in one room.
We also love PC games so we need a place where to put the desktops and ideally a good internet connection.
Places where the owner stays with us are probably not great because we have long nights and that may be uncomfortable for the owner. So places like guesthouses (penzion in Czech) are not great.
Also apartments are a no-go for us we need to rent the whole property.
We do not care about winter amenities because our event is happening in September.
The descriptions I will provide will be in Czech but always reply in English.
Make sure to take the visitor reviews with a grain of salt mainly when there is not enough of them.
We already visited the following 2 accommodations with my friends and we really liked it.
The structured description of the first accommodation follows:
{}
The structured description of the second accommodation follows:
{}
The structured description of the accommodation which should be rated follows:
{}
Your task is to rate the described object based on our requirements in json format containing the following fields (and only that):
* "rating": which is a number between 0 and 1 where 1 means very suitable object for the event.
* "description": max one sentence description for the object. Examples: "Fancy wooden cottage with sauna.", "Moldy dump."
* "owner_in_house": boolean, if the owner is present in the house which may be mentioned in the equipment or visitor reviews.
* "explanation": Explain the motivation for the rating.
{}
Make sure to reply with only the valid JSON and nothing more and only in english!
""",
}
#
def format_property(p):
# url https://ww...
# id objekt č. ...
# name Chalupa Pa...
# locality Pecka...
# capacity 18...
# rooms 4...
# icons ['Domácí m...
# contact_raw Telefo...
# contact_links ['http://w...
# map_link https://ww...
# distances [['autobus...
# equipment ['Možnost ...
# ratings ['Jaro 202...
# numeric_ratings [100, 100,...
# place Pecka - da...
# pricelist ['Ceny za ...
# images [['Chalupa...
# text
# Chalupa P...
# GPS {'N': '50....
# homepage http://www...
# rating_stats {'max': 10...
# distances_map {'autobus'...
# les_distance_m 500.0...
# restaurace_distance_m 200.0...
# obchod_distance_m 200.0...
# price (per day per object) 5580...
# filtered_reasons ['not_enou...
# filtered True...
# area cesky_raj...
ratings = [" * " + i.replace("\r\n", " ").replace("\n", "") for i in p["ratings"]]
return f"""
Name: {p["name"]}
Capacity: {p["capacity"]}
Rooms: {p["rooms"]}
Features: {','.join(p["icons"])}
Equipment: {','.join(p["equipment"])}
Price: {p.get("price (per day per object)", 0)}
Bad features: {','.join(p.get("filtered_reasons", []))}
Description: {p["text"].replace("\r\n", " ").replace("\n", " ").split("Kontakt na pronajímatele nebo provozovatele")[0].split("kontakty mapa")[1].strip()}
Visitor reviews:
{'\n'.join(ratings)}
"""
def load_objects():
return json.load(open(OBJECTS_JSON_PATH))
def find_by_name(name, properties):
for p in properties:
if name in p["name"].lower():
return p
def main():
properties = load_objects()
print(f"loaded {len(properties)} objects")
properties = [p for p in properties if not p.get("filtered", False)]
print(f"filtered to {len(properties)} objects")
simia = find_by_name("chalupa simia", properties)
centrum_slapy = find_by_name("drevníky resort slapy", properties)
# prompt = PROMPT.format(format_property(simia))
#
# response = ollama.generate(
# # model="llama3",
# # model="llama3.2:1b",
# model="gemma2",
# prompt=prompt
# )
#
# print(json.loads(response["response"].replace("```", "").replace("json", "")))
#
# prompt = PROMPT.format(format_property(centrum_slapy))
#
# response = ollama.generate(
# # model="llama3",
# # model="llama3.2:1b",
# model="gemma2",
# prompt=prompt
# )
#
# print(json.loads(response["response"].replace("```", "").replace("json", "")))
processed = 0
ratings = json.load(open("ratings.json"))
for p in properties:
print(p["name"], p["url"])
result_id = f"{MODEL}_{PROMPT_VERSION}"
present_ratings = ratings.get(p["id"], {})
if result_id in present_ratings:
print(f"already rated with {MODEL} and prompt {PROMPT_VERSION} skip")
processed += 1
print(f"{processed}/{len(properties)}")
continue
prompt = PROMPTS[PROMPT_VERSION].format(
format_property(centrum_slapy), format_property(simia), format_property(p),
"Do not forget to use attached images of the accommodation for the analysis.\n" if INCLUDE_IMAGES else "")
# print(prompt)
images = []
if INCLUDE_IMAGES:
images = [os.path.abspath(os.path.join("imgs", urllib.parse.quote(i[1], safe=''))) for i in p["images"]]
response = ollama.generate(
model=MODEL,
prompt=prompt,
images=images,
)
# print(response["response"])
try:
rating = json.loads(response["response"].replace("```", "").replace("json", "").strip())
print(rating)
# print("---")
present_ratings[result_id] = rating
ratings[p["id"]] = present_ratings
except:
print(f"rating failed")
processed += 1
# if processed > 2:
# break
print(f"{processed}/{len(properties)}")
json.dump(ratings, open("ratings.json", "w"))
if __name__ == '__main__':
main()