Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +60 -0
- freqs.pkl +3 -0
- sentiment1.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import numpy as np
|
| 3 |
+
import re
|
| 4 |
+
import string
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
from nltk.stem import PorterStemmer
|
| 7 |
+
from nltk.tokenize import TweetTokenizer
|
| 8 |
+
import nltk
|
| 9 |
+
import pickle
|
| 10 |
+
import gradio as gr
|
| 11 |
+
|
| 12 |
+
with open("freqs.pkl","rb") as fp:
|
| 13 |
+
freqs = pickle.load(fp)
|
| 14 |
+
fp.close()
|
| 15 |
+
model = joblib.load("C:\\Users\\aryan\\OneDrive\\Desktop\\sentiment\\sentiment1.pkl")
|
| 16 |
+
|
| 17 |
+
nltk.download("stopwords")
|
| 18 |
+
stop = stopwords.words("english")
|
| 19 |
+
punc = string.punctuation
|
| 20 |
+
def process_tweet(tweet):
|
| 21 |
+
stemmer = PorterStemmer()
|
| 22 |
+
tweet2 = re.sub(r'^RT[\s]+',"",tweet)
|
| 23 |
+
tweet2 = re.sub(r'https?://[^\s\n\r]+','',tweet2)
|
| 24 |
+
tweet2 = re.sub(r'#','',tweet2)
|
| 25 |
+
tokenizer = TweetTokenizer(preserve_case=False, strip_handles=True, reduce_len=True)
|
| 26 |
+
tokens = tokenizer.tokenize(tweet2)
|
| 27 |
+
tokens_new = []
|
| 28 |
+
for word in tokens:
|
| 29 |
+
if (word not in stop and word not in punc):
|
| 30 |
+
tokens_new.append(stemmer.stem(word))
|
| 31 |
+
else:
|
| 32 |
+
continue
|
| 33 |
+
return tokens_new
|
| 34 |
+
|
| 35 |
+
def extract_features(tweets, freqs):
|
| 36 |
+
m = len(tweets)
|
| 37 |
+
original_row = np.array([1,0,0])
|
| 38 |
+
x = np.tile(original_row, (m, 1))
|
| 39 |
+
count = 0
|
| 40 |
+
for i in range(0,m):
|
| 41 |
+
for word in process_tweet(tweets[i]):
|
| 42 |
+
x[i][1]+=freqs.get((word,1),0)
|
| 43 |
+
x[i][2]+=freqs.get((word,0),0)
|
| 44 |
+
if "not" in tweets[0]:
|
| 45 |
+
if(x[0][1]>x[0][2]): x[0][2]=x[0][1]+50
|
| 46 |
+
else: x[0][1]=x[0][2]+50
|
| 47 |
+
return x
|
| 48 |
+
|
| 49 |
+
def predict(tweet,freqs=freqs):
|
| 50 |
+
arr = [tweet]
|
| 51 |
+
x= extract_features(arr,freqs)
|
| 52 |
+
res = model.predict(x)
|
| 53 |
+
if (res==0): return "Negative comment"
|
| 54 |
+
else: return "Positive comment"
|
| 55 |
+
|
| 56 |
+
tweet = gr.Textbox(placeholder="Enter your tweet here")
|
| 57 |
+
int = gr.Interface(fn = predict, inputs = tweet, outputs = "textbox").launch(share = True)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
freqs.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:df533ac131097d72b6f8fe76300460151b48e6ca531fbfaa4252be7cd9051a7c
|
| 3 |
+
size 314822
|
sentiment1.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1482ddb613a544cd3307989f12554932cd7e87195d8dbc049c5f949106182ea0
|
| 3 |
+
size 847
|