Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""gradio_deploy.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
"""
|
7 |
+
import os
|
8 |
+
import gradio as gr
|
9 |
+
from PIL import Image
|
10 |
+
from timeit import default_timer as timer
|
11 |
+
from tensorflow import keras
|
12 |
+
import numpy as np
|
13 |
+
|
14 |
+
loaded_model = AutoModelWithLMHead.from_pretrained("runaksh/medquad-finetuned-gpt2"))
|
15 |
+
loaded_tokenizer = AutoTokenizer.from_pretrained("runaksh/medquad-finetuned-gpt2")
|
16 |
+
|
17 |
+
def generate_query_response(prompt, max_length=200):
|
18 |
+
|
19 |
+
model = loaded_model
|
20 |
+
tokenizer = loaded_tokenizer
|
21 |
+
|
22 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
23 |
+
|
24 |
+
attention_mask = torch.ones_like(input_ids)
|
25 |
+
pad_token_id = tokenizer.eos_token_id
|
26 |
+
|
27 |
+
output = model.generate(
|
28 |
+
input_ids,
|
29 |
+
max_length=max_length,
|
30 |
+
num_return_sequences=1,
|
31 |
+
attention_mask=attention_mask,
|
32 |
+
pad_token_id=pad_token_id
|
33 |
+
)
|
34 |
+
|
35 |
+
return tokenizer.decode(output[0])
|
36 |
+
|
37 |
+
# Gradio elements
|
38 |
+
|
39 |
+
# Input from user
|
40 |
+
in_prompt = gradio.inputs.Textbox(lines=2, label='Enter the question')
|
41 |
+
in_max_length = gradio.inputs.Number(label='Enter the max length')
|
42 |
+
|
43 |
+
# Output response
|
44 |
+
out_response = gradio.outputs.Textbox(label='Answer')
|
45 |
+
|
46 |
+
# Gradio interface to generate UI link
|
47 |
+
iface = gradio.Interface(fn=generate_query_response,
|
48 |
+
inputs = [in_prompt,in_max_length],
|
49 |
+
outputs = out_response
|
50 |
+
)
|
51 |
+
|
52 |
+
iface.launch(debug = True)
|
53 |
+
|