Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 3 |
+
|
| 4 |
+
# Define the device
|
| 5 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 6 |
+
|
| 7 |
+
model_id = "Narrativaai/BioGPT-Large-finetuned-chatdoctor"
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/BioGPT-Large")
|
| 10 |
+
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 12 |
+
|
| 13 |
+
# Move the model to the device
|
| 14 |
+
model = model.to(device)
|
| 15 |
+
|
| 16 |
+
def answer_question(
|
| 17 |
+
prompt,
|
| 18 |
+
temperature=0.1,
|
| 19 |
+
top_p=0.75,
|
| 20 |
+
top_k=40,
|
| 21 |
+
num_beams=2,
|
| 22 |
+
**kwargs,
|
| 23 |
+
):
|
| 24 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 25 |
+
# Move the inputs to the device
|
| 26 |
+
inputs = {key: val.to(device) for key, val in inputs.items()}
|
| 27 |
+
input_ids = inputs["input_ids"]
|
| 28 |
+
attention_mask = inputs["attention_mask"]
|
| 29 |
+
generation_config = GenerationConfig(
|
| 30 |
+
temperature=temperature,
|
| 31 |
+
top_p=top_p,
|
| 32 |
+
top_k=top_k,
|
| 33 |
+
num_beams=num_beams,
|
| 34 |
+
**kwargs,
|
| 35 |
+
)
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
generation_output = model.generate(
|
| 38 |
+
input_ids=input_ids,
|
| 39 |
+
attention_mask=attention_mask,
|
| 40 |
+
generation_config=generation_config,
|
| 41 |
+
return_dict_in_generate=True,
|
| 42 |
+
output_scores=True,
|
| 43 |
+
max_new_tokens=512,
|
| 44 |
+
eos_token_id=tokenizer.eos_token_id
|
| 45 |
+
|
| 46 |
+
)
|
| 47 |
+
s = generation_output.sequences[0]
|
| 48 |
+
output = tokenizer.decode(s, skip_special_tokens=True)
|
| 49 |
+
return output.split(" Response:")[1]
|
| 50 |
+
|
| 51 |
+
example_prompt = """
|
| 52 |
+
Below is an instruction that describes a task, paired with an input that provides further context.Write a response that appropriately completes the request.
|
| 53 |
+
|
| 54 |
+
### Instruction:
|
| 55 |
+
If you are a doctor, please answer the medical questions based on the patient's description.
|
| 56 |
+
|
| 57 |
+
### Input:
|
| 58 |
+
Hi 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.
|
| 59 |
+
|
| 60 |
+
### Response:
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
print(answer_question(example_prompt))
|