nkap360 commited on
Commit ·
85fd325
1
Parent(s): cc6a6ed
sentiment analysis app
Browse files- __pycache__/engine.cpython-310.pyc +0 -0
- app.py +17 -0
- engine.py +49 -0
- requirements.txt +0 -0
__pycache__/engine.cpython-310.pyc
ADDED
|
Binary file (1.92 kB). View file
|
|
|
app.py
ADDED
|
@@ -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 ... you"
|
| 48 |
+
sentiment_analyzer = SentimentAnalyzer()
|
| 49 |
+
print(sentiment_analyzer.get_sentiment(sentence))
|
requirements.txt
ADDED
|
Binary file (22.4 kB). View file
|
|
|