Amy Vaughn commited on
Commit
b0272e3
1 Parent(s): b3cd114

sentiment analysis app

Browse files
Files changed (4) hide show
  1. __pycache__/engine.cpython-312.pyc +0 -0
  2. app.py +17 -0
  3. engine.py +49 -0
  4. requirements.txt +53 -0
__pycache__/engine.cpython-312.pyc ADDED
Binary file (2.21 kB). View file
 
app.py CHANGED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from engine import SentimentAnalyzer
2
+ import streamlit as st
3
+
4
+
5
+ # Load the sentiment analysis model from Hugging Face
6
+ sentiment_analysis = SentimentAnalyzer()
7
+
8
+ # Define the Streamlit app interface
9
+ st.title("User Sentiment Analysis")
10
+
11
+ sentence = st.text_input("Enter a sentence:")
12
+
13
+ # Perform sentiment analysis on the input sentence
14
+ if sentence:
15
+ label = sentiment_analysis.get_sentiment(sentence)
16
+ # Display the sentiment analysis result to the user
17
+ st.write(f"Sentiment analysis result: {label}")
engine.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+
4
+ class SentimentAnalyzer:
5
+ """Class for analyzing the sentiment of sentences
6
+ """
7
+
8
+ def __init__(self) -> None:
9
+ """initializes the class with sentiment analysis pipeline using the distilbert-base-uncased-finetuned-sst-2-english model
10
+ """
11
+ self.analyzer = pipeline(
12
+ "sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
13
+
14
+ def score_sentiment(self, sentence: str) -> float:
15
+ """Uses the analyzer to analyze the sentiment of the provided sentence
16
+
17
+ Parameters
18
+ ----------
19
+ sentence : str
20
+ a short sentence to be analyzed
21
+
22
+ Returns
23
+ -------
24
+ float
25
+ score of the sentiment from 0 to 1. Below 0.5 is negative, above is positive. 0.5 is neutral
26
+ """
27
+ return self.analyzer(sentence)[0]
28
+
29
+ def get_sentiment(self, sentence: str) -> str:
30
+ """returns the label of the sentiment provided
31
+
32
+ Parameters
33
+ ----------
34
+ sentence : str
35
+ a short sentence to be analyzed
36
+
37
+ Returns
38
+ -------
39
+ str
40
+ label of the sentiment wether it is positive, negative, or neutral
41
+ """
42
+ sentiment_score = self.score_sentiment(sentence)
43
+ return sentiment_score['label']
44
+
45
+
46
+ if __name__ == "__main__":
47
+ sentence = "I love you"
48
+ sentiment_analyzer = SentimentAnalyzer()
49
+ print(sentiment_analyzer.get_sentiment(sentence))
requirements.txt ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.2.0
2
+ attrs==23.2.0
3
+ blinker==1.7.0
4
+ cachetools==5.3.3
5
+ certifi==2023.7.22
6
+ charset-normalizer==3.3.2
7
+ click==8.1.7
8
+ filelock==3.13.1
9
+ fsspec==2024.3.1
10
+ gitdb==4.0.11
11
+ GitPython==3.1.42
12
+ huggingface-hub==0.21.4
13
+ idna==3.4
14
+ Jinja2==3.1.3
15
+ jsonschema==4.21.1
16
+ jsonschema-specifications==2023.12.1
17
+ markdown-it-py==3.0.0
18
+ MarkupSafe==2.1.5
19
+ mdurl==0.1.2
20
+ mpmath==1.3.0
21
+ networkx==3.2.1
22
+ numpy==1.26.4
23
+ packaging==23.2
24
+ pandas==2.2.1
25
+ pillow==10.2.0
26
+ protobuf==4.25.3
27
+ pyarrow==15.0.2
28
+ pydeck==0.8.1b0
29
+ Pygments==2.17.2
30
+ python-dateutil==2.9.0.post0
31
+ pytz==2024.1
32
+ PyYAML==6.0.1
33
+ referencing==0.34.0
34
+ regex==2023.12.25
35
+ requests==2.31.0
36
+ rich==13.7.1
37
+ rpds-py==0.18.0
38
+ safetensors==0.4.2
39
+ six==1.16.0
40
+ smmap==5.0.1
41
+ streamlit==1.32.2
42
+ sympy==1.12
43
+ tenacity==8.2.3
44
+ tokenizers==0.15.2
45
+ toml==0.10.2
46
+ toolz==0.12.1
47
+ torch==2.2.1
48
+ tornado==6.4
49
+ tqdm==4.66.2
50
+ transformers==4.39.1
51
+ typing_extensions==4.10.0
52
+ tzdata==2024.1
53
+ urllib3==2.0.7