violetteallotey commited on
Commit
2b666cd
1 Parent(s): 0570fe0

Application file

Browse files
dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+ #This creates a directory for your app. Do not change anything here
3
+ WORKDIR /app
4
+
5
+ #This also makes your directory for the cache writable. DO not change anything here
6
+ RUN mkdir -p /.cache/huggingface/hub && chmod -R 777 /.cache
7
+
8
+ # This creates a virtual environment for your app on your hugging face. Dont change anything here as well
9
+ ENV TRANSFORMERS_CACHE /.cache/huggingface/hub
10
+
11
+ #This copies the requirement to your hugging face account. Do not change anything
12
+ COPY requirements.txt .
13
+
14
+ # This copies your streamlit app to your hugging face
15
+ # change 'sentimentappstreamlit.py' to the actual name of your app. There is one space and a (fullstop)after the name of your app
16
+ COPY sentimentapp.py .
17
+
18
+ #If you used any picture in your application,first make sure its in the same path as your app.
19
+ #This code copies the picture unto your hugging face.
20
+ COPY senti.jpg .
21
+ COPY negative-smiley-face.png .
22
+ COPY positive-smiley-face.png .
23
+ COPY neutral-smiley-face.png .
24
+ COPY swipe-swoosh.mp3 .
25
+
26
+
27
+ RUN pip3 install --upgrade pip
28
+
29
+ RUN pip3 install -r requirements.txt
30
+
31
+
32
+
33
+ CMD ["streamlit","run","sentimentapp.py", "--server.address", "0.0.0.0", "--server.port", "7860", "--browser.serverAddress", "Adoley/personalized_app.hf.space", "--browser.serverAddress","0.0.0.0:7860"]
negative-smiley-face.png ADDED
neutral-smiley-face.png ADDED
positive-smiley-face.png ADDED
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ streamlit==0.93.0
2
+ transformers==4.11.3
3
+ torch==1.9.0
4
+ pandas==1.3.4
5
+ altair==4.1.0
6
+ textblob==0.15.3
7
+ vaderSentiment==3.5.1
8
+ Pillow==8.4.0
senti.jpg ADDED
sentimentapp.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import streamlit as st
4
+ import altair as alt
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
+ from PIL import Image
7
+ import base64
8
+
9
+
10
+
11
+
12
+
13
+
14
+ # Functions
15
+ def main():
16
+ st.title("Sentiment Analysis App")
17
+ st.subheader("Reformation Team Project")
18
+
19
+
20
+
21
+ st.image("senti.jpg")
22
+
23
+ # Define the available models
24
+ models = {
25
+ "ROBERTA": "Adoley/covid-tweets-sentiment-analysis-roberta-model",
26
+ "BERT": "Adoley/covid-tweets-sentiment-analysis",
27
+ "DISTILBERT": "Adoley/covid-tweets-sentiment-analysis-distilbert-model"
28
+ }
29
+
30
+ menu = ["Home", "About"]
31
+ choice = st.sidebar.selectbox("Menu", menu)
32
+
33
+ how_to_use = """
34
+ ## How to Use
35
+
36
+ 1. Enter your text in the input box.
37
+ 2. Click the **Analyze Sentiment** button.
38
+ 3. Wait for the app to process the text and display the sentiment analysis results.
39
+ 4. Explore the sentiment scores and visualization provided.
40
+ """
41
+
42
+ # Add the "How to Use" message to the sidebar
43
+ st.sidebar.markdown(how_to_use)
44
+
45
+ if choice == "Home":
46
+ st.subheader("Home")
47
+
48
+ # Add a dropdown menu to select the model
49
+ model_name = st.selectbox("Select a model", list(models.keys()))
50
+
51
+ with st.form(key="nlpForm"):
52
+ raw_text = st.text_area("Enter Text Here")
53
+ submit_button = st.form_submit_button(label="Analyze")
54
+
55
+
56
+
57
+ col1, col2 = st.columns(2)
58
+ if submit_button:
59
+ # Display sound-effect
60
+ st.info("🔮 Abracadabra! Your report has been submitted!")
61
+ sound_file = 'C:/Users/viole/OneDrive/Documents/streamlit2/swipe-swoosh.mp3'
62
+ st.audio(sound_file, format='audio/wav')
63
+
64
+ with col1:
65
+ st.info("Results")
66
+ tokenizer = AutoTokenizer.from_pretrained(models[model_name])
67
+ model = AutoModelForSequenceClassification.from_pretrained(models[model_name])
68
+
69
+
70
+ # Tokenize the input text
71
+ inputs = tokenizer(raw_text, return_tensors="pt")
72
+
73
+ # Make a forward pass through the model
74
+ outputs = model(**inputs)
75
+
76
+ # Get the predicted class and associated score
77
+ predicted_class = outputs.logits.argmax().item()
78
+ score = outputs.logits.softmax(dim=1)[0][predicted_class].item()
79
+
80
+ # Compute the scores for all sentiments
81
+ positive_score = outputs.logits.softmax(dim=1)[0][2].item()
82
+ negative_score = outputs.logits.softmax(dim=1)[0][0].item()
83
+ neutral_score = outputs.logits.softmax(dim=1)[0][1].item()
84
+
85
+ # Compute the confidence level
86
+ confidence_level = np.max(outputs.logits.detach().numpy())
87
+
88
+ # Print the predicted class and associated score
89
+ st.write(f"Predicted class: {predicted_class}, Score: {score:.3f}, Confidence Level: {confidence_level:.2f}")
90
+
91
+ # Emoji
92
+ if predicted_class == 2:
93
+ st.markdown("Sentiment: Positive :smiley:")
94
+ st.image("positive-smiley-face.png")
95
+ elif predicted_class == 1:
96
+ st.markdown("Sentiment: Neutral :😐:")
97
+ st.image("neutral-smiley-face.png")
98
+ else:
99
+ st.markdown("Sentiment: Negative :angry:")
100
+ st.image("negative-smiley-face.png")
101
+
102
+
103
+
104
+ results_df = pd.DataFrame(columns=["Sentiment Class", "Score"])
105
+
106
+ # Create a DataFrame with scores for all sentiments
107
+ all_scores_df = pd.DataFrame({
108
+ 'Sentiment Class': ['Positive', 'Negative', 'Neutral'],
109
+ 'Score': [positive_score, negative_score, neutral_score]
110
+ })
111
+
112
+ # Concatenate the two DataFrames
113
+
114
+ results_df = pd.concat([results_df, all_scores_df], ignore_index=True)
115
+
116
+
117
+
118
+ # Create the Altair chart
119
+ chart = alt.Chart(results_df).mark_bar(width=50).encode(
120
+ x="Sentiment Class",
121
+ y="Score",
122
+ color="Sentiment Class"
123
+ )
124
+
125
+ # Display the chart
126
+ with col2:
127
+ st.altair_chart(chart, use_container_width=True)
128
+ st.write(results_df)
129
+
130
+ else:
131
+ st.subheader("About")
132
+ st.write("This marvelous sentiment analysis NLP app, crafted with love by the brilliant minds of Team Reformation, dives into the realm of Covid-19 tweets. Armed with a pre-trained model, it fearlessly predicts the sentiment lurking within the depths of your text. Brace yourself for an adventure of teamwork and collaboration, as we embark on a quest to unravel the sentiments that dwell within the tweetsphere!")
133
+
134
+
135
+
136
+ if __name__ == "__main__":
137
+ main()
swipe-swoosh.mp3 ADDED
Binary file (11.8 kB). View file