benliang99 commited on
Commit
55bdad4
1 Parent(s): 62d4f49

Added Streamlit app and Huggingface Spaces yaml

Browse files
Files changed (2) hide show
  1. README.md +12 -0
  2. app.py.txt +53 -0
README.md CHANGED
@@ -1,6 +1,18 @@
1
  # cs-gy-6613-project
2
  Benjamin Liang's AI Project
3
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  Installation Steps
5
 
6
 
 
1
  # cs-gy-6613-project
2
  Benjamin Liang's AI Project
3
 
4
+ # Milestone 2
5
+ ---
6
+ title: Sentiment Analysis App
7
+ emoji: 🤖
8
+ colorFrom: pink
9
+ colorTo: purple
10
+ sdk: streamlit
11
+ sdk_version: 1.17.0
12
+ app_file: app.py
13
+ pinned: false
14
+ ---
15
+
16
  Installation Steps
17
 
18
 
app.py.txt 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 = NULL, NULL
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 model is NULL and tokenizer is NULL:
30
+ print("Model not loaded correctly. Try again!")
31
+
32
+ return model, tokenizer
33
+
34
+ def sentiment_analysis(model, tokenizer):
35
+ if tokenizer not NULL:
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'])