File size: 2,050 Bytes
dfa5f93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8787192
dfa5f93
 
 
 
 
3e36f52
 
dfa5f93
 
 
ce22bda
dfa5f93
 
 
 
ce22bda
 
dfa5f93
 
 
 
 
 
 
 
3e36f52
dfa5f93
 
 
 
3e36f52
dfa5f93
 
3e36f52
 
 
 
 
 
dfa5f93
 
 
 
3e36f52
 
dfa5f93
 
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
import torch
import gradio as gr
from transformers import BioGptTokenizer, BioGptForCausalLM

model_names = [
    "BioGPT",
    "BioGPT-Large",
    "BioGPT-QA-PubMedQA-BioGPT",
    "BioGPT-QA-PubMEDQA-BioGPT-Large",
    "BioGPT-RE-BC5CDR",
    "BioGPT-RE-DDI",
    "BioGPT-RE-DTI",
    "BioGPT-DC-HoC"
]

def load_model(model_name="BioGPT"):
  model_name_map = {
      "BioGPT":"microsoft/biogpt",
      "BioGPT-QA-PubMedQA-BioGPT":"microsoft/BioGPT-Large-PubMedQA"
  }

  tokenizer = BioGptTokenizer.from_pretrained(model_name_map[model_name])
  model = BioGptForCausalLM.from_pretrained(model_name_map[model_name])
  return tokenizer, model


def get_beam_output(sentence, selected_model, min_len=100,max_len=512, n_beams=1):
    tokenizer, model = load_model(selected_model)
    inputs = tokenizer(sentence, return_tensors="pt")
    with torch.no_grad():
        beam_output = model.generate(**inputs,
                                    min_length=min_len,
                                    max_length=max_len,
                                    num_beams=n_beams,
                                    early_stopping=True
                                    )
    output=tokenizer.decode(beam_output[0], skip_special_tokens=True)
    return output

inputs = [
    gr.inputs.Textbox(label="prompt", lines=5, default="Bicalutamide"),
    gr.Dropdown(model_names, value="BioGPT", label="selected_model"),
    gr.inputs.Slider(1, 500, 1, default=100, label="min_len"),
    gr.inputs.Slider(1, 2048, 1, default=1024, label="max_len"),
    gr.inputs.Slider(1, 10, 1, default=5, label="num_beams")
    ]

outputs = gr.outputs.Textbox(label="output")

examples = [
    ["Bicalutamide", "BioGPT", 25, 100, 5],
    ["Janus kinase 3 (JAK-3)", "BioGPT", 25, 100, 5],
    ["Apricitabine", "BioGPT", 25, 100, 5],
]

iface = gr.Interface(
  fn=get_beam_output, 
  inputs=inputs,
  outputs=outputs,
  examples=examples,
  title="BioGPT: generative pre-trained transformer for biomedical text generation and mining"
)
iface.launch(debug=True, enable_queue=True)