zizzimars commited on
Commit
0df907f
·
verified ·
1 Parent(s): 99d78fb

Update medchat.py

Browse files
Files changed (1) hide show
  1. medchat.py +13 -15
medchat.py CHANGED
@@ -7,25 +7,23 @@ class MedChat:
7
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
8
  self.tokenizer = GPT2Tokenizer.from_pretrained(self.path)
9
  self.model = GPT2LMHeadModel.from_pretrained(self.path).to(self.device)
10
- def prompt(self, input):
 
 
11
  prompt_input = (
12
  "The conversation between human and AI assistant.\n"
13
- f"[|Human|] {self.input}\n"
14
  "[|AI|]"
15
  )
16
-
17
- def __call__(self, question):
18
  sentence = prompt_input.format_map({'input': f"{question}"})
19
- inputs = tokenizer(sentence, return_tensors="pt").to(self.device)
20
  with torch.no_grad():
21
  beam_output = self.model.generate(**inputs,
22
- min_new_tokens=1,
23
- max_length=512,
24
- num_beams=3,
25
- repetition_penalty=1.2,
26
- early_stopping=True,
27
- eos_token_id=198
28
- )
29
- return tokenizer.decode(beam_output[0], skip_special_tokens=True)
30
-
31
-
 
7
  self.device = "cuda" if torch.cuda.is_available() else "cpu"
8
  self.tokenizer = GPT2Tokenizer.from_pretrained(self.path)
9
  self.model = GPT2LMHeadModel.from_pretrained(self.path).to(self.device)
10
+
11
+
12
+ def forward(self, question):
13
  prompt_input = (
14
  "The conversation between human and AI assistant.\n"
15
+ "[|Human|]"
16
  "[|AI|]"
17
  )
 
 
18
  sentence = prompt_input.format_map({'input': f"{question}"})
19
+ inputs = self.tokenizer(sentence, return_tensors="pt").to(self.device)
20
  with torch.no_grad():
21
  beam_output = self.model.generate(**inputs,
22
+ min_new_tokens=1,
23
+ max_length=512,
24
+ num_beams=3,
25
+ repetition_penalty=1.2,
26
+ early_stopping=True,
27
+ eos_token_id=198
28
+ )
29
+ return self.tokenizer.decode(beam_output[0], skip_special_tokens=True)