File size: 1,400 Bytes
5d34d7c
 
 
 
 
d430ce1
5d34d7c
 
02050c0
 
 
5d34d7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d430ce1
 
 
 
 
 
 
 
 
 
 
 
5d34d7c
d430ce1
63b3859
d430ce1
5d34d7c
 
99116b6
5d34d7c
 
d430ce1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import tensorflow as tf
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
import gradio as gr
import numpy as np
from scipy.special import softmax
from transformers import TFAutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained('roberta-base')
access_token = "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"
model = TFAutoModelForSequenceClassification.from_pretrained("ilan541/ssid_classification", use_auth_token=access_token)

def get_probs(text):
  inp = tokenizer(text,
                  truncation=True,
                  padding='max_length',
                  max_length=30,
                  return_tensors='tf')
  y_pred = model(inp)

  return y_pred.logits


def predict(your_text):
  
  y_pred_logits = get_probs(your_text)
  labels = ['Bus', 'Portable Device', 'Stationary Device', 'Vehicle']
  num_labels = len(labels)

  # # print logits
  # for i in range(num_labels):
  #   print(f"logit - {labels[i]}: {y_pred_logits[:,i][0]:.4f}")

  # print('\n')

  # # print probas
  y_probs = softmax(y_pred_logits)[0]
  # for i in range(num_labels):
  #   print(f"prob - {labels[i]}: {y_probs[i]:.4f}")
  
  # print('\n')
  #

  # print predicted class
  i = np.argmax(y_pred_logits)
  return f"Predicted class: {labels[i]} with proba {y_probs[i]:.4f}\n{labels}{y_probs}"

iface = gr.Interface(fn=predict, inputs="text", outputs="text")
iface.launch()