jbraha commited on
Commit
e57eba3
1 Parent(s): 967ef1c

debugging 3

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -10,13 +10,19 @@ st.title("Sentiment Analysis")
10
  def analyze(input, model):
11
  return "This is a sample output"
12
 
13
-
14
  # load my fine-tuned model
15
  fine_tuned = "jbraha/tweet-bert"
16
  labels = {'LABEL_0': 'toxic', 'LABEL_1': 'severe_toxic', 'LABEL_2': 'obscene', 'LABEL_3': 'threat',
17
  'LABEL_4': 'insult', 'LABEL_5': 'identity_hate'}
18
 
19
- # make a dictionary of the labels with keys like "LABEL_0" and values like "toxic"
 
 
 
 
 
 
 
20
 
21
  #text insert
22
  input = st.text_area("Insert text to be analyzed", value="Nice to see you today.",
@@ -32,25 +38,21 @@ option = st.selectbox(
32
  if option == 'Fine-Tuned':
33
  model = AutoModelForSequenceClassification.from_pretrained(fine_tuned)
34
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
35
- classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
36
  elif option == 'Roberta':
37
  model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
38
  tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
39
- classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer, top_k=None)
40
  else:
41
  classifier = pipeline('sentiment-analysis')
42
 
43
 
44
  if st.button('Analyze'):
45
  result = classifier(input)
46
- print(result)
47
- print(type(result))
48
  output = None
49
  result = result[0]
50
  if option == 'Fine-Tuned':
51
- output = {'Toxic': result['LABEL_0']}
52
- del result['LABEL_0']
53
- output[max(result, key=result.get)] = result[max(result, key=result.get)]
54
  else:
55
  output = result
56
  st.write(output)
@@ -60,3 +62,5 @@ else:
60
 
61
 
62
 
 
 
 
10
  def analyze(input, model):
11
  return "This is a sample output"
12
 
 
13
  # load my fine-tuned model
14
  fine_tuned = "jbraha/tweet-bert"
15
  labels = {'LABEL_0': 'toxic', 'LABEL_1': 'severe_toxic', 'LABEL_2': 'obscene', 'LABEL_3': 'threat',
16
  'LABEL_4': 'insult', 'LABEL_5': 'identity_hate'}
17
 
18
+
19
+ # make a dictionary of the labels and values
20
+ def unpack(result):
21
+ output = {}
22
+ for res in result:
23
+ output[labels[res['label']]] = res['score']
24
+ return output
25
+
26
 
27
  #text insert
28
  input = st.text_area("Insert text to be analyzed", value="Nice to see you today.",
 
38
  if option == 'Fine-Tuned':
39
  model = AutoModelForSequenceClassification.from_pretrained(fine_tuned)
40
  tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
41
+ classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer, top_k=None)
42
  elif option == 'Roberta':
43
  model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
44
  tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
45
+ classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
46
  else:
47
  classifier = pipeline('sentiment-analysis')
48
 
49
 
50
  if st.button('Analyze'):
51
  result = classifier(input)
 
 
52
  output = None
53
  result = result[0]
54
  if option == 'Fine-Tuned':
55
+ output = unpack(result)
 
 
56
  else:
57
  output = result
58
  st.write(output)
 
62
 
63
 
64
 
65
+
66
+