Spaces:
Runtime error
Runtime error
chsubhasis
commited on
Commit
·
d38220b
1
Parent(s):
63ba1e0
app and requirements files added
Browse files- app.py +57 -4
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,7 +1,60 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
-
return "Hello " + name + "!!"
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelWithLMHead, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel, DataCollatorForLanguageModeling
|
5 |
|
6 |
+
#def greet(name):
|
7 |
+
# return "Hello " + name + "!!"
|
8 |
+
username = "chsubhasis"
|
9 |
+
my_repo = "Medical-QnA-gpt2"
|
10 |
+
my_checkpoint = username + '/' + my_repo
|
11 |
+
loaded_model = AutoModelWithLMHead.from_pretrained(my_checkpoint)
|
12 |
+
loaded_tokenizer = AutoTokenizer.from_pretrained(my_checkpoint)
|
13 |
|
14 |
+
|
15 |
+
def generate_response(model, tokenizer, prompt, max_length=200):
|
16 |
+
|
17 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
18 |
+
|
19 |
+
# Check the device of the model
|
20 |
+
device = next(model.parameters()).device
|
21 |
+
|
22 |
+
# Move input_ids to the same device as the model
|
23 |
+
input_ids = input_ids.to(device)
|
24 |
+
|
25 |
+
# Create the attention mask and pad token id
|
26 |
+
attention_mask = torch.ones_like(input_ids)
|
27 |
+
pad_token_id = tokenizer.eos_token_id
|
28 |
+
|
29 |
+
output = model.generate(
|
30 |
+
input_ids,
|
31 |
+
max_length=max_length,
|
32 |
+
num_return_sequences=1,
|
33 |
+
attention_mask=attention_mask,
|
34 |
+
pad_token_id=pad_token_id
|
35 |
+
)
|
36 |
+
|
37 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
38 |
+
|
39 |
+
def generate_query_response(prompt, max_length=200):
|
40 |
+
|
41 |
+
model = loaded_model
|
42 |
+
tokenizer = loaded_tokenizer
|
43 |
+
|
44 |
+
return generate_response(model, tokenizer, prompt, max_length)
|
45 |
+
|
46 |
+
#demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
47 |
+
#demo.launch()
|
48 |
+
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=generate_query_response,
|
51 |
+
inputs=[
|
52 |
+
gr.Textbox(lines=2, placeholder="Enter your medical query here..."),
|
53 |
+
gr.Slider(minimum=50, maximum=500, value=200, label="Maximum Length")
|
54 |
+
],
|
55 |
+
outputs="text",
|
56 |
+
title="Medical Question Answering Bot",
|
57 |
+
description="Ask your medical questions to get relevant answers."
|
58 |
+
)
|
59 |
+
|
60 |
+
iface.launch(share=True)
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1 |
# Dependencies for application are to be added to this file
|
2 |
|
3 |
-
numpy
|
|
|
|
|
|
1 |
# Dependencies for application are to be added to this file
|
2 |
|
3 |
+
numpy
|
4 |
+
gradio
|
5 |
+
tensorflow
|