RICHARDMENSAH commited on
Commit
8454de8
1 Parent(s): a39c327

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -65
app.py CHANGED
@@ -1,74 +1,68 @@
1
- # import streamlit as st
2
- # import transformers
3
- # import torch
4
 
5
- # # Load the model and tokenizer
6
- # #model = transformers.AutoModelForSequenceClassification.from_pretrained("RICHARDMENSAH/twitter_xlm_roberta_base")
7
- # # tokenizer = transformers.AutoTokenizer.from_pretrained("RICHARDMENSAH/twitter_xlm_roberta_base")
8
 
9
- # model_name = "RICHARDMENSAH/twitter_xlm_roberta_base"
10
- # tokenizer_name = "RICHARDMENSAH/twitter_xlm_roberta_base"
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # model = transformers.pipeline("text-classification", model=model_name, tokenizer=tokenizer_name)
 
 
 
 
 
 
13
 
14
- # # Define the function for sentiment analysis
15
- # @st.cache_resource
16
- # def predict_sentiment(text):
17
- # result = model(text)
18
- # labell = result[0]['label']
19
- # if labell == 'label_0':
20
- # sentiment = 'Negative'
21
- # elif labell == 'label_1':
22
- # sentiment = 'Neutral'
23
- # elif labell == 'label_2':
24
- # sentiment = 'Positive'
25
- # else: sentiment = labell
26
- # score = result[0]['score']
27
- # return sentiment, score
28
 
29
- # # Setting the page configurations
30
- # st.set_page_config(
31
- # page_title="Sentiment Analysis App",
32
- # page_icon=":smile:",
33
- # layout="wide",
34
- # initial_sidebar_state="auto",
35
- # )
36
 
37
- # # Add description and title
38
- # st.write("""
39
- # # How Positive or Negative is your Text?
40
- # Enter some text and we'll tell you if it has a positive, negative, or neutral sentiment!
41
- # """ )
42
 
43
- # # Add image
44
- # image = st.image("https://i0.wp.com/thedatascientist.com/wp-content/uploads/2018/10/sentiment-analysis.png", width=400)
 
 
 
 
 
45
 
46
- # # Get user input
47
- # text = st.text_input("Enter some text here:")
 
 
 
 
 
48
 
49
- # # Define the CSS style for the app
50
- # st.markdown(
51
- # """
52
- # <style>
53
- # body {
54
- # background-color: #f5f5f5;
55
- # }
56
-
57
- # h1 {
58
- # color: #4e79a7;
59
- # }
60
- # </style>
61
- # """,
62
- # unsafe_allow_html=True
63
- # )
64
-
65
-
66
- # # Show sentiment output
67
- # if text:
68
- # sentiment, score = predict_sentiment(text)
69
- # if sentiment == "Positive":
70
- # st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
71
- # elif sentiment == "Negative":
72
- # st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
73
- # else:
74
- # st.warning(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
 
1
+ import streamlit as st
2
+ import transformers
3
+ import torch
4
 
5
+ # Load the model and tokenizer
6
+ model = transformers.AutoModelForSequenceClassification.from_pretrained("RICHARDMENSAH/twitter_xlm_roberta_base")
7
+ tokenizer = transformers.AutoTokenizer.from_pretrained("RICHARDMENSAH/twitter_xlm_roberta_base")
8
 
9
+ # Define the function for sentiment analysis
10
+ def predict_sentiment(text):
11
+ result = model([text])
12
+ labell = result[0]['label']
13
+ if labell == 'label_0':
14
+ sentiment = 'Negative'
15
+ elif labell == 'label_1':
16
+ sentiment = 'Neutral'
17
+ elif labell == 'label_2':
18
+ sentiment = 'Positive'
19
+ else:
20
+ sentiment = labell
21
+ score = result[0]['score']
22
+ return sentiment, score
23
 
24
+ # Setting the page configurations
25
+ st.set_page_config(
26
+ page_title="Sentiment Analysis App",
27
+ page_icon=":smile:",
28
+ layout="wide",
29
+ initial_sidebar_state="auto",
30
+ )
31
 
32
+ # Add description and title
33
+ st.write("""
34
+ # How Positive or Negative is your Text?
35
+ Enter some text and we'll tell you if it has a positive, negative, or neutral sentiment!
36
+ """ )
 
 
 
 
 
 
 
 
 
37
 
38
+ # Add image
39
+ image = st.image("https://i0.wp.com/thedatascientist.com/wp-content/uploads/2018/10/sentiment-analysis.png", width=400)
 
 
 
 
 
40
 
41
+ # Get user input
42
+ text = st.text_input("Enter some text here:")
 
 
 
43
 
44
+ # Define the CSS style for the app
45
+ st.markdown(
46
+ """
47
+ <style>
48
+ body {
49
+ background-color: #f5f5f5;
50
+ }
51
 
52
+ h1 {
53
+ color: #4e79a7;
54
+ }
55
+ </style>
56
+ """,
57
+ unsafe_allow_html=True
58
+ )
59
 
60
+ # Show sentiment output
61
+ if text:
62
+ sentiment, score = predict_sentiment(text)
63
+ if sentiment == "Positive":
64
+ st.success(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
65
+ elif sentiment == "Negative":
66
+ st.error(f"The sentiment is {sentiment} with a score of {score*100:.2f}%!")
67
+ else:
68
+ st.warning(f"The sentiment is {sentiment} with a score of {score*100:.