def translate(request, ip_address): template = loader.get_template('translator/translate.html') if request.method == 'POST': request_str = request.POST.get('request_str') else: request_str = request.GET.get('request_str') if not request_str or not request_str.strip(): return redirect('/') while request_str.endswith('/'): request_str = request_str[:-1] trans_list = [] html_strs = [] if CACHE_TRANSLATIONS and NLRequest.objects.filter( request_str=request_str).exists(): # if the natural language request string has been translated before, # directly output previously cached translations if Translation.objects.filter( request__request_str=request_str).exists(): # model translations exist cached_trans = Translation.objects.filter( request__request_str=request_str) for trans in cached_trans: print(trans.pred_cmd) pred_tree = data_tools.bash_parser(trans.pred_cmd) if pred_tree is not None: trans_list.append(trans) html_str = tokens2html(pred_tree) html_strs.append(html_str) try: nl_request = NLRequest.objects.get(request_str=request_str) except ObjectDoesNotExist: nl_request = NLRequest.objects.create(request_str=request_str) try: user = User.objects.get(ip_address=ip_address) except ObjectDoesNotExist: r = requests.get('http://ipinfo.io/{}/json'.format(ip_address)) organization = r.json()['org'] city = r.json()['city'] region = r.json()['region'] country = r.json()['country'] user = User.objects.create( ip_address=ip_address, organization=organization, city=city, region=region, country=country ) # check if the natural language request has been issued by the IP # address before # if not, save the natural language request issued by this IP Address if not NLRequestIPAddress.objects.filter( request=nl_request, user=user).exists(): NLRequestIPAddress.objects.create( request=nl_request, user=user) if not trans_list: if not WEBSITE_DEVELOP: # call learning model and store the translations batch_outputs, output_logits = translate_fun(request_str) if batch_outputs: top_k_predictions = batch_outputs[0] top_k_scores = output_logits[0] for i in range(len(top_k_predictions)): pred_tree, pred_cmd, outputs = top_k_predictions[i] score = top_k_scores[i] trans = Translation.objects.create( request=nl_request, pred_cmd=pred_cmd, score=score) trans_list.append(trans) html_str = tokens2html(pred_tree) html_strs.append(html_str) translation_list = [] for trans, html_str in zip(trans_list, html_strs): upvoted, downvoted, starred = "", "", "" if Vote.objects.filter(translation=trans, ip_address=ip_address).exists(): v = Vote.objects.get(translation=trans, ip_address=ip_address) upvoted = 1 if v.upvoted else "" downvoted = 1 if v.downvoted else "" starred = 1 if v.starred else "" translation_list.append((trans, upvoted, downvoted, starred, trans.pred_cmd.replace('\\', '\\\\'), html_str)) # sort translation_list based on voting results translation_list.sort( key=lambda x: x[0].num_votes + x[0].score, reverse=True) context = { 'nl_request': nl_request, 'trans_list': translation_list } return HttpResponse(template.render(context, request))
def translate(request, ip_address): template = loader.get_template('translator/translate.html') if request.method == 'POST': request_str = request.POST.get('request_str') else: request_str = request.GET.get('request_str') if not request_str or not request_str.strip(): return redirect('/') while request_str.endswith('/'): request_str = request_str[:-1] # check if the natural language request is in the database nl = get_nl(request_str) trans_list = [] annotated_trans_list = [] if CACHE_TRANSLATIONS and \ Translation.objects.filter(nl=nl).exists(): # model translations exist cached_trans = Translation.objects.filter(nl=nl).order_by('score') count = 0 for trans in cached_trans: pred_tree = data_tools.bash_parser(trans.pred_cmd.str) if pred_tree is not None: trans_list.append(trans) annotated_trans_list.append(tokens2html(pred_tree)) count += 1 if count >= NUM_TRANSLATIONS: break # check if the user is in the database try: user = User.objects.get(ip_address=ip_address) except ObjectDoesNotExist: if ip_address == '123.456.789.012': organization = '' city = '--' region = '--' country = '--' else: r = requests.get('http://ipinfo.io/{}/json'.format(ip_address)) organization = '' if r.json()['org'] is None else r.json()['org'] city = '--' if r.json()['city'] is None else r.json()['city'] region = '--' if r.json()['region'] is None else r.json()['region'] country = '--' if r.json()['country'] is None else r.json( )['country'] user = User.objects.create(ip_address=ip_address, organization=organization, city=city, region=region, country=country) # save the natural language request issued by this IP Address nl_request = NLRequest.objects.create(nl=nl, user=user) if not trans_list: if not WEBSITE_DEVELOP: # call learning model and store the translations batch_outputs, output_logits = translate_fun(request_str) if batch_outputs: top_k_predictions = batch_outputs[0] top_k_scores = output_logits[0] for i in range(len(top_k_predictions)): pred_tree, pred_cmd = top_k_predictions[i] score = top_k_scores[i] cmd = get_command(pred_cmd) trans_set = Translation.objects.filter(nl=nl, pred_cmd=cmd) if not trans_set.exists(): trans = Translation.objects.create(nl=nl, pred_cmd=cmd, score=score) else: for trans in trans_set: break trans.score = score trans.save() trans_list.append(trans) start_time = time.time() annotated_trans_list.append(tokens2html(pred_tree)) print(time.time() - start_time) start_time = time.time() translation_list = [] for trans, annotated_cmd in zip(trans_list, annotated_trans_list): upvoted, downvoted, starred = "", "", "" if Vote.objects.filter(translation=trans, ip_address=ip_address).exists(): v = Vote.objects.get(translation=trans, ip_address=ip_address) upvoted = 1 if v.upvoted else "" downvoted = 1 if v.downvoted else "" starred = 1 if v.starred else "" translation_list.append( (trans, upvoted, downvoted, starred, trans.pred_cmd.str.replace('\\', '\\\\'), annotated_cmd)) # sort translation_list based on voting results translation_list.sort(key=lambda x: x[0].num_votes + x[0].score, reverse=True) context = {'nl_request': nl_request, 'trans_list': translation_list} return HttpResponse(template.render(context, request))