-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext2knowledge.py
287 lines (257 loc) · 9.85 KB
/
text2knowledge.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import click
import os
import json
import logging
import pandas as pd
from text2knowledge.utils import init_logger, EmbeddingGenerator
from text2knowledge.strategy1 import (
extract_entities as extract_entities_from_text,
extract_relations as extract_relations_from_text,
classify_article as classify_article_from_text,
correct_extracted_entities,
)
logging.basicConfig(level=logging.WARNING)
logger = init_logger(__name__)
cli = click.Group()
@cli.command(help="Extract biomedical entities from a given text or a set of texts.")
@click.option(
"--text-file",
"-i",
help="Text file.",
required=True,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.option(
"--output-file",
"-o",
help="Output file.",
required=True,
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--model-name",
"-m",
help="Model name. You can use any model which supported by ollama.ai. If you don't know which models are available, you can use the command `ollama list` to list all installed models or visit https://ollama.ai/library. Default: mistral-openorca:latest",
default="mistral-openorca:latest",
)
@click.option(
"--metadata",
"-d",
type=click.Path(exists=False, file_okay=False, dir_okay=False),
help="A metadata file which contains a json object. Such as {'source': 'pubmed', 'pmid': '123456', 'type': 'abstract', ...}, you can specify any key-value pairs you want.",
)
@click.option(
"--review",
"-r",
is_flag=True,
help="Review the entities and make corrections.",
)
@click.option(
"--ontology-embedding-file",
"-e",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
help="A file which contains ontology embeddings, which is a biomedgps-format file but with an embedding column.",
)
@click.option(
"--embedding-model-name",
"-n",
help="Embedding model name. Default: mistralai/Mistral-7B-v0.1",
default="mistralai/Mistral-7B-v0.1",
)
def extract_entities(text_file: str, output_file: str, model_name: str, metadata: str, review: bool = False, ontology_embedding_file: str | None = None, embedding_model_name: str = "mistralai/Mistral-7B-v0.1"):
print("Extracting entities using the model %s..." % model_name)
if metadata and os.path.exists(metadata):
with open(metadata, "r") as f:
metadata = f.read()
else:
metadata = {} # type: ignore
def extract(text):
if os.path.exists(output_file):
if review:
entities = json.load(open(output_file))
if entities:
print(
f"Entities found in the {text_file} file, so we will review them."
)
entities = correct_extracted_entities(
text=text,
model=model_name,
metadata=metadata,
entities=entities,
is_list=True,
)
else:
print(
f"No entities found for the {text_file} file, please extract the entities first."
)
return
else:
print(
f"Entities found in the {text_file} file, so we will skip the extraction."
)
return
else:
if ontology_embedding_file and os.path.exists(ontology_embedding_file):
df = pd.read_csv(ontology_embedding_file, sep="\t")
df["embedding"] = df["embedding"].apply(
lambda x: [float(i) for i in x.split("|")]
)
else:
df = None
entities = extract_entities_from_text(
text,
model=model_name,
metadata=metadata,
embeddings=df,
embedding_model_name=embedding_model_name,
)
return entities
if os.path.dirname(output_file) and not os.path.exists(os.path.dirname(output_file)):
os.makedirs(os.path.dirname(output_file))
with open(text_file, "r") as f:
text = f.read()
entities = extract(text)
if entities:
output_file = output_file if not review else output_file.replace(".json", "_reviewed.json")
with open(output_file, "w") as f:
entities_str = json.dumps(entities, indent=4)
f.write(entities_str)
else:
print(f"No entities found for the {text_file} file.")
@cli.command(help="Extract relationships between biomedical entities from a given text using strategy 1.")
@click.option(
"--text-file",
"-a",
help="Text file which contains a paragraph.",
required=True,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.option(
"--output-file",
"-o",
help="Output file.",
required=True,
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--model-name",
"-m",
help="Model name. You can use any model which supported by ollama.ai. If you don't know which models are available, you can use the command `ollama list` to list all installed models or visit https://ollama.ai/library. Default: mistral-openorca:latest",
default="mistral-openorca:latest",
)
@click.option(
"--metadata",
"-d",
type=click.Path(exists=False, file_okay=False, dir_okay=False),
help="A metadata file which contains a json object. Such as {'source': 'pubmed', 'pmid': '123456', 'type': 'abstract', ...}, you can specify any key-value pairs you want.",
)
def extract_relations(text_file: str, model_name: str, metadata: str, output_file: str):
if metadata and os.path.exists(metadata):
with open(metadata, "r") as f:
metadata = f.read()
else:
metadata = {} # type: ignore
with open(text_file, "r") as f:
text = f.read()
relations = extract_relations_from_text(
text, model=model_name, metadata=metadata
)
if relations:
with open(output_file, "w") as f:
relations_str = json.dumps(relations, indent=4)
f.write(relations_str)
else:
print(f"No relations found for the {text_file} file.")
@cli.command(
help="Classify the given text into a specific category using the model."
)
@click.option(
"--input-file",
"-i",
help="A json file which contains a list of texts.",
required=True,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.option(
"--output-file",
"-o",
help="Output file.",
required=True,
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--model-name",
"-m",
help="Model name. You can use any model which supported by ollama.ai. If you don't know which models are available, you can use the command `ollama list` to list all installed models or visit https://ollama.ai/library. Default: mistral-openorca:latest",
default="mistral-openorca:latest",
)
def classify_article(input_file: str, output_file: str, model_name: str):
if not os.path.exists(input_file):
raise FileNotFoundError(f"The {input_file} file does not exist.")
if os.path.exists(output_file):
logger.info("The output file exists, so we will load the previous outputs.")
with open(output_file, "r") as f:
outputs = json.load(f)
else:
outputs = []
processed_titles = set([o.get("title") for o in outputs])
with open(input_file, "r") as f:
data = json.load(f)
for idx, d in enumerate(data):
title = d.get("title", "")
if title in processed_titles:
logger.info(f"Classifying the {idx + 1}th / {len(data)} text: {title} has been processed, so we skip it.")
continue
abstract = d.get("abstract", "")
text = f"{title}\n{abstract or 'No abstract found.'}"
logger.info(f"Classifying the {idx + 1}th / {len(data)} text: {title}")
outputs.append(classify_article_from_text(text, model=model_name))
if idx > 0 and idx % 10 == 0:
with open(output_file, "w") as f:
outputs_str = json.dumps(outputs, indent=4)
f.write(outputs_str)
if outputs:
with open(output_file, "w") as f:
outputs_str = json.dumps(outputs, indent=4)
f.write(outputs_str)
else:
logger.info(f"No valid outputs found for the {input_file} file.")
@cli.command(
help="Generate embeddings for the given entities using the model."
)
@click.option(
"--input-file",
"-i",
help="A tsv file which contains a list of entities.",
required=True,
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.option(
"--output-file",
"-o",
help="Output file.",
required=True,
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--model-name",
"-m",
help="Model name.",
default="mistralai/Mistral-7B-v0.1",
)
def generate_embeddings(input_file: str, output_file: str, model_name: str):
if not os.path.exists(input_file):
raise FileNotFoundError(f"The {input_file} file does not exist.")
with open(input_file, "r") as f:
entities_df = pd.read_csv(f, sep="\t")
embedding_generator = EmbeddingGenerator(model_name=model_name)
entities_df["embedding"] = entities_df["name"].apply(lambda x: embedding_generator.gen_text_embedding(x).tolist())
entities_df["embedding"] = entities_df["embedding"].apply(lambda x: "|".join([str(i) for i in x]))
if entities_df is not None:
entities_df.to_csv(output_file, sep="\t", index=False)
if __name__ == "__main__":
# Add the directory which contains this file to the python path
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
cli()