예제 #1
0
def edit(id):
    cust_id = request.args.get('cust_id', None)
    if cust_id == None or cust_id == '': 
        return render_template("login.html")
    cust_id = cust_id.strip().lower()
    cust = getCustomerModel().authenticate(cust_id)   
    
    if cust == None:            
        logging.error('\'' + cust_id + '\' is invalid customer Organization. ')
        return render_template("login.html", error_msg='Error: Invalid User Organization')
    
    book = getResponseModel().read(id, cust_id)

    if request.method == 'POST':
        data = request.form.to_dict(flat=True)
        orgdata = getResponseModel().read(id, cust_id=cust_id)
        book = getResponseModel().update(data['resp_name'], orgdata['res_category'], data['response_text'], data['tags'], orgdata['resp_tags'], modifiedflag=True, done=True, id=id, cust_id=cust_id)
        print (orgdata['resp_name'])
        '''
        trainlist = getTrainingModel().list_by_respcategory(orgdata['resp_name'], cust_id=cust_id)
        for resp in trainlist: 
            if (resp != None) and (len(resp) > 0) :
                for resp_item in resp: 
                    getTrainingModel().update(data['tags'], resp_item['query'], resp_item['response'], resp_item['query_category'], resp_category=resp_item['resp_category'], done=True, id=resp_item['id'], cust_id=cust_id)
        '''
        return redirect(url_for('.view', cust_id=cust_id, id=book['id']))

    return render_template("form.html", cust_id=cust_id, action="Edit", book=book)
예제 #2
0
    def extractFeedbackData_default(self, src_cust_id='default'):   
        logging.info ('extractFeedbackData_default : Started : ' + str(src_cust_id))
        trainlog = get_model()
        traindata = getTrainingModel() 

        next_page_token = 0
        token = None        
        cust_list, next_page_token2 = getCustomerModel().list(done=True)
        while next_page_token != None:             
            intent_logs, next_page_token = trainlog.list(log_type='feedback', cursor=token, cust_id=src_cust_id, done=True)
            token = next_page_token
            for intent_log in intent_logs: 
                intents_data = intent_log["json_data"]                 
                intents_data_json = json.loads(intents_data)
                selected_response_id = intents_data_json["selected_response_id"]
                cust_id = intents_data_json["ticket_data"]['currentAccount']['subdomain'] 
                id = intents_data_json["ticket_data"]['id']                
                for cust_id_x in cust_list:
                    if cust_id_x['cust_name'] == cust_id:
                        train_data = traindata.read(id, cust_id=cust_id)
                        response_data = getResponseModel().read(selected_response_id, cust_id=cust_id)
                        if train_data != None and response_data != None: 
                            traindata.update(train_data["tags"], train_data["query"], train_data["response"], query_category=train_data['query_category'], resp_category=response_data['res_category'], feedback_flag=True, done=True, id=train_data['id'], cust_id=cust_id)
                            trainlog.delete(intent_log['id'], cust_id=src_cust_id)
                            print('Updating Feedback : ' , id, cust_id)
        logging.info ('extractFeedbackData_default : Completed : ' + str(src_cust_id))
예제 #3
0
 def removeRespDuplicate(self, cust_id):
     logging.info('removeRespDuplicate : Started ' + str(cust_id))
     tickets_learn = tickets_learner()
     resp_model = getResponseModel()
     resp_data = tickets_learn.getResponseData(cust_id=cust_id)
     for resp_logs in resp_data:
         for resp_logx in resp_logs:
             for resp_logy in resp_logs:
                 if (resp_logx['id'] == resp_logy['id']
                         or resp_logx['modifiedflag'] == True
                         or resp_logx['defaultflag'] == True):
                     continue
                 respx_list = resp_logx['tags'].split()
                 respy_list = resp_logy['tags'].split()
                 count = 0
                 for items in respx_list:
                     if items in respy_list:
                         count += 1
                 #diff_cent = int((len(respx_list) - count) * 100 / len(respx_list))
                 if (count == len(respx_list)):
                     resp_model.delete(resp_logx['id'], cust_id)
                     logging.info('Deleting Duplicate Response id : ' +
                                  str(resp_logx['id']) + ' Over ' +
                                  str(resp_logy['id']))
     logging.info('removeRespDuplicate : Completed ' + str(cust_id))
