corykhal commited on
Commit
aa28f7a
1 Parent(s): a057f42

Added more documentation

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -6,12 +6,15 @@ from transformers import BertTokenizerFast
6
 
7
  st.set_page_config(page_title="AI Project", page_icon=":tada:", layout="wide")
8
 
 
9
  pre_trained_models = ["finiteautomata/bertweet-base-sentiment-analysis",
10
  'bhadresh-savani/distilbert-base-uncased-emotion',
11
  "nlptown/bert-base-multilingual-uncased-sentiment"]
12
 
 
13
  fine_tuned_model = "corykhal/twitter-finetuned"
14
 
 
15
  fine_tuned_labels = {"LABEL_0": "Toxic",
16
  "LABEL_1": "Severe Toxic",
17
  "LABEL_2": "Obscene",
@@ -19,6 +22,7 @@ fine_tuned_labels = {"LABEL_0": "Toxic",
19
  "LABEL_4": "Insult",
20
  "LABEL_5": "Identity Hate"}
21
 
 
22
  models = pre_trained_models + [fine_tuned_model]
23
 
24
  with st.container():
@@ -28,6 +32,7 @@ with st.container():
28
  with st.container():
29
  st.write("---")
30
 
 
31
  text = st.text_input("Please enter the text of a tweet to use for the sentiment analysis:",
32
  value="Hello! It is a pleasure to meet you!")
33
 
@@ -36,28 +41,33 @@ with st.container():
36
  st.write("The first 3 models in the select box are for any sentiment analysis.")
37
  st.write("The last model (which is finetuned) in the select box is specifically for toxicity analysis.")
38
 
 
39
  model = st.selectbox("Please select one of the following pre-trained models:", models)
40
 
41
 
42
-
43
  with st.container():
44
  st.write("---")
 
45
  if st.button("Analyze!"):
46
  # If the user selects a pre-trained model
47
  if model in pre_trained_models:
 
48
  analysis = pipeline("sentiment-analysis", model=model)
 
49
  result = analysis(text)
 
50
  sentiment = result[0]["label"]
51
  score = result[0]["score"]
52
 
53
  # Create a table for the label and score
54
  data = pd.DataFrame({"Tweet": [text], "Sentiment": [sentiment], "Confidence Score": [score]})
55
-
56
  st.table(data)
57
 
58
  # If the user selects the finetuned model
59
  else:
 
60
  analysis = pipeline("sentiment-analysis", model=model, top_k=2)
 
61
  result = analysis(text)
62
 
63
  # Get the top two labels and scores
@@ -70,8 +80,6 @@ with st.container():
70
  # Create a table for the labels and scores
71
  data = pd.DataFrame({"Tweet": [text], "Toxic": [sentiment1], "Toxic Confidence Score": [score1],
72
  "Type of Toxicity": [sentiment2], "Toxicity Type Confidence Score": [score2]})
73
-
74
-
75
  st.table(data)
76
 
77
 
 
6
 
7
  st.set_page_config(page_title="AI Project", page_icon=":tada:", layout="wide")
8
 
9
+ # Defining the list of pre-trained models from other people's HuggingFace spaces
10
  pre_trained_models = ["finiteautomata/bertweet-base-sentiment-analysis",
11
  'bhadresh-savani/distilbert-base-uncased-emotion',
12
  "nlptown/bert-base-multilingual-uncased-sentiment"]
13
 
14
+ # My fine-tuned model from my personal HuggingFace space
15
  fine_tuned_model = "corykhal/twitter-finetuned"
16
 
17
+ # Dictionary that maps the output labels of my fine-tuned model to the actual labels to present
18
  fine_tuned_labels = {"LABEL_0": "Toxic",
19
  "LABEL_1": "Severe Toxic",
20
  "LABEL_2": "Obscene",
 
22
  "LABEL_4": "Insult",
23
  "LABEL_5": "Identity Hate"}
24
 
25
+ # List of all four models
26
  models = pre_trained_models + [fine_tuned_model]
27
 
28
  with st.container():
 
32
  with st.container():
33
  st.write("---")
34
 
35
+ # Take in user text to input into model
36
  text = st.text_input("Please enter the text of a tweet to use for the sentiment analysis:",
37
  value="Hello! It is a pleasure to meet you!")
38
 
 
41
  st.write("The first 3 models in the select box are for any sentiment analysis.")
42
  st.write("The last model (which is finetuned) in the select box is specifically for toxicity analysis.")
43
 
44
+ # Display a select box for the user to select a model name
45
  model = st.selectbox("Please select one of the following pre-trained models:", models)
46
 
47
 
 
48
  with st.container():
49
  st.write("---")
50
+ # If the button is pressed
51
  if st.button("Analyze!"):
52
  # If the user selects a pre-trained model
53
  if model in pre_trained_models:
54
+ # Load the model pipeline using the model name
55
  analysis = pipeline("sentiment-analysis", model=model)
56
+ # Run the model using user input text and store the predicted result
57
  result = analysis(text)
58
+ # Extract the label and score of prediction
59
  sentiment = result[0]["label"]
60
  score = result[0]["score"]
61
 
62
  # Create a table for the label and score
63
  data = pd.DataFrame({"Tweet": [text], "Sentiment": [sentiment], "Confidence Score": [score]})
 
64
  st.table(data)
65
 
66
  # If the user selects the finetuned model
67
  else:
68
+ # Load the model pipeline using the model name and return the top 2 results
69
  analysis = pipeline("sentiment-analysis", model=model, top_k=2)
70
+ # Run the model using user input text and store the predicted result
71
  result = analysis(text)
72
 
73
  # Get the top two labels and scores
 
80
  # Create a table for the labels and scores
81
  data = pd.DataFrame({"Tweet": [text], "Toxic": [sentiment1], "Toxic Confidence Score": [score1],
82
  "Type of Toxicity": [sentiment2], "Toxicity Type Confidence Score": [score2]})
 
 
83
  st.table(data)
84
 
85