Samp007 commited on
Commit
5361c84
1 Parent(s): ebfafb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -39
app.py CHANGED
@@ -1,43 +1,31 @@
1
  import streamlit as st
2
- import pandas as pd
3
- import plotly.express as px
4
- from transformers import pipeline, AutoTokenizer
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 and isinstance(text_input, str):
15
- tokenizer = AutoTokenizer.from_pretrained(model_name)
16
- encoded_input = tokenizer(text_input, return_tensors='pt', padding=True, truncation=True)
17
- sentiment_classifier = pipeline("sentiment-analysis", model=model_name)
18
- result = sentiment_classifier(encoded_input)[0]
19
- st.write(f"Sentiment: {result['label']}")
20
- st.write(f"Score: {round(result['score'], 4)}")
21
-
22
- # create a dataframe to hold the sentiment distribution
23
- df_sentiments = pd.DataFrame({"sentiment": ["Positive", "Negative", "Neutral"], "count": [0, 0, 0]})
24
-
25
- # get the sentiment distribution of the text using the selected model
26
- results = sentiment_classifier(encoded_input)
27
- for r in results:
28
- if r["label"] == "POSITIVE":
29
- df_sentiments.loc[0, "count"] = r["score"]
30
- elif r["label"] == "NEGATIVE":
31
- df_sentiments.loc[1, "count"] = r["score"]
32
- elif r["label"] == "NEUTRAL":
33
- df_sentiments.loc[2, "count"] = r["score"]
34
-
35
- # plot the sentiment distribution as a pie chart
36
- fig = px.pie(df_sentiments, values='count', names='sentiment', hole=.4)
37
- st.plotly_chart(fig)
38
-
39
- else:
40
- st.warning("Please enter some valid text for sentiment analysis")
41
 
42
- if __name__ == "__main__":
43
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
 
4
 
5
+ tokenizer = AutoTokenizer.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
6
+ model = AutoModelForSequenceClassification.from_pretrained("nlptown/bert-base-multilingual-uncased-sentiment")
 
 
 
 
7
 
8
+ st.title("Sentiment Analysis App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ text = st.text_input("Enter text to analyze:")
11
+ if st.button("Analyze"):
12
+ encoding = tokenizer.encode_plus(text, return_tensors="pt", padding=True, truncation=True)
13
+ input_ids = encoding["input_ids"]
14
+ attention_mask = encoding["attention_mask"]
15
+
16
+ with torch.no_grad():
17
+ output = model(input_ids, attention_mask)
18
+ prediction = int(torch.argmax(output.logits))
19
+
20
+ if prediction == 0:
21
+ st.write("Negative")
22
+ elif prediction == 1:
23
+ st.write("Neutral")
24
+ else:
25
+ st.write("Positive")
26
+
27
+ values = [output.logits[0][0].item(), output.logits[0][1].item(), output.logits[0][2].item()]
28
+ labels = ["Negative", "Neutral", "Positive"]
29
+ fig, ax = plt.subplots()
30
+ ax.bar(labels, values)
31
+ st.pyplot(fig)