예제 #4
0
def delete(id):
    cust_id = request.args.get('cust_id', None)
    if cust_id == None or cust_id == '': 
        return render_template("login.html")
    cust_id = cust_id.strip().lower()
    cust = getCustomerModel().authenticate(cust_id)
    if cust == None:            
        logging.error('\'' + cust_id + '\' is invalid customer Organization. ')
        return render_template("login.html", error_msg='Error: Invalid User Organization')
    
    dataitm = getResponseModel().read(id, cust_id)
    '''
    trainlist = getTrainingModel().list_by_respcategory(dataitm['resp_name'], cust_id=cust_id, done=True)
    for resp in trainlist: 
        if (resp != None) and (len(resp) > 0) :
            for resp_item in resp: 
                getTrainingModel().delete(resp_item['id'], cust_id=cust_id)
    '''                
    getResponseModel().delete(id, cust_id)
    return redirect(url_for('.list', cust_id=cust_id))
예제 #5
0
def view(id):
    cust_id = request.args.get('cust_id', None)
    if cust_id == None or cust_id == '': 
        return render_template("login.html")
    cust_id = cust_id.strip().lower()
    cust = getCustomerModel().authenticate(cust_id)
    if cust == None:            
        logging.error('\'' + cust_id + '\' is invalid customer Organization. ')
        return render_template("login.html")
    
    book = getResponseModel().read(id, cust_id)
    return render_template("view.html", cust_id=cust_id, book=book)
예제 #6
0
    def extractFeedbackData_cust(self, cust_id):   
        logging.info ('extractFeedbackData_cust : Started : ' + str(cust_id))
        trainlog = get_model()
        traindata = getTrainingModel() 
        respdata = getResponseModel()
        tickets_learn = tickets_learner()
        
        ticket_struct = []
        trainlog_struct = []
        intent_data = tickets_learn.getTrainingLog(cust_id=cust_id, log_type = 'feedback', done=None)
        len_traindata = 0
        for intent_logs in intent_data:
            len_traindata += len (intent_logs)           
            for intent_log in intent_logs:                
                intents_data = intent_log["json_data"] 
                intents_data_json = json.loads(intents_data)
                selected_response_id = intents_data_json["selected_response_id"]
                selected_response_prob = (intents_data_json["selected_response_prob"]/100) if 'selected_response_prob' in intents_data_json else 0
                cust_id = intents_data_json["ticket_data"]['currentAccount']['subdomain'] 
                id = intents_data_json["ticket_data"]['id']                
                train_data = traindata.read(id, cust_id=cust_id)
                response_data = respdata.read(selected_response_id, cust_id=cust_id)                
                if train_data != None and response_data != None: 
                    '''
                    traindata.update(train_data["tags"], train_data['query'], response_data["response_text"], query_category=train_data['query_category'], 
                                     resp_category=train_data['resp_category'], feedback_resp=response_data['res_category'],
                                     feedback_flag=True, feedback_prob=selected_response_prob, predict_prob = train_data['predict_prob'],
                                     done=True, id=train_data['id'], cust_id=cust_id)
                    '''
                    ticket_struct.append({'id' : train_data['id'], 'query' : train_data['query'], 'query_category' : train_data['query_category'], 
                        'response': response_data["response_text"], 'resp_category': train_data['resp_category'], 
                        'feedback_resp' : response_data['res_category'], 'feedback_flag' : True, 'feedback_prob' : selected_response_prob, 
                        'predict_prob': train_data['predict_prob'] if 'predict_prob' in train_data else 0, 
                        'done' : train_data['done'], 'tags' : train_data['tags']})
                    trainlog_struct.append({'id' : intent_log['id'], 'type': intent_log['type'], 'created': intent_log['created'], 
                                            'json_data': intent_log['json_data'], 'done': False})
                    print('Updating Feedback : ' , id, cust_id)

        logging.info ('No of Training Data Processing : ' + str(len_traindata))
        if (len (ticket_struct) > 1):
            ticket_pd = pd.DataFrame(ticket_struct)
            trainlog_pd = pd.DataFrame(trainlog_struct)
            ticket_pd = ticket_pd.drop_duplicates(subset=['id'], keep='last')
            trainlog_pd = trainlog_pd.drop_duplicates(subset=['id'], keep='last')
            traindata.batchUpdate(ticket_pd, cust_id)
            trainlog.batchUpdate(trainlog_pd, cust_id)
        logging.info ('extractFeedbackData_cust : Completed : ' + str(cust_id))  
