Spaces:
Runtime error
Runtime error
Chitrayogi
commited on
Commit
•
271be70
1
Parent(s):
77e1eb2
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
|
4 |
+
|
5 |
+
path = "jianghc/medical_chatbot"
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained(path)
|
8 |
+
model = GPT2LMHeadModel.from_pretrained(path).to(device)
|
9 |
+
|
10 |
+
prompt_input = (
|
11 |
+
"The conversation between human and AI assistant.\n"
|
12 |
+
"[|Human|] {input}\n"
|
13 |
+
"[|AI|]"
|
14 |
+
)
|
15 |
+
sentence = prompt_input.format_map({'input': "what is parkinson's disease?"})
|
16 |
+
inputs = tokenizer(sentence, return_tensors="pt").to(device)
|
17 |
+
|
18 |
+
with torch.no_grad():
|
19 |
+
beam_output = model.generate(**inputs,
|
20 |
+
min_new_tokens=1,
|
21 |
+
max_length=512,
|
22 |
+
num_beams=3,
|
23 |
+
repetition_penalty=1.2,
|
24 |
+
early_stopping=True,
|
25 |
+
eos_token_id=198
|
26 |
+
)
|
27 |
+
print(tokenizer.decode(beam_output[0], skip_special_tokens=True))
|