Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from pytube import extract
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
-
import
|
| 6 |
import re
|
|
|
|
|
|
|
| 7 |
import nltk
|
| 8 |
import nltk.sentiment.util
|
| 9 |
from nltk.corpus import stopwords
|
| 10 |
from nltk.stem import WordNetLemmatizer
|
| 11 |
-
from textblob import TextBlob
|
| 12 |
-
import numpy as np
|
| 13 |
-
import math
|
| 14 |
|
| 15 |
-
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
sw = stopwords.words('english')
|
| 18 |
lemmatizer = WordNetLemmatizer()
|
| 19 |
|
| 20 |
-
|
| 21 |
## get YouTube ID
|
| 22 |
def getID(url):
|
| 23 |
print("Getting YouTube ID...")
|
| 24 |
return extract.video_id(url)
|
| 25 |
|
| 26 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def clean_text(text):
|
|
|
|
| 28 |
# remove symbols and Emojis
|
| 29 |
text = text.lower()
|
| 30 |
text = re.sub('@', '', text)
|
|
@@ -53,20 +63,33 @@ def clean_text(text):
|
|
| 53 |
|
| 54 |
return text
|
| 55 |
|
| 56 |
-
## download comments
|
| 57 |
-
def downloadComments(videoID):
|
| 58 |
-
print("Downloading Comments...")
|
| 59 |
-
os.system("youtube-comment-downloader --youtubeid=" + videoID + " --output Comments/" + videoID + ".json")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
def getSentenceTrain():
|
| 63 |
# open sentences_train file
|
| 64 |
sentences_train_f = open('../Deep learning/pickles/sentences_train.pickle', "rb")
|
| 65 |
sentences_train = pickle.load(sentences_train_f)
|
| 66 |
sentences_train_f.close()
|
| 67 |
return sentences_train
|
| 68 |
-
|
| 69 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def vote(test_point, _test):
|
| 71 |
print("Voting on video effectivess...\n")
|
| 72 |
pos_weighting = []
|
|
@@ -75,11 +98,11 @@ def vote(test_point, _test):
|
|
| 75 |
algos_score = 0
|
| 76 |
|
| 77 |
algorithms = [
|
| 78 |
-
|
| 79 |
-
{'name': 'SGD', 'accuracy':
|
| 80 |
-
|
| 81 |
-
{'name': 'Logistic Regression', 'accuracy':
|
| 82 |
-
{'name': 'CNN', 'accuracy':
|
| 83 |
]
|
| 84 |
|
| 85 |
for algo in algorithms:
|
|
@@ -104,7 +127,7 @@ def vote(test_point, _test):
|
|
| 104 |
result = 'effective'
|
| 105 |
confidence = pos_result
|
| 106 |
|
| 107 |
-
|
| 108 |
|
| 109 |
def quantizeEffectiveness(url):
|
| 110 |
# 1. Get YouTube ID
|
|
@@ -128,7 +151,7 @@ def quantizeEffectiveness(url):
|
|
| 128 |
# 4. Create test dataframe
|
| 129 |
test = pd.DataFrame([[videoID]], columns=['VideoID'])
|
| 130 |
|
| 131 |
-
# 5. Get
|
| 132 |
test_documents = []
|
| 133 |
comment = pd.read_csv("Processed Comments/" + videoID + "_all_words.csv")
|
| 134 |
test_documents.append(list(comment["0"]))
|
|
@@ -151,14 +174,14 @@ def quantizeEffectiveness(url):
|
|
| 151 |
_test = pad_sequences(tokenizer.texts_to_sequences(test_sentence), padding='post', maxlen=100)
|
| 152 |
|
| 153 |
# 10. Vote on video effectiveness
|
| 154 |
-
vote(test_point,_test)
|
| 155 |
|
| 156 |
-
def greet(
|
| 157 |
-
|
| 158 |
-
if not os.exists('
|
| 159 |
-
os.mkdir('
|
| 160 |
|
| 161 |
-
return
|
| 162 |
|
| 163 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 164 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import pandas as pd
|
| 4 |
+
from pytube import extract
|
| 5 |
import re
|
| 6 |
+
import string
|
| 7 |
+
import pickle
|
| 8 |
import nltk
|
| 9 |
import nltk.sentiment.util
|
| 10 |
from nltk.corpus import stopwords
|
| 11 |
from nltk.stem import WordNetLemmatizer
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
from sklearn.metrics import accuracy_score
|
| 14 |
|
| 15 |
+
from keras.preprocessing.text import Tokenizer
|
| 16 |
+
from keras.preprocessing.sequence import pad_sequences
|
| 17 |
+
from tensorflow import keras
|
| 18 |
+
|
| 19 |
+
import xgboost
|
| 20 |
+
|
| 21 |
+
nltk.download(stopwords)
|
| 22 |
sw = stopwords.words('english')
|
| 23 |
lemmatizer = WordNetLemmatizer()
|
| 24 |
|
|
|
|
| 25 |
## get YouTube ID
|
| 26 |
def getID(url):
|
| 27 |
print("Getting YouTube ID...")
|
| 28 |
return extract.video_id(url)
|
| 29 |
|
| 30 |
+
## download comments
|
| 31 |
+
def downloadComments(videoID):
|
| 32 |
+
print("Downloading Comments...")
|
| 33 |
+
os.system("youtube-comment-downloader --youtubeid=" + videoID + " --output Comments/" + videoID + ".json")
|
| 34 |
+
|
| 35 |
+
# function to clean comments
|
| 36 |
def clean_text(text):
|
| 37 |
+
|
| 38 |
# remove symbols and Emojis
|
| 39 |
text = text.lower()
|
| 40 |
text = re.sub('@', '', text)
|
|
|
|
| 63 |
|
| 64 |
return text
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def getSentenceTrain():
|
| 67 |
# open sentences_train file
|
| 68 |
sentences_train_f = open('../Deep learning/pickles/sentences_train.pickle', "rb")
|
| 69 |
sentences_train = pickle.load(sentences_train_f)
|
| 70 |
sentences_train_f.close()
|
| 71 |
return sentences_train
|
| 72 |
+
|
| 73 |
+
# open pickle file
|
| 74 |
+
# randFor_71_f = open('../Shallow machine learning/pickles/randFor_71.pickle', "rb")
|
| 75 |
+
# randFor_train = pickle.load(randFor_71_f)
|
| 76 |
+
# randFor_71_f.close()
|
| 77 |
+
|
| 78 |
+
SGD_74_f = open('../Shallow machine learning/pickles/SGD_74.pickle', "rb")
|
| 79 |
+
SGD_train = pickle.load(SGD_74_f)
|
| 80 |
+
SGD_74_f.close()
|
| 81 |
+
|
| 82 |
+
# XGB_74_f = open('../Shallow machine learning/pickles/XGB_74.pickle', "rb")
|
| 83 |
+
# XGB_train = pickle.load(XGB_74_f)
|
| 84 |
+
# XGB_74_f.close()
|
| 85 |
+
|
| 86 |
+
logreg_79_f = open('../Shallow machine learning/pickles/logreg_79.pickle', "rb")
|
| 87 |
+
logreg_train = pickle.load(logreg_79_f)
|
| 88 |
+
logreg_79_f.close()
|
| 89 |
+
|
| 90 |
+
# get saved CNN model
|
| 91 |
+
model = keras.models.load_model("../Deep learning/CNN_82")
|
| 92 |
+
|
| 93 |
def vote(test_point, _test):
|
| 94 |
print("Voting on video effectivess...\n")
|
| 95 |
pos_weighting = []
|
|
|
|
| 98 |
algos_score = 0
|
| 99 |
|
| 100 |
algorithms = [
|
| 101 |
+
# {'name': 'Random Forest', 'accuracy': 0.71*100, 'trained': randFor_train},
|
| 102 |
+
{'name': 'SGD', 'accuracy': 0.74*100, 'trained': SGD_train},
|
| 103 |
+
# {'name': 'XGBoost', 'accuracy': 0.74*100, 'trained': XGB_train},
|
| 104 |
+
{'name': 'Logistic Regression', 'accuracy': 0.79*100, 'trained': logreg_train},
|
| 105 |
+
{'name': 'CNN', 'accuracy': 0.82*100, 'trained': model}
|
| 106 |
]
|
| 107 |
|
| 108 |
for algo in algorithms:
|
|
|
|
| 127 |
result = 'effective'
|
| 128 |
confidence = pos_result
|
| 129 |
|
| 130 |
+
return result
|
| 131 |
|
| 132 |
def quantizeEffectiveness(url):
|
| 133 |
# 1. Get YouTube ID
|
|
|
|
| 151 |
# 4. Create test dataframe
|
| 152 |
test = pd.DataFrame([[videoID]], columns=['VideoID'])
|
| 153 |
|
| 154 |
+
# 5. Get documents (pre-processd comments)
|
| 155 |
test_documents = []
|
| 156 |
comment = pd.read_csv("Processed Comments/" + videoID + "_all_words.csv")
|
| 157 |
test_documents.append(list(comment["0"]))
|
|
|
|
| 174 |
_test = pad_sequences(tokenizer.texts_to_sequences(test_sentence), padding='post', maxlen=100)
|
| 175 |
|
| 176 |
# 10. Vote on video effectiveness
|
| 177 |
+
vote_result = vote(test_point,_test)
|
| 178 |
|
| 179 |
+
def greet(url):
|
| 180 |
+
vote_result = quantizeEffectiveness(url)
|
| 181 |
+
if not os.exists('Comments'):
|
| 182 |
+
os.mkdir('Comments')
|
| 183 |
|
| 184 |
+
return vote_result
|
| 185 |
|
| 186 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 187 |
iface.launch()
|