Spaces:
No application file
No application file
Commit
·
d18eb75
1
Parent(s):
d3db004
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,47 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
-
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
5 |
|
|
|
6 |
models = {
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
}
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
return 'Negative'
|
18 |
-
else:
|
19 |
-
return 'Neutral'
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
if scores['compound'] > 0.5:
|
25 |
-
return 'Positive'
|
26 |
-
elif scores['compound'] < -0.5:
|
27 |
-
return 'Negative'
|
28 |
-
else:
|
29 |
-
return 'Neutral'
|
30 |
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
st.write(f'Sentiment: {sentiment}')
|
43 |
-
else:
|
44 |
-
model = models[model_name]
|
45 |
-
results = model(text, return_all_scores=True)
|
46 |
-
st.write('Results:')
|
47 |
-
for result in results:
|
48 |
-
label = result['label']
|
49 |
-
score = result['score']
|
50 |
-
if score > 0.2:
|
51 |
-
st.write(f'{label} (Score: {score:.2f})')
|
52 |
-
mixed_score = 1 - sum([r['score'] for r in results])
|
53 |
-
if mixed_score > 0.2:
|
54 |
-
st.write(f'Mixed (Score: {mixed_score:.2f})')
|
|
|
1 |
import streamlit as st
|
2 |
+
import transformers
|
3 |
+
import torch
|
|
|
4 |
|
5 |
+
# Define sentiment analysis models
|
6 |
models = {
|
7 |
+
"DistilBERT": transformers.pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english"),
|
8 |
+
"BERT": transformers.pipeline("sentiment-analysis", model="bert-base-uncased-finetuned-sst-2-english"),
|
9 |
+
"RoBERTa": transformers.pipeline("sentiment-analysis", model="roberta-base-openai-detector"),
|
10 |
}
|
11 |
|
12 |
+
# Define function to analyze sentiment using selected model
|
13 |
+
def analyze_sentiment(text, model_name):
|
14 |
+
model = models[model_name]
|
15 |
+
result = model(text)[0]
|
16 |
+
return result['label'], result['score']
|
|
|
|
|
|
|
17 |
|
18 |
+
# Define Streamlit app
|
19 |
+
def app():
|
20 |
+
st.title("Sentiment Analysis App")
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# User input
|
23 |
+
text = st.text_area("Enter text to analyze", max_chars=1024)
|
24 |
|
25 |
+
# Sentiment analysis
|
26 |
+
if st.button("Analyze"):
|
27 |
+
st.write("Analyzing sentiment...")
|
28 |
+
with st.spinner("Wait for it..."):
|
29 |
+
results = []
|
30 |
+
for model_name in models:
|
31 |
+
label, score = analyze_sentiment(text, model_name)
|
32 |
+
results.append((model_name, label, score))
|
33 |
+
st.success("Sentiment analysis complete!")
|
34 |
+
st.write("Results:")
|
35 |
+
for model_name, label, score in results:
|
36 |
+
st.write(f"- {model_name}: {label} ({score:.2f})")
|
37 |
|
38 |
+
# Advanced features
|
39 |
+
if st.beta_expander("Advanced Options", expanded=False):
|
40 |
+
model_name = st.selectbox("Select model", list(models.keys()))
|
41 |
+
if st.button("Test model"):
|
42 |
+
label, score = analyze_sentiment("This is a test.", model_name)
|
43 |
+
st.write(f"Test result: {label} ({score:.2f})")
|
44 |
|
45 |
+
# Run Streamlit app
|
46 |
+
if __name__ == "__main__":
|
47 |
+
app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|