def predict(): # 받은 파일의 핸들러 취득 f = request.files["file"] # 파일이 비어있는지 확인 if f.filename == "": return "Bad Request", 400 ext = os.path.splitext(f.filename)[0] #filename change filename = f.filename = ext path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(filename)) f.save(path) pe = pefile.PE(path) index, probability = classifier.predict(path) result = OrderedDict() result["Machine"] = hex(pe.FILE_HEADER.Machine) result["malware"] = index result["probability"] = probability[index] # 결과를 JSON 형식으로 반환 return jsonify({"result": result})
def query_and_fetch(query, top_n=12): """Query Duck Duck Go (DDG) for top n results""" global url_details, url_text print('Query: ' + query + '; Top N: ' + str(top_n)) url_details = [] url_text = [] driver = None bad_request = False try: driver = Fetcher.get_selenium_driver() driver.get('https://api.duckduckgo.com/?q=' + query + '&kl=wt-wt') except: print('An error occurred while searching query: ' + query) Fetcher.close_selenium_driver(driver) Fetcher.search_driver = None bad_request = True finally: try: if not bad_request: results = driver.find_elements_by_class_name('result__a') result_size = len(results) print('Result Size: ' + str(result_size)) while result_size > 0 and len(url_details) < top_n: urls = [] for element in results: new_url = element.get_attribute('href') # TODO: Filter URLs if required print(new_url) urls.append(new_url) fetched_result = Fetcher.fetch_multiple(urls, top_n) for fetched_data in fetched_result: if not fetched_data[1] or len(fetched_data[1].strip()) == 0: continue details = dict() details['url'] = fetched_data[0] details['html'] = fetched_data[1] details['title'] = fetched_data[2] details['label'] = predict(fetched_data[3]) url_details.append(details) url_text.append(fetched_data[3]) if len(url_details) == top_n: break # Infinite Scroll if len(url_details) < top_n: driver.execute_script('window.scrollTo(0, document.body.scrollHeight);') results = driver.find_elements_by_class_name('result__a') results = results[result_size:] result_size = len(results) print('Moved to Next Page. Result Size: ' + str(result_size)) except: print('An error occurred while searching query: '+ query + ' and fetching results') #finally: # if driver is not None: # Fetcher.close_selenium_driver(driver) setattr(flask.current_app, 'url_text', url_text) print('Search Completed') return url_details
def classify_themes(self, split_sentences=True): predictions = classifier.predict(self.text, split_sentences) for prediction in predictions: sentence = Sentence(sentence=prediction['sentence'], sentiment=sentiment.polarity_scores( prediction['sentence'])['compound']) self.sentences.append(sentence) themes = [ Theme(theme=theme, score=score) for theme, score in prediction['themes'] ] sentence.themes.extend(themes)
def get(self, content): """Predict using ML model""" classes = { -1: 'Model doesn\'t exist', 0: 'Not Relevant', 1: 'Relevant', 2: 'Highly Relevant' } args = request.args print(args) if len(args) != 0: content = args['content'] result = classifier.predict(content) return classes[result]
def parse_details(model, fetched_data): """ Parse query details :param model: :param fetched_data: :return: """ details = dict() details['url'] = fetched_data[0] details['html'] = fetched_data[1] details['title'] = str(fetched_data[2]) details['label'] = int(predict(model, fetched_data[3])) imag = None now = datetime.now() current_time = now.strftime("%H:%M:%S") app.logger.info('Fetching image: '+ current_time) try: imag = requests.get(SCEHOST+'/render.png?url=' + fetched_data[0] + '&wait=1&width=320&height=240&timeout=5') except requests.exceptions.ConnectionError: app.logger.info('Connection error to SCE') if imag is not None and imag.status_code == 200: generatedid = str(uuid.uuid4()) now = datetime.now() current_time = now.strftime("%H:%M:%S") app.logger.info('Writing image: '+ current_time) with open(STORAGE_LOCATION+'/images/' + generatedid + '.png', 'wb') as file: file.write(imag.content) details['image'] = STORAGE_LOCATION+'/images/' + generatedid + '.png' now = datetime.now() current_time = now.strftime("%H:%M:%S") app.logger.info('Decoding image: '+ current_time) with open(details['image'], 'rb') as image_file: encoded_string = base64.b64encode(image_file.read()) details['image'] = encoded_string.decode() now = datetime.now() current_time = now.strftime("%H:%M:%S") app.logger.info('Image stuff done: '+ current_time) return details
def get(content, model): """ Predict using ML model :param content: :param model: :return: """ classes = { -1: 'Model doesn\'t exist', 0: 'Not Relevant', 1: 'Relevant', 2: 'Highly Relevant' } args = request.args if len(args) != 0: content = args['content'] if content: result = classifier.predict(model, content) return classes[result] print('NO CONTENT FOUND') return classes[-1]
def post(): """ Predict using ML model :return: """ classes = { -1: 'Model doesn\'t exist', 0: 'Not Relevant', 1: 'Relevant', 2: 'Highly Relevant' } result = -1 data = request.data loaded_data = json.loads(data.decode('utf-8', 'ignore')) if len(data) != 0: content = loaded_data['score'][0]['content'] if content is None: return classes[-1] model = loaded_data['score'][0]['model'] result = classifier.predict(model, content) return classes[result]