raghavdw commited on
Commit
24940d0
·
verified ·
1 Parent(s): 0806e41

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio
2
+ import torch
3
+ from transformers import AutoModelWithLMHead, AutoTokenizer
4
+
5
+ # Load model directly
6
+
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained("raghavdw/finedtuned_gpt2_medQA_model")
9
+ model = AutoModelForCausalLM.from_pretrained("raghavdw/finedtuned_gpt2_medQA_model")
10
+
11
+ # Function for response generation
12
+
13
+ def generate_query_response(prompt, max_length=200):
14
+
15
+ model = loaded_model
16
+ tokenizer = loaded_tokenizer
17
+
18
+ input_ids = tokenizer.encode(prompt, return_tensors="pt")
19
+
20
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21
+ model.to(device)
22
+ input_ids = input_ids.to(device)
23
+ attention_mask = torch.ones_like(input_ids)
24
+ pad_token_id = tokenizer.eos_token_id
25
+
26
+ output = model.generate(input_ids,
27
+ max_length=max_length,
28
+ num_return_sequences=1,
29
+ attention_mask=attention_mask,
30
+ pad_token_id=pad_token_id)
31
+
32
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
33
+ return response
34
+
35
+ # Gradio elements
36
+
37
+ # Input from user
38
+ in_prompt = gradio.Textbox(label="Enter your prompt")
39
+
40
+ # Output response
41
+ in_max_length = 200
42
+
43
+
44
+ # Output response
45
+ out_response = gradio.Textbox(label="Generated Response")
46
+
47
+ # Gradio
48
+ iface = gradio.Interface(fn=generate_query_response,
49
+ inputs=[in_prompt],
50
+ outputs=out_response,
51
+ title = "Medical Summary",
52
+ description = "using fine-tune medQA gpt-2 model")
53
+
54
+
55
+ # YOUR CODE HERE to launch the interface
56
+ iface.launch(share = True)