APJ23 commited on
Commit
4894de4
1 Parent(s): a27a7e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -13,7 +13,6 @@ tweet = df['comment_text'][r.randint(0,1000)]
13
  tokenizer = AutoTokenizer.from_pretrained("APJ23/MultiHeaded_Sentiment_Analysis_Model")
14
  model = AutoModelForSequenceClassification.from_pretrained("APJ23/MultiHeaded_Sentiment_Analysis_Model")
15
 
16
- # Define the classes and their corresponding labels
17
  classes = {
18
  0: 'Non-Toxic',
19
  1: 'Toxic',
@@ -23,11 +22,14 @@ classes = {
23
  5: 'Insult',
24
  6: 'Identity Hate'
25
  }
26
-
27
- # Create a function to generate the toxicity predictions
28
  @st.cache(allow_output_mutation=True)
29
-
30
- # Create a table to display the toxicity predictions
 
 
 
 
 
31
  def create_table(predictions):
32
  data = {'Tweet': [], 'Highest Toxicity Class': [], 'Probability': []}
33
  for tweet, prediction in predictions.items():
@@ -38,13 +40,11 @@ def create_table(predictions):
38
  return df
39
 
40
  st.title('Toxicity Prediction App')
41
- tweet_input = st.text_input('Enter a tweet:')
42
  if st.button('Predict'):
43
- predicted_class_label, predicted_prob = predict_toxicity(tweet_input, model, tokenizer)
44
  prediction_text = f'Prediction: {predicted_class_label} ({predicted_prob:.2f})'
45
  st.write(prediction_text)
46
-
47
- # Display the toxicity predictions in a table
48
  predictions = {tweet_input: (predicted_class_label, predicted_prob)}
49
  table = create_table(predictions)
50
  st.table(table)
 
13
  tokenizer = AutoTokenizer.from_pretrained("APJ23/MultiHeaded_Sentiment_Analysis_Model")
14
  model = AutoModelForSequenceClassification.from_pretrained("APJ23/MultiHeaded_Sentiment_Analysis_Model")
15
 
 
16
  classes = {
17
  0: 'Non-Toxic',
18
  1: 'Toxic',
 
22
  5: 'Insult',
23
  6: 'Identity Hate'
24
  }
 
 
25
  @st.cache(allow_output_mutation=True)
26
+ def prediction(tweet,model,tokenizer):
27
+ inputs = tokenizer(tweet, return_tensors="pt", padding=True, truncation=True)
28
+ outputs = model(**inputs)
29
+ predicted_class = torch.argmax(outputs.logits, dim=1)
30
+ predicted_prob = torch.softmax(outputs.logits, dim=1)[0][predicted_class].item()
31
+ return classes[predicted_class], predicted_prob
32
+
33
  def create_table(predictions):
34
  data = {'Tweet': [], 'Highest Toxicity Class': [], 'Probability': []}
35
  for tweet, prediction in predictions.items():
 
40
  return df
41
 
42
  st.title('Toxicity Prediction App')
43
+ st.write(f'The random tweet select is {tweet}',tweet)
44
  if st.button('Predict'):
45
+ predicted_class_label, predicted_prob = predict_toxicity(tweet, model, tokenizer)
46
  prediction_text = f'Prediction: {predicted_class_label} ({predicted_prob:.2f})'
47
  st.write(prediction_text)
 
 
48
  predictions = {tweet_input: (predicted_class_label, predicted_prob)}
49
  table = create_table(predictions)
50
  st.table(table)