Spaces:
Sleeping
Sleeping
kkesarwani
commited on
Commit
·
1a44e19
1
Parent(s):
faabc3a
gru model for sentiment analysis
Browse files- .DS_Store +0 -0
- .gitattributes +1 -0
- app.py +55 -0
- examples.txt +4 -0
- helper_functions.py +68 -0
- label_names.txt +4 -0
- models/gru_model.keras +3 -0
- requirements.txt +138 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from sklearn.preprocessing import LabelEncoder
|
4 |
+
import spacy
|
5 |
+
from timeit import default_timer as timer
|
6 |
+
from helper_functions import preprocess_single_sentence
|
7 |
+
|
8 |
+
|
9 |
+
with open('label_names.txt', 'r') as f:
|
10 |
+
labels = [emotion.strip() for emotion in f.readlines()]
|
11 |
+
|
12 |
+
with open('examples.txt', 'r') as f:
|
13 |
+
example_list = [example.strip() for example in f.readlines()]
|
14 |
+
|
15 |
+
encoder= LabelEncoder()
|
16 |
+
encoder.fit(labels)
|
17 |
+
|
18 |
+
nlp = spacy.load("en_core_web_sm")
|
19 |
+
|
20 |
+
model = tf.keras.models.load_model('models/gru_model.keras')
|
21 |
+
|
22 |
+
def make_predictions(text):
|
23 |
+
"""
|
24 |
+
Make predictions on the given text using the trained model.
|
25 |
+
|
26 |
+
Args:
|
27 |
+
text (str): The text to make predictions on.
|
28 |
+
|
29 |
+
Returns:
|
30 |
+
list: A list of predictions.
|
31 |
+
"""
|
32 |
+
text= preprocess_single_sentence(text)
|
33 |
+
text= tf.expand_dims(text, 0)
|
34 |
+
|
35 |
+
start_time= timer()
|
36 |
+
probability = model.predict(text)
|
37 |
+
pred_label_with_prob= {labels[i]: float(probability[0][i]) for i in range(len(labels))}
|
38 |
+
pred_time = round(timer() - start_time, 5)
|
39 |
+
return pred_label_with_prob, pred_time
|
40 |
+
|
41 |
+
input= gr.Textbox(lines=5, label="Enter text", placeholder="i like to have the same breathless feeling as a reader eager to see what will happen next")
|
42 |
+
outputs=[
|
43 |
+
gr.Label(num_top_classes=len(labels), label="Predictions"),
|
44 |
+
gr.Number(label="Prediction time (s)"),
|
45 |
+
]
|
46 |
+
title= ' Sentiment Analysis 🤣😱😡😢 '
|
47 |
+
description= 'The sentiment analysis model is a deep learning-based natural language processing (NLP) model designed to analyze and classify the sentiment expressed in text data. It is trained to understand the emotional tone of text and categorize it into predefined sentiment categories such as <b>anger, fear, saddness and joy.<b>'
|
48 |
+
demo= gr.Interface(fn=make_predictions,
|
49 |
+
inputs=input,
|
50 |
+
outputs=outputs,
|
51 |
+
title=title,
|
52 |
+
description=description,
|
53 |
+
examples= example_list
|
54 |
+
)
|
55 |
+
demo.launch()
|
examples.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
im feeling rather rotten so im not very ambitious right now
|
2 |
+
i left with my bouquet of red and yellow tulips under my arm feeling slightly more optimistic than when i arrived
|
3 |
+
i cant walk into a shop anywhere where i do not feel uncomfortable
|
4 |
+
i felt anger when at the end of a telephone call
|
helper_functions.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import seaborn as sns
|
4 |
+
from sklearn.preprocessing import LabelEncoder
|
5 |
+
import spacy
|
6 |
+
|
7 |
+
with open('label_names.txt', 'r') as f:
|
8 |
+
labels = [emotion.strip() for emotion in f.readlines()]
|
9 |
+
|
10 |
+
encoder= LabelEncoder()
|
11 |
+
encoder.fit(labels)
|
12 |
+
|
13 |
+
nlp = spacy.load("en_core_web_sm")
|
14 |
+
|
15 |
+
|
16 |
+
def plot_pie_chart(data_frame: pd.DataFrame, title: str) -> None:
|
17 |
+
"""
|
18 |
+
Plot a pie chart to visualize label distribution in the provided DataFrame.
|
19 |
+
|
20 |
+
Args:
|
21 |
+
data_frame (pd.DataFrame): The DataFrame containing the data to visualize.
|
22 |
+
title (str): The title for the pie chart.
|
23 |
+
|
24 |
+
Returns:
|
25 |
+
None
|
26 |
+
"""
|
27 |
+
label_count = data_frame['label'].value_counts()
|
28 |
+
plt.figure(figsize=(8, 8))
|
29 |
+
sns.set_style("whitegrid")
|
30 |
+
plt.pie(label_count, labels=label_count.index, colors=sns.color_palette("hls", len(label_count.index)), autopct='%1.1f%%', startangle=90)
|
31 |
+
plt.title(f"{title} Label Distribution")
|
32 |
+
plt.show()
|
33 |
+
plt.close()
|
34 |
+
|
35 |
+
def preprocess_text(df: pd.DataFrame, emotions: list=['love', 'surprise']):
|
36 |
+
"""
|
37 |
+
Preprocesses text data in a DataFrame.
|
38 |
+
|
39 |
+
Args:
|
40 |
+
df (pd.DataFrame): DataFrame containing 'sentence' and 'label' columns.
|
41 |
+
encoder (LabelEncoder): Label encoder for the labels.
|
42 |
+
emotions (list): List of emotions to drop from the DataFrame.
|
43 |
+
|
44 |
+
Returns:
|
45 |
+
pd.DataFrame: DataFrame with preprocessed text and encoded labels.
|
46 |
+
"""
|
47 |
+
for i in emotions:
|
48 |
+
df = df[df['label'] != i]
|
49 |
+
|
50 |
+
df['processed_text'] = df['text'].apply(lambda x: ' '.join([token.lemma_ for token in nlp(x) if not token.is_stop and not token.is_punct and not token.is_space]))
|
51 |
+
|
52 |
+
df['label_num'] = encoder.transform(df['label'])
|
53 |
+
df.drop(columns=['text', 'label'], inplace=True)
|
54 |
+
return df
|
55 |
+
|
56 |
+
def preprocess_single_sentence(sentence):
|
57 |
+
"""
|
58 |
+
Preprocesses a single sentence.
|
59 |
+
|
60 |
+
Args:
|
61 |
+
sentence (str): Input sentence.
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
str: Preprocessed and tokenized sentence.
|
65 |
+
"""
|
66 |
+
processed_text = ' '.join([token.lemma_ for token in nlp(sentence) if not token.is_stop and not token.is_punct and not token.is_space])
|
67 |
+
return processed_text
|
68 |
+
|
label_names.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
joy
|
2 |
+
sadness
|
3 |
+
fear
|
4 |
+
anger
|
models/gru_model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5e1e9d49645ff672edec9f35fd19857b9143b0e9295d759886bbb50f7cfdba0d
|
3 |
+
size 9241147
|
requirements.txt
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
absl-py==2.1.0
|
2 |
+
aiofiles==23.2.1
|
3 |
+
altair==5.2.0
|
4 |
+
annotated-types==0.6.0
|
5 |
+
anyio==4.3.0
|
6 |
+
appnope==0.1.4
|
7 |
+
asttokens==2.4.1
|
8 |
+
astunparse==1.6.3
|
9 |
+
attrs==23.2.0
|
10 |
+
blis==0.7.11
|
11 |
+
catalogue==2.0.10
|
12 |
+
certifi==2024.2.2
|
13 |
+
charset-normalizer==3.3.2
|
14 |
+
click==8.1.7
|
15 |
+
cloudpathlib==0.16.0
|
16 |
+
colorama==0.4.6
|
17 |
+
comm==0.2.2
|
18 |
+
confection==0.1.4
|
19 |
+
contourpy==1.2.0
|
20 |
+
cycler==0.12.1
|
21 |
+
cymem==2.0.8
|
22 |
+
debugpy==1.8.1
|
23 |
+
decorator==5.1.1
|
24 |
+
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.1/en_core_web_sm-3.7.1-py3-none-any.whl#sha256=86cc141f63942d4b2c5fcee06630fd6f904788d2f0ab005cce45aadb8fb73889
|
25 |
+
exceptiongroup==1.2.0
|
26 |
+
executing==2.0.1
|
27 |
+
fastapi==0.110.0
|
28 |
+
ffmpy==0.3.2
|
29 |
+
filelock==3.13.1
|
30 |
+
flatbuffers==24.3.7
|
31 |
+
fonttools==4.50.0
|
32 |
+
fsspec==2024.3.1
|
33 |
+
gast==0.5.4
|
34 |
+
google-pasta==0.2.0
|
35 |
+
gradio==4.21.0
|
36 |
+
gradio_client==0.12.0
|
37 |
+
grpcio==1.62.1
|
38 |
+
h11==0.14.0
|
39 |
+
h5py==3.10.0
|
40 |
+
httpcore==1.0.4
|
41 |
+
httpx==0.27.0
|
42 |
+
huggingface-hub==0.21.4
|
43 |
+
idna==3.6
|
44 |
+
importlib_metadata==7.0.2
|
45 |
+
importlib_resources==6.3.1
|
46 |
+
ipykernel==6.29.3
|
47 |
+
ipython==8.18.1
|
48 |
+
jedi==0.19.1
|
49 |
+
Jinja2==3.1.3
|
50 |
+
joblib==1.3.2
|
51 |
+
jsonschema==4.21.1
|
52 |
+
jsonschema-specifications==2023.12.1
|
53 |
+
jupyter_client==8.6.1
|
54 |
+
jupyter_core==5.7.2
|
55 |
+
keras==3.1.0
|
56 |
+
kiwisolver==1.4.5
|
57 |
+
langcodes==3.3.0
|
58 |
+
libclang==18.1.1
|
59 |
+
Markdown==3.6
|
60 |
+
markdown-it-py==3.0.0
|
61 |
+
MarkupSafe==2.1.5
|
62 |
+
matplotlib==3.8.3
|
63 |
+
matplotlib-inline==0.1.6
|
64 |
+
mdurl==0.1.2
|
65 |
+
ml-dtypes==0.3.2
|
66 |
+
murmurhash==1.0.10
|
67 |
+
namex==0.0.7
|
68 |
+
nest-asyncio==1.6.0
|
69 |
+
numpy==1.26.4
|
70 |
+
opt-einsum==3.3.0
|
71 |
+
optree==0.10.0
|
72 |
+
orjson==3.9.15
|
73 |
+
packaging==24.0
|
74 |
+
pandas==2.2.1
|
75 |
+
parso==0.8.3
|
76 |
+
pexpect==4.9.0
|
77 |
+
pillow==10.2.0
|
78 |
+
platformdirs==4.2.0
|
79 |
+
preshed==3.0.9
|
80 |
+
prompt-toolkit==3.0.43
|
81 |
+
protobuf==4.25.3
|
82 |
+
psutil==5.9.8
|
83 |
+
ptyprocess==0.7.0
|
84 |
+
pure-eval==0.2.2
|
85 |
+
pydantic==2.6.4
|
86 |
+
pydantic_core==2.16.3
|
87 |
+
pydub==0.25.1
|
88 |
+
Pygments==2.17.2
|
89 |
+
pyparsing==3.1.2
|
90 |
+
python-dateutil==2.9.0.post0
|
91 |
+
python-multipart==0.0.9
|
92 |
+
pytz==2024.1
|
93 |
+
PyYAML==6.0.1
|
94 |
+
pyzmq==25.1.2
|
95 |
+
referencing==0.34.0
|
96 |
+
requests==2.31.0
|
97 |
+
rich==13.7.1
|
98 |
+
rpds-py==0.18.0
|
99 |
+
ruff==0.3.3
|
100 |
+
scikit-learn==1.4.1.post1
|
101 |
+
scipy==1.12.0
|
102 |
+
seaborn==0.13.2
|
103 |
+
semantic-version==2.10.0
|
104 |
+
shellingham==1.5.4
|
105 |
+
six==1.16.0
|
106 |
+
smart-open==6.4.0
|
107 |
+
sniffio==1.3.1
|
108 |
+
spacy==3.7.4
|
109 |
+
spacy-legacy==3.0.12
|
110 |
+
spacy-loggers==1.0.5
|
111 |
+
srsly==2.4.8
|
112 |
+
stack-data==0.6.3
|
113 |
+
starlette==0.36.3
|
114 |
+
tensorboard==2.16.2
|
115 |
+
tensorboard-data-server==0.7.2
|
116 |
+
tensorflow==2.16.1
|
117 |
+
tensorflow-io-gcs-filesystem==0.36.0
|
118 |
+
termcolor==2.4.0
|
119 |
+
thinc==8.2.3
|
120 |
+
threadpoolctl==3.3.0
|
121 |
+
tomlkit==0.12.0
|
122 |
+
toolz==0.12.1
|
123 |
+
tornado==6.4
|
124 |
+
tqdm==4.66.2
|
125 |
+
traitlets==5.14.2
|
126 |
+
typer==0.9.0
|
127 |
+
typing_extensions==4.10.0
|
128 |
+
tzdata==2024.1
|
129 |
+
urllib3==2.2.1
|
130 |
+
uvicorn==0.28.1
|
131 |
+
wasabi==1.1.2
|
132 |
+
wcwidth==0.2.13
|
133 |
+
weasel==0.3.4
|
134 |
+
websockets==11.0.3
|
135 |
+
Werkzeug==3.0.1
|
136 |
+
wordcloud==1.9.3
|
137 |
+
wrapt==1.16.0
|
138 |
+
zipp==3.18.1
|