KojoKesse commited on
Commit
664f368
·
1 Parent(s): a90b9e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -1
app.py CHANGED
@@ -1,3 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Requirements
2
  model_path = f"avichr/heBERT_sentiment_analysis"
3
  tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
@@ -35,4 +97,5 @@ demo = gr.Interface(
35
  outputs="label",
36
  examples=[["This is something!"]])
37
 
38
- demo.launch()
 
 
1
+ !pip install -q transformers datasets gradio
2
+
3
+ from transformers import AutoModelForSequenceClassification
4
+ from transformers import TFAutoModelForSequenceClassification
5
+ from transformers import AutoTokenizer, AutoConfig
6
+ import numpy as np
7
+ from scipy.special import softmax
8
+
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
11
+
12
+ model_path = f"avichr/heBERT_sentiment_analysis"
13
+ config = AutoConfig.from_pretrained(model_path)
14
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
15
+
16
+ # Preprocess text (username and link placeholders)
17
+ def preprocess(text):
18
+ new_text = []
19
+ for t in text.split(" "):
20
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
21
+ t = 'http' if t.startswith('http') else t
22
+ new_text.append(t)
23
+ return " ".join(new_text)
24
+
25
+ # Input preprocessing
26
+ text = "Covid cases are increasing fast!"
27
+ text = preprocess(text)
28
+
29
+ # PyTorch-based models
30
+ encoded_input = tokenizer(text, return_tensors='pt')
31
+ output = model(**encoded_input)
32
+ scores = output[0][0].detach().numpy()
33
+ scores = softmax(scores)
34
+
35
+ # TensorFlow-based models
36
+ # model = TFAutoModelForSequenceClassification.from_pretrained(model_path)
37
+ # model.save_pretrained(model_path)
38
+ # text = "Covid cases are increasing fast!"
39
+ # encoded_input = tokenizer(text, return_tensors='tf')
40
+ # output = model(encoded_input)
41
+ # scores = output[0][0].numpy()
42
+ # scores = softmax(scores)
43
+
44
+ config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
45
+
46
+ # Print labels and scores
47
+ ranking = np.argsort(scores)
48
+ ranking = ranking[::-1]
49
+ print(f"Classified text: {text}")
50
+ for i in range(scores.shape[0]):
51
+ l = config.id2label[ranking[i]]
52
+ s = scores[ranking[i]]
53
+ print(f"{i+1}) {l} {np.round(float(s), 4)}")
54
+
55
+ from transformers import AutoModelForSequenceClassification
56
+ from transformers import TFAutoModelForSequenceClassification
57
+ from transformers import AutoTokenizer, AutoConfig
58
+ from scipy.special import softmax
59
+ import gradio as gr
60
+
61
+
62
+
63
  # Requirements
64
  model_path = f"avichr/heBERT_sentiment_analysis"
65
  tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
 
97
  outputs="label",
98
  examples=[["This is something!"]])
99
 
100
+ demo.launch()
101
+