Jainesh212 commited on
Commit
d18eb75
·
1 Parent(s): d3db004

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -44
app.py CHANGED
@@ -1,54 +1,47 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
- from textblob import TextBlob
4
- from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
5
 
 
6
  models = {
7
- 'DistilBERT': pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english'),
8
- 'TextBlob': None,
9
- 'VADER': None
10
  }
11
 
12
- def textblob_sentiment(text):
13
- polarity = TextBlob(text).sentiment.polarity
14
- if polarity > 0.2:
15
- return 'Positive'
16
- elif polarity < -0.2:
17
- return 'Negative'
18
- else:
19
- return 'Neutral'
20
 
21
- def vader_sentiment(text):
22
- analyzer = SentimentIntensityAnalyzer()
23
- scores = analyzer.polarity_scores(text)
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
- models['TextBlob'] = textblob_sentiment
32
- models['VADER'] = vader_sentiment
33
 
34
- st.title('Sentiment Analysis App')
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- text = st.text_input('Enter some text')
37
- model_name = st.selectbox('Select a model', list(models.keys()))
 
 
 
 
38
 
39
- if st.button('Predict'):
40
- if model_name == 'TextBlob' or model_name == 'VADER':
41
- sentiment = models[model_name](text)
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()