File size: 1,652 Bytes
7d84486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d39ec0
7d84486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
601b1f1
 
 
 
7d84486
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import torch
from timeit import default_timer as timer

from model import create_GPT_model
from utils import prepare_vocab


def main():
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

    vocab_size, encode, decode = prepare_vocab()
    model = create_GPT_model(vocab_size=vocab_size, device=device)

    model.load_state_dict(torch.load(
        f="Pretrained_GPT_med_bot.pth",
        map_location=torch.device(device)))

    def predict(question: str):

        start = timer()

        in_len = len(question)
        prompt = torch.tensor(encode(question),
                              dtype=torch.long,
                              device=device)

        model.eval()
        with torch.inference_mode():
            response = model.generate(prompt.unsqueeze(0),
                                      max_new_tokens=200)[0].tolist()
        answer = decode(response)[in_len:]

        pred_time = round(timer() - start, 5)

        return answer, pred_time

    title = "Med Chat Bot"
    example_list = [
        "What are the common symptoms of the flu?",
        "How can I prevent catching a cold?",
        "What lifestyle changes can I make to improve my heart health?",
        "Is it necessary to get vaccinated every year?"
    ]

    demo = gr.Interface(fn=predict,
                        inputs=gr.Text(),
                        outputs=[gr.Text(label="Answer"),
                                 gr.Number(label="Prediction time (s)")],
                        examples=example_list,
                        title=title)

    demo.launch()


if __name__ == "__main__":
    main()