Spaces:
Running
Running
Commit
·
d799ebd
1
Parent(s):
2d7e826
added application files
Browse files- app.py +68 -0
- models/model_v1.pkl +3 -0
- models/transformer_v1.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import joblib as jb
|
3 |
+
import re
|
4 |
+
import string
|
5 |
+
from nltk.corpus import stopwords
|
6 |
+
from nltk.tokenize import word_tokenize
|
7 |
+
from nltk.stem import PorterStemmer, WordNetLemmatizer
|
8 |
+
import nltk
|
9 |
+
nltk.download('stopwords')
|
10 |
+
stop_words = set(stopwords.words('english'))
|
11 |
+
|
12 |
+
cv = jb.load('.//models//transformer_v1.pkl')
|
13 |
+
model = jb.load('.//models//model_v1.pkl')
|
14 |
+
|
15 |
+
|
16 |
+
def preprocess_text(text):
|
17 |
+
"""
|
18 |
+
Runs a set of transformational steps to
|
19 |
+
preprocess the text of the tweet.
|
20 |
+
"""
|
21 |
+
# convert all text to lower case
|
22 |
+
text = text.lower()
|
23 |
+
|
24 |
+
# remove any urls
|
25 |
+
text = re.sub(r'http\S+|www\S+|https\S+', "", text, flags=re.MULTILINE)
|
26 |
+
|
27 |
+
# replace '****' with 'curse'
|
28 |
+
text = re.sub(r'\*\*\*\*', "gaali", text)
|
29 |
+
|
30 |
+
# remove punctuations
|
31 |
+
text = text.translate(str.maketrans("", "", string.punctuation))
|
32 |
+
|
33 |
+
# remove user @ references and hashtags
|
34 |
+
text = re.sub(r'\@\w+|\#', "", text)
|
35 |
+
|
36 |
+
# remove useless characters
|
37 |
+
text = re.sub(r'[^ -~]', '', text)
|
38 |
+
|
39 |
+
# remove stopwords
|
40 |
+
tweet_tokens = word_tokenize(text)
|
41 |
+
filtered_words = [word for word in tweet_tokens if word not in stop_words]
|
42 |
+
|
43 |
+
# stemming
|
44 |
+
ps = PorterStemmer()
|
45 |
+
stemmed_words = [ps.stem(w) for w in filtered_words]
|
46 |
+
|
47 |
+
# lemmatizing
|
48 |
+
lemmatizer = WordNetLemmatizer()
|
49 |
+
lemma_words = [lemmatizer.lemmatize(w, pos='a') for w in stemmed_words]
|
50 |
+
|
51 |
+
return ' '.join(lemma_words)
|
52 |
+
|
53 |
+
|
54 |
+
def sentiment_analysis(text):
|
55 |
+
print(text)
|
56 |
+
text = cv.transform([preprocess_text(text)])
|
57 |
+
pred_prob = model.predict_proba(text)[0]
|
58 |
+
output = {"Negative": float(pred_prob[0]), "Neutral": float(pred_prob[1]), "Positive": float(pred_prob[2])}
|
59 |
+
print(output)
|
60 |
+
return output
|
61 |
+
|
62 |
+
|
63 |
+
demo = gr.Interface(
|
64 |
+
fn=sentiment_analysis,
|
65 |
+
inputs=gr.Textbox(label="Input here", lines=2, placeholder="Input your text"),
|
66 |
+
outputs=gr.Label(label="Sentiment Analysis"),
|
67 |
+
)
|
68 |
+
demo.launch()
|
models/model_v1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:28b989f2dec37cb7615dee7d299189decf1d15647fa92e1e9b6f38966a994449
|
3 |
+
size 419236163
|
models/transformer_v1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9fb9484ac5da542395636d3f2bed85ee06109f66eb4923d11bc0da05218a8357
|
3 |
+
size 232123
|