-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_read.py
More file actions
executable file
·62 lines (54 loc) · 1.94 KB
/
auto_read.py
File metadata and controls
executable file
·62 lines (54 loc) · 1.94 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/env python
import pandas
import torch
import typer
from datasets import Dataset
from rich.progress import Progress, SpinnerColumn, TextColumn, track
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
from transformers.pipelines.pt_utils import KeyDataset
def initial_pipeline(model_name):
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
return pipeline(task="text2text-generation",
model=model,
tokenizer=tokenizer,
device=device)
app = typer.Typer()
@app.command()
def answer(
question: str = typer.Argument(
...,
help=
"The question to answer"
),
papers: str = typer.Argument(
...,
help=
"The tsv file of papers infomation with columns `title` and `abstract`"
),
outfile: str = typer.Argument(...),
model: str = typer.Option("google/flan-t5-base"),
batch_size: int = typer.Option(64, "--batch_size", "-bs")):
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=False,
) as progress:
progress.add_task(description="Loading Model...", total=None)
pipe = initial_pipeline(model)
df = pandas.read_csv(papers, sep='\t')
inputs = df.apply(lambda x: f"{x['title']}\n{x['abstract']}\n\n{question}",
axis=1)
data = KeyDataset(Dataset.from_pandas(inputs.to_frame()), key='0')
answers = [
output[0]['generated_text']
for output in track(pipe(data, batch_size=batch_size),
total=inputs.size,
description="Reading...")
]
with open(outfile, 'w') as fout:
fout.write(f"{question}\n")
fout.write("\n".join(answers))
if __name__ == "__main__":
app()