Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
def main():
|
7 |
+
st.set_page_config(page_title="Sentiment Analysis App")
|
8 |
+
st.sidebar.title("Sentiment Analysis App")
|
9 |
+
st.sidebar.write("Choose a pre-trained model for sentiment analysis")
|
10 |
+
model_name = st.sidebar.selectbox("Select Model", ["bert-base-uncased", "distilbert-base-uncased", "roberta-base"])
|
11 |
+
text_input = st.text_input("Enter text for sentiment analysis")
|
12 |
+
|
13 |
+
if st.button("Analyze"):
|
14 |
+
if text_input:
|
15 |
+
sentiment_classifier = pipeline("sentiment-analysis", model=model_name)
|
16 |
+
result = sentiment_classifier(text_input)[0]
|
17 |
+
st.write(f"Sentiment: {result['label']}")
|
18 |
+
st.write(f"Score: {round(result['score'], 4)}")
|
19 |
+
|
20 |
+
# create a dataframe to hold the sentiment distribution
|
21 |
+
df_sentiments = pd.DataFrame({"sentiment": ["Positive", "Negative", "Neutral"], "count": [0, 0, 0]})
|
22 |
+
|
23 |
+
# get the sentiment distribution of the text using the selected model
|
24 |
+
results = sentiment_classifier(text_input, task="sentiment-analysis")
|
25 |
+
for r in results:
|
26 |
+
if r["label"] == "POSITIVE":
|
27 |
+
df_sentiments.loc[0, "count"] = r["score"]
|
28 |
+
elif r["label"] == "NEGATIVE":
|
29 |
+
df_sentiments.loc[1, "count"] = r["score"]
|
30 |
+
elif r["label"] == "NEUTRAL":
|
31 |
+
df_sentiments.loc[2, "count"] = r["score"]
|
32 |
+
|
33 |
+
# plot the sentiment distribution as a pie chart
|
34 |
+
fig = px.pie(df_sentiments, values='count', names='sentiment', hole=.4)
|
35 |
+
st.plotly_chart(fig)
|
36 |
+
|
37 |
+
else:
|
38 |
+
st.warning("Please enter some text for sentiment analysis")
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
main()
|