def create(): global FLAG print("u" * 1000) print(request.data) print("u" * 1000) if (request.data == b'' and FLAG == 1): search_term = "#twitter" FLAG = 0 else: request_data = json.loads(request.data) search_term = request_data['content'] print(search_term) print(search_term) CONSUMER_KEY = "Nea50h6UNQ5MLH6DIiPKxTnaV" CONSUMER_SECRET = "VszNel95RlnGckBhV9gmKp1N1U92HdKZbTi3nmm9YOFDBcs1ki" ACCESS_TOKEN = "926179846253723648-PgHMZnAW2wf9ANOmEkLdGUPTmfBEVGP" ACCESS_TOKEN_SECRET = "SWbmV6apGGeApXr6yTwJU4LNJbNUIPt7AOZ4cjawl1o34" """consumer_key = os.environ.get('CONSUMER_KEY') consumer_secret = os.environ.get('CONSUMER_SECRET') access_token = os.environ.get('ACCESS_TOKEN') access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET')""" consumer_key = CONSUMER_KEY consumer_secret = CONSUMER_SECRET access_token = ACCESS_TOKEN access_token_secret = ACCESS_TOKEN_SECRET auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) print(consumer_key) print("all good") tweets = api.search(search_term, count=500) data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets']) head = data.head(10) listy = [] print("here 1" * 100) tweetList = [tweets for tweets in data['Tweets']] print("p" * 1000) print(tweetList) print("p" * 1000) print("here 5" * 100) scoreList = [] labelList = [] model = load_model("model.h5") for i in range(len(tweetList)): scoreList.append(str(round(model(tweetList[i]).polarity, 1))) print("here 6" * 100) for i in range(len(tweetList)): pLabel = 0 print('0') scr = float(scoreList[i]) print(scr) if (scr < -1 * 0.3): pLabel = "Negative" if (scr >= -1 * 0.3 and scr <= 0.3): pLabel = "Neutral" if (scr > 0.3): pLabel = "Positive" labelList.append(pLabel) global arr tempArr = [] print("here 2" * 100) for i in range(len(tweetList)): print('heeee') print(tweetList[i]) print(scoreList[i]) print(labelList[i]) tempArr.append({ "id": i, "key_word": search_term, "tweet": tweetList[i], "score": scoreList[i], "label": labelList[i] }) print(tweetList[i]) arr = tempArr print("here 3" * 100) resp = jsonify(json.dumps(arr)) resp.headers.add("Access-Control-Allow-Origin", "*") resp.status_code = 200 return resp
# loop of the base directory for idx, target_dir in enumerate(dirs): print("\n===========================") print("[{idx}/{total}] {target_dir}".format(target_dir=target_dir, idx=idx + 1, total=len(dirs))) machine_type = os.path.split(target_dir)[1] print("============== MODEL LOAD ==============") # load model file model_file = "{model}/model_{machine_type}.hdf5".format( model=param["model_directory"], machine_type=machine_type) if not os.path.exists(model_file): com.logger.error("{} model not found ".format(machine_type)) sys.exit(-1) model = keras_model.load_model(model_file) model.summary() # load anomaly score distribution for determining threshold score_distr_file_path = "{model}/score_distr_{machine_type}.pkl".format( model=param["model_directory"], machine_type=machine_type) shape_hat, loc_hat, scale_hat = joblib.load(score_distr_file_path) # determine threshold for decision decision_threshold = scipy.stats.gamma.ppf( q=param["decision_threshold"], a=shape_hat, loc=loc_hat, scale=scale_hat) if mode:
def create(): print("u" * 1000) print(request.data) print("u" * 1000) if (request.data == b''): search_term = "#twitter" else: request_data = json.loads(request.data) search_term = request_data['content'] print(search_term) print(search_term) consumer_key = os.environ.get('CONSUMER_KEY') consumer_secret = os.environ.get('CONSUMER_SECRET') access_token = os.environ.get('ACCESS_TOKEN') access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET') auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) tweets = api.search(search_term, count=500) data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets']) head = data.head(10) listy = [] print("here 1" * 100) tweetList = [tweets for tweets in data['Tweets']] print("p" * 1000) print(tweetList) print("p" * 1000) print("here 5" * 100) scoreList = [] labelList = [] model = load_model("model.h5") for i in range(len(tweetList)): scoreList.append(str(round(model(tweetList[i]).polarity, 1))) print("here 6" * 100) for i in range(len(tweetList)): pLabel = 0 print('0') scr = float(scoreList[i]) print(scr) if (scr < -1 * 0.3): pLabel = "Negative" if (scr >= -1 * 0.3 and scr <= 0.3): pLabel = "Neutral" if (scr > 0.3): pLabel = "Positive" labelList.append(pLabel) global arr tempArr = [] print("here 2" * 100) for i in range(len(tweetList)): print('heeee') print(tweetList[i]) print(scoreList[i]) print(labelList[i]) tempArr.append({ "id": i, "key_word": search_term, "tweet": tweetList[i], "score": scoreList[i], "label": labelList[i] }) print(tweetList[i]) arr = tempArr print("here 3" * 100) resp = jsonify(json.dumps(arr)) resp.headers.add("Access-Control-Allow-Origin", "*") resp.status_code = 200 return resp