Views
No views yet

1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
3
4
5model_id = "Narrativaai/BioGPT-Large-finetuned-chatdoctor"
6
7tokenizer = AutoTokenizer.from_pretrained("microsoft/BioGPT-Large")
8
9model = AutoModelForCausalLM.from_pretrained(model_id)
10
11def answer_question(
12 prompt,
13 temperature=0.1,
14 top_p=0.75,
15 top_k=40,
16 num_beams=2,
17 **kwargs,
18):
19 inputs = tokenizer(prompt, return_tensors="pt")
20 input_ids = inputs["input_ids"].to("cuda")
21 attention_mask = inputs["attention_mask"].to("cuda")
22 generation_config = GenerationConfig(
23 temperature=temperature,
24 top_p=top_p,
25 top_k=top_k,
26 num_beams=num_beams,
27 **kwargs,
28 )
29 with torch.no_grad():
30 generation_output = model.generate(
31 input_ids=input_ids,
32 attention_mask=attention_mask,
33 generation_config=generation_config,
34 return_dict_in_generate=True,
35 output_scores=True,
36 max_new_tokens=512,
37 eos_token_id=tokenizer.eos_token_id
38
39 )
40 s = generation_output.sequences[0]
41 output = tokenizer.decode(s, skip_special_tokens=True)
42 return output.split(" Response:")[1]
43
44example_prompt = """
45Below is an instruction that describes a task, paired with an input that provides further context.Write a response that appropriately completes the request.
46
47### Instruction:
48If you are a doctor, please answer the medical questions based on the patient's description.
49
50### Input:
51Hi i have sore lumps under the skin on my legs. they started on my left ankle and are approx 1 - 2cm diameter and are spreading up onto my thies. I am eating panadol night and anti allergy pills (Atarax). I have had this for about two weeks now. Please advise.
52
53### Response:
54"""
55
56print(answer_question(example_prompt))@misc {narrativa_2023,
author = { {Narrativa} },
title = { BioGPT-Large-finetuned-chatdoctor (Revision 13764c0) },
year = 2023,
url = { https://huggingface.co/Narrativaai/BioGPT-Large-finetuned-chatdoctor },
doi = { 10.57967/hf/0601 },
publisher = { Hugging Face }
}