Benjamin S Liang commited on
Commit
df50e06
2 Parent(s): 5e244ec 0a8e2bc

Merge branch 'milestone-2'

Browse files
Files changed (3) hide show
  1. README.md +12 -1
  2. app.py +53 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,6 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
1
  # cs-gy-6613-project
2
  Benjamin Liang's AI Project
3
-
4
  Installation Steps
5
 
6
 
 
1
+ ---
2
+ title: Sentiment Analysis App
3
+ emoji: 🤖
4
+ colorFrom: pink
5
+ colorTo: purple
6
+ sdk: streamlit
7
+ sdk_version: 1.17.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
  # cs-gy-6613-project
13
  Benjamin Liang's AI Project
14
+
15
  Installation Steps
16
 
17
 
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer, TFAutoModelForSequenceClassification, DistillbertForSequenceClassification
3
+
4
+ # Options for models from transformers library
5
+ MODEL_OPTS = ['default', 'bertweet-base-sentiment-analysis', 'twitter-roberta-base', 'distilRoberta-financial-sentiment']
6
+ DEFAULT_OPT = MODEL_OPTS[0]
7
+
8
+ # returns loaded model and tokenizer, if any
9
+ def load_model(opt):
10
+ if opt not in MODEL_OPTS: print("Incorrect model selection. Try again!")
11
+ model, tokenizer = None, None
12
+
13
+ # Load the chosen sentiment analysis model from transformers
14
+ if opt == DEFAULT_OPT:
15
+ return pipeline("sentiment-analysis"), tokenizer
16
+ elif opt == 'bertweet-base-sentiment-analysis':
17
+ tokenizer = AutoTokenizer.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
18
+ model = AutoModelForSequenceClassification.from_pretrained("finiteautomata/bertweet-base-sentiment-analysis")
19
+ elif opt == 'twitter-roberta-base-sentiment':
20
+ tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
21
+ model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
22
+ elif opt == 'distilRoberta-financial-sentiment'
23
+ tokenizer = AutoTokenizer.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
24
+ model = AutoModelForSequenceClassification.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
25
+ elif opt == 'bert-base-multilingual-uncased-sentiment ':
26
+ tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
27
+ model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncas
28
+
29
+ elif not model and not tokenizer:
30
+ print("Model not loaded correctly. Try again!")
31
+
32
+ return model, tokenizer
33
+
34
+ def sentiment_analysis(model, tokenizer):
35
+ if tokenizer:
36
+ return pipeline('text-classification', model=model, tokenizer=tokenizer)
37
+ else: return pipeline('text-classification', model=model)
38
+
39
+ # Title the Streamlit app 'Sentiment Analysis'
40
+ st.title('Sentiment Analysis')
41
+
42
+ # Take in user input
43
+ user_text = st.text_input('Input text to perform sentiment analysis on here.')
44
+
45
+ # The user can interact with a dropdown menu to choose a sentiment analysis model.
46
+ dropdown_value = st.selectbox('Select one of the following sentiment analysis models', MODEL_OPTS, index=MODEL_OPTS.index(DEFAULT_OPT))
47
+ model, tokenizer = load_model(dropdown_value)
48
+
49
+ # Perform sentiment analysis on the user's input
50
+ result = sentiment_analysis(text_input)
51
+
52
+ # Display the sentiment analysis results
53
+ st.write('Sentiment:', result[0]['label'], '; Score:', result[0]['score'])
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ streamlit