MohammedAlakhras commited on
Commit
608f3af
1 Parent(s): 13f7006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -2
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import torch
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
3
 
 
 
4
 
5
  model_id = "Narrativaai/BioGPT-Large-finetuned-chatdoctor"
6
 
@@ -8,6 +10,9 @@ tokenizer = AutoTokenizer.from_pretrained("microsoft/BioGPT-Large")
8
 
9
  model = AutoModelForCausalLM.from_pretrained(model_id)
10
 
 
 
 
11
  def answer_question(
12
  prompt,
13
  temperature=0.1,
@@ -17,8 +22,10 @@ def answer_question(
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,
@@ -57,6 +64,19 @@ print(answer_question(example_prompt))
57
 
58
 
59
  def gui_interface(prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  return answer_question(prompt)
61
 
62
  iface = gr.Interface(fn=gui_interface, inputs="text", outputs="text")
 
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
 
 
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,
 
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,
 
64
 
65
 
66
  def gui_interface(prompt):
67
+
68
+ prompt="""
69
+ Below is an instruction that describes a task, paired with an input that provides further context.Write a response that appropriately completes the request.
70
+
71
+ ### Instruction:
72
+ If you are a doctor, please answer the medical questions based on the patient's description.
73
+
74
+ ### Input:
75
+ """+prompt+"""
76
+ ### Response:
77
+ """
78
+
79
+ print(p)
80
  return answer_question(prompt)
81
 
82
  iface = gr.Interface(fn=gui_interface, inputs="text", outputs="text")