owaiskha9654
commited on
Commit
β’
0058211
1
Parent(s):
87148f5
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import numpy as np
|
2 |
import torch
|
3 |
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
|
4 |
-
from transformers import BertForSequenceClassification,BertTokenizer
|
5 |
|
6 |
import gradio as gr
|
7 |
from typing import Dict
|
8 |
|
9 |
|
10 |
num_labels=14
|
11 |
-
model = BertForSequenceClassification.from_pretrained("owaiskha9654/Multi-Label-Classification-of-PubMed-Articles", num_labels=num_labels)
|
12 |
-
tokenizer = BertTokenizer.from_pretrained('owaiskha9654/Multi-Label-Classification-of-PubMed-Articles', do_lower_case=True)
|
13 |
|
14 |
|
15 |
def Multi_Label_Classification_of_Pubmed_Articles(model_input: str) -> Dict[str, float]:
|
@@ -23,10 +23,10 @@ def Multi_Label_Classification_of_Pubmed_Articles(model_input: str) -> Dict[str,
|
|
23 |
test_attention_masks = test_encodings['attention_mask']
|
24 |
# Make tensors out of data
|
25 |
test_inputs = torch.tensor(test_input_ids)
|
26 |
-
test_labels = torch.tensor(test_labels)
|
27 |
test_masks = torch.tensor(test_attention_masks)
|
28 |
# Create test dataloader
|
29 |
-
test_data = TensorDataset(test_inputs, test_masks, test_labels,
|
30 |
test_sampler = SequentialSampler(test_data)
|
31 |
test_dataloader = DataLoader(test_data, sampler=test_sampler, batch_size=batch_size)
|
32 |
|
@@ -34,7 +34,7 @@ def Multi_Label_Classification_of_Pubmed_Articles(model_input: str) -> Dict[str,
|
|
34 |
model.eval()
|
35 |
|
36 |
#track variables
|
37 |
-
logit_preds,
|
38 |
|
39 |
# Predict
|
40 |
for i, batch in enumerate(test_dataloader):
|
@@ -53,15 +53,15 @@ def Multi_Label_Classification_of_Pubmed_Articles(model_input: str) -> Dict[str,
|
|
53 |
|
54 |
tokenized_texts.append(b_input_ids)
|
55 |
logit_preds.append(b_logit_pred)
|
56 |
-
true_labels.append(b_labels)
|
57 |
pred_labels.append(pred_label)
|
58 |
|
59 |
# Flatten outputs
|
60 |
tokenized_texts = [item for sublist in tokenized_texts for item in sublist]
|
61 |
pred_labels = [item for sublist in pred_labels for item in sublist]
|
62 |
-
|
63 |
# Converting flattened binary values to boolean values
|
64 |
-
|
65 |
|
66 |
|
67 |
#prediction = model.predict(tokenized)[0]
|
@@ -69,7 +69,7 @@ def Multi_Label_Classification_of_Pubmed_Articles(model_input: str) -> Dict[str,
|
|
69 |
# "negative": float(prediction[0]),
|
70 |
# "positive": float(prediction[1])
|
71 |
#}
|
72 |
-
return
|
73 |
|
74 |
|
75 |
model_input = gr.Textbox("Input text here", show_label=False)
|
@@ -127,4 +127,4 @@ app = gr.Interface(
|
|
127 |
analytics_enabled=False,
|
128 |
)
|
129 |
|
130 |
-
app.launch(
|
|
|
1 |
import numpy as np
|
2 |
import torch
|
3 |
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
|
4 |
+
# from transformers import BertForSequenceClassification,BertTokenizer
|
5 |
|
6 |
import gradio as gr
|
7 |
from typing import Dict
|
8 |
|
9 |
|
10 |
num_labels=14
|
11 |
+
#model = BertForSequenceClassification.from_pretrained("owaiskha9654/Multi-Label-Classification-of-PubMed-Articles", num_labels=num_labels)
|
12 |
+
#tokenizer = BertTokenizer.from_pretrained('owaiskha9654/Multi-Label-Classification-of-PubMed-Articles', do_lower_case=True)
|
13 |
|
14 |
|
15 |
def Multi_Label_Classification_of_Pubmed_Articles(model_input: str) -> Dict[str, float]:
|
|
|
23 |
test_attention_masks = test_encodings['attention_mask']
|
24 |
# Make tensors out of data
|
25 |
test_inputs = torch.tensor(test_input_ids)
|
26 |
+
#test_labels = torch.tensor(test_labels)
|
27 |
test_masks = torch.tensor(test_attention_masks)
|
28 |
# Create test dataloader
|
29 |
+
test_data = TensorDataset(test_inputs, test_masks, )#test_labels, test_token_types)
|
30 |
test_sampler = SequentialSampler(test_data)
|
31 |
test_dataloader = DataLoader(test_data, sampler=test_sampler, batch_size=batch_size)
|
32 |
|
|
|
34 |
model.eval()
|
35 |
|
36 |
#track variables
|
37 |
+
logit_preds,pred_labels,tokenized_texts = [],[],[]
|
38 |
|
39 |
# Predict
|
40 |
for i, batch in enumerate(test_dataloader):
|
|
|
53 |
|
54 |
tokenized_texts.append(b_input_ids)
|
55 |
logit_preds.append(b_logit_pred)
|
56 |
+
#true_labels.append(b_labels)
|
57 |
pred_labels.append(pred_label)
|
58 |
|
59 |
# Flatten outputs
|
60 |
tokenized_texts = [item for sublist in tokenized_texts for item in sublist]
|
61 |
pred_labels = [item for sublist in pred_labels for item in sublist]
|
62 |
+
# true_labels = [item for sublist in true_labels for item in sublist]
|
63 |
# Converting flattened binary values to boolean values
|
64 |
+
# true_bools = [tl==1 for tl in true_labels]
|
65 |
|
66 |
|
67 |
#prediction = model.predict(tokenized)[0]
|
|
|
69 |
# "negative": float(prediction[0]),
|
70 |
# "positive": float(prediction[1])
|
71 |
#}
|
72 |
+
return pred_labels
|
73 |
|
74 |
|
75 |
model_input = gr.Textbox("Input text here", show_label=False)
|
|
|
127 |
analytics_enabled=False,
|
128 |
)
|
129 |
|
130 |
+
app.launch(inline=True,share=True)
|