-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmatch_rerank.py
More file actions
38 lines (29 loc) · 1.11 KB
/
match_rerank.py
File metadata and controls
38 lines (29 loc) · 1.11 KB
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
import dashscope
import json
descriptions_path = "./autodl-tmp/descriptions.json"
dashscope.api_key = "api_key"
with open(descriptions_path, "r", encoding="utf-8") as f:
descriptions = json.load(f)
# Process in batches of 500
batch_size = 500
best_match = None
best_score = -1
descriptions_items = list(descriptions.items())
for i in range(0, len(descriptions_items), batch_size):
batch = descriptions_items[i:i + batch_size]
descriptions_list = [item[1] for item in batch]
resp = dashscope.TextReRank.call(
model=dashscope.TextReRank.Models.gte_rerank,
query="歌词",
documents=descriptions_list,
top_n=1
)
batch_best_score = resp.output.results[0].relevance_score
batch_best_index = resp.output.results[0].index
print(f"[INFO] Best match in batch: {batch[batch_best_index]}")
print(f"[INFO] Best score in batch: {batch_best_score}")
if batch_best_score > best_score:
best_score = batch_best_score
best_match = batch[batch_best_index]
print(f"[DONE] Best match overall: {best_match}")
print(f"[DONE] Best score: {best_score}")