CodyJiang commited on
Commit
58f100b
·
1 Parent(s): 1dbf49e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -1,20 +1,21 @@
1
  import streamlit as st
2
  import transformers
3
  import torch
 
4
 
5
- st.title("Sentiment Analysis App")
6
 
7
  # Set default text
8
  default_text = "Cody Jiang is a fantastic student in CS-UY-4613!"
9
 
10
  # Select pretrained model
11
- model_names = ['model', 'distilbert-base-uncased-finetuned-sst-2-english', 'bert-base-uncased', 'roberta-base']
12
  model_name = st.selectbox("Select a pretrained model", model_names)
13
 
14
  # Load selected model
15
- if model_name == 'model':
16
- tokenizer = transformers.DistilBertTokenizerFast.from_pretrained('./model/')
17
- model = transformers.DistilBertForSequenceClassification.from_pretrained('./model/')
18
  else:
19
  tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
20
  model = transformers.AutoModelForSequenceClassification.from_pretrained(model_name)
@@ -22,10 +23,10 @@ else:
22
  # Set device
23
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
24
 
25
- # Define sentiment labels
26
- sentiment_labels = ['Negative', 'Positive']
27
 
28
- # Analyze sentiment
29
  if st.button("Submit"):
30
  with st.spinner("Analyzing..."):
31
  text = st.text_input("Enter text to analyze", default_text)
@@ -33,7 +34,8 @@ if st.button("Submit"):
33
  inputs = inputs.to(device)
34
  outputs = model(**inputs)
35
  logits = outputs.logits
36
- predictions = torch.argmax(logits, dim=1).cpu().numpy()
37
- sentiment = sentiment_labels[predictions[0]]
 
38
 
39
- st.success(f"Sentiment: {sentiment}")
 
1
  import streamlit as st
2
  import transformers
3
  import torch
4
+ import pandas as pd
5
 
6
+ st.title("Toxicity Classification App")
7
 
8
  # Set default text
9
  default_text = "Cody Jiang is a fantastic student in CS-UY-4613!"
10
 
11
  # Select pretrained model
12
+ model_names = ['distilbert-base-uncased-finetuned-sst-2-english', 'bert-base-uncased', 'roberta-base', 'Codys Finetuning Language Model']
13
  model_name = st.selectbox("Select a pretrained model", model_names)
14
 
15
  # Load selected model
16
+ if model_name == 'Codys Finetuning Language Model':
17
+ tokenizer = transformers.AutoTokenizer.from_pretrained('./model/')
18
+ model = transformers.AutoModelForSequenceClassification.from_pretrained('./model/')
19
  else:
20
  tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
21
  model = transformers.AutoModelForSequenceClassification.from_pretrained(model_name)
 
23
  # Set device
24
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25
 
26
+ # Define toxicity labels
27
+ toxicity_labels = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
28
 
29
+ # Analyze toxicity
30
  if st.button("Submit"):
31
  with st.spinner("Analyzing..."):
32
  text = st.text_input("Enter text to analyze", default_text)
 
34
  inputs = inputs.to(device)
35
  outputs = model(**inputs)
36
  logits = outputs.logits
37
+ predictions = torch.sigmoid(logits).cpu().detach().numpy()[0]
38
+ results = pd.DataFrame({'Toxicity Class': toxicity_labels, 'Probability': predictions})
39
+ results = results.sort_values('Probability', ascending=False)
40
 
41
+ st.success(f"Toxicity Probabilities: \n{results.to_string(index=False)}")