Spaces:
Runtime error
Runtime error
Commit
·
4d54af4
1
Parent(s):
c1e316f
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import torch
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
model = FalconForSequenceClassification.from_pretrained("rocketknight1/falcon-rw-1b", num_labels=8)
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# Get last hidden state (i.e., output of final layer)
|
17 |
-
last_hidden_state = outputs.last_hidden_state[:, 0, :]
|
18 |
-
|
19 |
-
# Return predicted label
|
20 |
-
return torch.argmax(last_hidden_state).item() + 1
|
21 |
|
22 |
-
|
23 |
-
if __name__ == "__main__":
|
24 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
|
|
3 |
|
4 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
5 |
+
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=8)
|
|
|
6 |
|
7 |
+
def classify(text):
|
8 |
+
inputs = tokenizer(text, return_tensors='pt')
|
9 |
+
outputs = model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
10 |
+
logits = outputs[0].logits
|
11 |
+
probabilities = torch.softmax(logits, dim=-1).tolist()
|
12 |
+
predicted_label = np.argmax(probabilities)
|
13 |
+
return {'Label': predicted_label}
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
gr.Interface(fn=classify, inputs="text", outputs="json").launch()
|
|
|
|