Create sentiment_analysis_using_transformers.py
Browse files
sentiment_analysis_using_transformers.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
st.title('Sentiment Analyser App')
|
5 |
+
st.write('This app uses the Hugging Face Transformers [sentiment analyser](https://huggingface.co/course/chapter1/3?fw=tf) library to clasify the sentiment of your input as postive or negative. The web app is built using [Streamlit](https://docs.streamlit.io/en/stable/getting_started.html).')
|
6 |
+
st.write(
|
7 |
+
'To find out how this app was developed, please check out my [Medium post](https://medium.com/@rtkilian/deploy-and-share-your-sentiment-analysis-app-using-streamlit-sharing-2ba3ca6a3ead). To see my source code, have a look at my [GitHub repo](https://github.com/rtkilian/streamlit-huggingface).')
|
8 |
+
|
9 |
+
st.write('*Note: it will take up to 30 seconds to run the app.*')
|
10 |
+
|
11 |
+
form = st.form(key='sentiment-form')
|
12 |
+
user_input = form.text_area('Enter your text')
|
13 |
+
submit = form.form_submit_button('Submit')
|
14 |
+
|
15 |
+
if submit:
|
16 |
+
classifier = pipeline("sentiment-analysis")
|
17 |
+
result = classifier(user_input)[0]
|
18 |
+
label = result['label']
|
19 |
+
score = result['score']
|
20 |
+
|
21 |
+
if label == 'POSITIVE':
|
22 |
+
st.success(f'{label} sentiment (score: {score})')
|
23 |
+
else:
|
24 |
+
st.error(f'{label} sentiment (score: {score})')
|