Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5model_hf = "microsoft/BioGPT-Large"
6lora_adapters = "mdelmas/BioGPT-Large-Natural-Products-RE-Diversity-synt-v1.0"
7
8# Load model and plug adapters using peft
9model = AutoModelForCausalLM.from_pretrained(model_hf)
10model = PeftModel.from_pretrained(model, lora_adapters)
11model = model.merge_and_unload()
12tokenizer = AutoTokenizer.from_pretrained(model_hf)
13
14# Example from PubMed article 24048364
15title_text = "Producers and important dietary sources of ochratoxin A and citrinin."
16abstract_text = "Ochratoxin A (OTA) is a very important mycotoxin, and its research is focused right now on the new findings of OTA, like being a complete carcinogen, information about OTA producers and new exposure sources of OTA. Citrinin (CIT) is another important mycotoxin, too, and its research turns towards nephrotoxicity. Both additive and synergistic effects have been described in combination with OTA. OTA is produced in foodstuffs by Aspergillus Section Circumdati (Aspergillus ochraceus, A. westerdijkiae, A. steynii) and Aspergillus Section Nigri (Aspergillus carbonarius, A. foetidus, A. lacticoffeatus, A. niger, A. sclerotioniger, A. tubingensis), mostly in subtropical and tropical areas. OTA is produced in foodstuffs by Penicillium verrucosum and P. nordicum, notably in temperate and colder zones. CIT is produced in foodstuffs by Monascus species (Monascus purpureus, M. ruber) and Penicillium species (Penicillium citrinum, P. expansum, P. radicicola, P. verrucosum). OTA was frequently found in foodstuffs of both plant origin (e.g., cereal products, coffee, vegetable, liquorice, raisins, wine) and animal origin (e.g., pork/poultry). CIT was also found in foodstuffs of vegetable origin (e.g., cereals, pomaceous fruits, black olive, roasted nuts, spices), food supplements based on rice fermented with red microfungi Monascus purpureus and in foodstuffs of animal origin (e.g., cheese)."
17text = title_text + " " + abstract_text
18
19# Tokenization
20input_text = text + tokenizer.eos_token + tokenizer.bos_token
21input_tokens = tokenizer(input_text, return_tensors='pt')
22
23# Decoding parameters
24EVAL_GENERATION_ARGS = {"max_length": 1024,
25 "do_sample": False,
26 "forced_eos_token_id": tokenizer.eos_token_id,
27 "num_beams": 3,
28 "early_stopping": "never",
29 "length_penalty": 1.5,
30 "temperature": 0}
31
32# Generation
33with torch.no_grad():
34 beam_output = model.generate(**input_tokens, **EVAL_GENERATION_ARGS)
35 output = tokenizer.decode(beam_output[0][len(input_tokens["input_ids"][0]):], skip_special_tokens=True)
36
37# Parse and print
38rels = output.strip().split("; ")
39for rel in rels:
40 print("- " + rel)