Spaces:
Sleeping
Sleeping
app v0.1
Browse files
app.py
CHANGED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from timeit import default_timer as timer
|
5 |
+
|
6 |
+
from model import create_GPT_model
|
7 |
+
from utils import prepare_vocab
|
8 |
+
|
9 |
+
|
10 |
+
def main():
|
11 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
12 |
+
|
13 |
+
vocab_size, encode, decode = prepare_vocab()
|
14 |
+
model = create_GPT_model(vocab_size=vocab_size, device=device)
|
15 |
+
|
16 |
+
model.load_state_dict(torch.load(
|
17 |
+
f="Pretrianed_GPT_med_bot.pth",
|
18 |
+
map_location=torch.device(device)))
|
19 |
+
|
20 |
+
def predict(question: str):
|
21 |
+
|
22 |
+
start = timer()
|
23 |
+
|
24 |
+
in_len = len(question)
|
25 |
+
prompt = torch.tensor(encode(question),
|
26 |
+
dtype=torch.long,
|
27 |
+
device=device)
|
28 |
+
|
29 |
+
model.eval()
|
30 |
+
with torch.inference_mode():
|
31 |
+
response = model.generate(prompt.unsqueeze(0),
|
32 |
+
max_new_tokens=200)[0].tolist()
|
33 |
+
answer = decode(response)[in_len:]
|
34 |
+
|
35 |
+
pred_time = round(timer() - start, 5)
|
36 |
+
|
37 |
+
return answer, pred_time
|
38 |
+
|
39 |
+
title = "Med Chat Bot"
|
40 |
+
example_list = [
|
41 |
+
|
42 |
+
]
|
43 |
+
|
44 |
+
demo = gr.Interface(fn=predict,
|
45 |
+
inputs=gr.Text(),
|
46 |
+
outputs=[gr.Text(label="Answer"),
|
47 |
+
gr.Number(label="Prediction time (s)")],
|
48 |
+
examples=example_list,
|
49 |
+
title=title)
|
50 |
+
|
51 |
+
demo.launch()
|
52 |
+
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
main()
|