Spaces:
Sleeping
Sleeping
Update medchat.py
Browse files- 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 |
-
|
|
|
|
|
|
|
| 11 |
prompt_input = (
|
| 12 |
"The conversation between human and AI assistant.\n"
|
| 13 |
-
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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)
|
|
|
|
|
|