예제 #7
0
    def populateResponseData(self, cust_id):
        logging.info('populateResponseData : Started ' + str(cust_id))
        try:
            tx = self.ticket_pd['response_cluster']
        except KeyError as err:
            logging.error("populateResponseData : " + str(err))
            return

        resp_model = getResponseModel()

        last_id = 9999
        tickets_learn = tickets_learner()
        resp_data = tickets_learn.getResponseData(cust_id=cust_id,
                                                  modifiedflag=False,
                                                  defaultflag=False)
        for resp_logs in resp_data:
            for resp_log in resp_logs:
                resp_model.delete(resp_log['id'], cust_id)
                last_id = resp_log['id']

        rep_index = int(last_id) + 1
        for index, item in self.ticket_pd.iterrows():
            if item['select_response'] == 'true' and item['select_tags'].strip(
            ) != '' and item['response_summary'].strip(
            ) != '' and item['response_tags'] != '':
                resptitle = item['response_title'] if (
                    item['response_title'] != '') else (cust_id +
                                                        '_Response_' +
                                                        str(rep_index))
                resp_model.create(resptitle,
                                  str(cust_id + '_Response_' + str(rep_index)),
                                  item['response_summary'],
                                  item['select_tags'],
                                  item['response_tags'],
                                  done=True,
                                  id=rep_index,
                                  cust_id=cust_id)
                rep_index += 1

        self.removeRespDuplicate(cust_id)

        csvfile = self.ticket_pd.to_csv()
        self.storage.put_bucket(csvfile,
                                str("SmartReply_DataFrame_" + str(cust_id)),
                                filetype='csv')
        logging.info('populateResponseData : Completed ' + str(cust_id))
        return
예제 #8
0
def add():
    cust_id = request.args.get('cust_id', None)
    if cust_id == None or cust_id == '': 
        return render_template("login.html")
    cust_id = cust_id.strip().lower()
    cust = getCustomerModel().authenticate(cust_id)

    if cust == None:            
        logging.error('\'' + cust_id + '\' is invalid customer Organization. ')
        return render_template("login.html")
    
    if request.method == 'POST':
        data = request.form.to_dict(flat=True)

        book = getResponseModel().create(data['resp_name'], data['resp_name'].strip().lower().replace(" ", "_"), data['response_text'], data['tags'], data['tags'], modifiedflag=True, done=True, id=None, cust_id=cust_id)

        traindata = getTrainingModel().create(data['tags'], '', data['response_text'], query_category='', resp_category=data['resp_name'].strip().lower().replace(" ", "_"), done=True, id=None, cust_id=cust_id)

        return redirect(url_for('.view', cust_id=cust_id, id=book['id']))

    return render_template("form.html", cust_id=cust_id, action="Add", book={})
    def uploadArticles():
        logging.info('uploadArticles : ')
        cust_id = ''
        received_data = request.json
        #print (received_data)
        try: 
            cust_id = received_data['ticket_data']['currentAccount']['subdomain']
        except KeyError as err:
            logging.error(err)
            return '200'
        
        cust = getCustomerModel().authenticate(cust_id.strip().lower(), newflag=False)
        if cust == None:
            cust_id = 'default'
        else: 
            cust_id = cust['cust_name']

        logging.info('Customer Id : ' + str(cust_id))
        response_struct = []
        article_data = received_data['article_data']
        respdata = getResponseModel()
        for items in article_data:
            response_text = str(items['body'])
            resp_name = items['title'].split()[:5]
            resp_name = ' '.join(resp_name).title().replace(" ", "_")
            utils_class = UtilityClass()
            response_text = utils_class.cleanhtml(response_text)
            if len(response_text) > 600:                
                response_text = str ('Please find response for your query at following link. \n ' + items['html_url'])
            response_struct.append({'id' : items['id'], 'resp_name': resp_name, 'res_category': str(cust_id + '_Article_' + str(items['id'])), 
                                    'response_text' : response_text, 'tags' : items['title'],
                                    'modifiedflag': False, 'defaultflag' : False, 'resp_tags' : '', 'created': datetime.datetime.utcnow(), 
                                    'done': True})
        if (len (response_struct) > 1):
            response_pd = pd.DataFrame(response_struct)
            respdata.batchUpdate(response_pd, cust_id)
        return '200' 
예제 #10
0
def list():    
    token = request.args.get('page_token', None)
    cust_id = request.args.get('cust_id', None)
    error_msg = request.args.get('error_msg', None)
    if error_msg == None:
        error_msg = ''
    if cust_id == None or cust_id == '': 
        return render_template("login.html")
    cust_id = cust_id.strip().lower()

    cust = getCustomerModel().authenticate(cust_id)
    if cust == None:            
        logging.error('\'' + cust_id + '\' is invalid customer Organization. ')
        return render_template("login.html")
    
    if token:
        token = token.encode('utf-8')

    books, next_page_token = getResponseModel().list(cursor=token, cust_id=cust_id, done=True, modifiedflag=None, defaultflag=None)

    return render_template(
        "list.html", cust_id=cust_id, error_msg=error_msg, 
        books=books,
        next_page_token=next_page_token)