-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.py
207 lines (179 loc) · 5.8 KB
/
client.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
import os
import sys
import json
import logging
import argparse
import requests
logger = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(message)s",
datefmt="%Y/%m/%d %H:%M:%S",
level=logging.INFO,
)
def read_json(file, is_jsonl=False, write_log=True):
if write_log:
logger.info(f"Reading {file}")
with open(file, "r", encoding="utf8") as f:
if is_jsonl:
data = []
for line in f:
datum = json.loads(line[:-1])
data.append(datum)
else:
data = json.load(f)
if write_log:
objects = len(data)
logger.info(f"Read {objects:,} objects")
return data
def write_json(file, data, is_jsonl=False, indent=None, write_log=True):
if write_log:
objects = len(data)
logger.info(f"Writing {objects:,} objects")
with open(file, "w", encoding="utf8") as f:
if is_jsonl:
for datum in data:
json.dump(datum, f)
f.write("\n")
else:
json.dump(data, f, indent=indent)
if write_log:
logger.info(f"Written to {file}")
return
def get_result_from_server(server_path, query):
logger.info("[Query]")
for key, value in query.items():
logger.info(f"[argument] {key}={value}")
response = requests.get(
server_path,
params=query,
)
result = response.json()
return result
def run_rel_test(server_path, client_path):
query_list = [
{
"e1_spec": json.dumps(
("type_id", ("VARIANT", "RS#:113488022"))
),
"paper_start": "0",
"paper_end": "10",
},
{
"e1_spec": json.dumps(
("type_id", ("VARIANT", "RS#:113488022"))
),
"e2_spec": json.dumps(
("type_id", ("Disease", "MESH:D009369"))
),
"pmid": "35246262",
"paper_start": "0",
"paper_end": "10",
},
{
"e1_spec": json.dumps(
(
"OR",
(
(
"AND",
(
("type_id", ("ProteinMutation", "HGVS:p.V600E")),
("type_id", ("ProteinMutation", "CorrespondingGene:673")),
)
),
("type_id", ("VARIANT", "RS#:113488022")),
)
)
),
"e2_spec": json.dumps(
("type_id", ("Disease", "MESH:D009369"))
),
"pmid": "35246262",
"paper_start": "0",
"paper_end": "10",
},
]
os.makedirs(client_path, exist_ok=True)
for qi, query in enumerate(query_list):
result = get_result_from_server(server_path, query)
result_file = os.path.join(client_path, f"result_{qi}.json")
write_json(result_file, result)
return
def run_litsum_test(server_path, client_path):
openai_api_key = input("OpenAI API Key: ")
query_list = [
{
"entity_1": "V600E", "entity_2": "lung cancer",
"pmid_list": [
"36112789", "24589553", "22511580", "34433708", "33276639",
"33037234", "31452510", "36697098", "34484797", "24019382",
],
},
{
"entity_1": "PRKD1", "entity_2": "diseases",
"pmid_list": None,
},
{
"entity_1": "GABRA5", "entity_2": "diseases",
"pmid_list": None,
},
{
"entity_1": "SYNE1", "entity_2": "diseases",
"pmid_list": None,
},
{
"entity_1": "EPO", "entity_2": "diseases",
"pmid_list": None,
},
{
"entity_1": "V600E", "entity_2": "diseases",
"pmid_list": None,
},
{
"entity_1": "variants", "entity_2": "lung cancer",
"pmid_list": None,
},
{
"entity_1": "genes", "entity_2": "lung cancer",
"pmid_list": None,
},
]
os.makedirs(client_path, exist_ok=True)
for qi, query in enumerate(query_list):
pmid_list = query["pmid_list"]
if not pmid_list:
e1 = query["entity_1"].replace(" ", "_")
e2 = query["entity_2"].replace(" ", "_")
rel_file = os.path.join(client_path, f"{e1}_{e2}_relevance.json")
rel_data = read_json(rel_file)
pmid_list = [paper["pmid"] for paper in rel_data["paper_list"]]
query["pmid_list"] = pmid_list
assert len(pmid_list) == 10
for qi, query in enumerate(query_list):
logger.info(f"[query #{qi}] {query}")
e1 = query["entity_1"].replace(" ", "_")
e2 = query["entity_2"].replace(" ", "_")
query = {
"query_dict": query,
"query": json.dumps(query),
"openai_api_key": openai_api_key,
}
result = get_result_from_server(server_path, query)
result_file = os.path.join(client_path, f"summary_{e1}_and_{e2}.json")
write_json(result_file, result)
return
def main():
parser = argparse.ArgumentParser()
# parser.add_argument("--server", default="https://lab5-k8s.corp.ailabs.tw/pubmedkb-api/rel")
parser.add_argument("--server", default="http://127.0.0.1:12345/query_litsum")
parser.add_argument("--client", default="test_litsum")
arg = parser.parse_args()
for key, value in vars(arg).items():
if value is not None:
logger.info(f"[{key}] {value}")
# run_rel_test(arg.server, arg.client)
run_litsum_test(arg.server, arg.client)
return
if __name__ == "__main__":
main()
sys.exit()