def index(): form = QueryForm() if form.validate_on_submit(): flash('Query required'.format( form.query.data )) return render_template('index.html', title='Home', form=form)
def query(): form = QueryForm() if form.validate_on_submit(): # flash('Search phrase "{}" is going to be analyzed.'.format(form.query.data)) query = form.query.data session['query'] = query = form.query.data return redirect(url_for('statistics')) return render_template('query.html', title='Analyze Tweets', form=form)
def search(): form = QueryForm() page = request.args.get('page', 1, type=int) mode = request.args.get('mode', 'similarity', type=str) form.theme.choices = THEME_CHOICES if form.is_submitted(): app.config['selected_themes'] = selected_themes = form.theme.data if form.query.data != '': books_by_themes = MODEL.get_thematic_books(selected_themes) if selected_themes else None app.config['query'] = form.query.data app.config['ranking'] = QuerySearch(app.config['SELECTED_LANGUAGE'], form.query.data, books_by_themes).get_ranks() else: return render_template('search.html', query='', form=form, model=THEMES, selected_themes=selected_themes) if 'ranking' in app.config: if app.config['ranking'] is None: return render_template('search.html', form=form, model=THEMES, selected_themes=app.config['selected_themes']) form.query.data = app.config['query'] if mode == 'similarity': maximum_pages = math.ceil( len(app.config['ranking']['similarity_rank']) / app.config['BOOKS_PER_PAGE_IN_SEARCH']) if page > maximum_pages: page = maximum_pages ranking_range = calculate_range(len(app.config['ranking']['similarity_rank']), page, maximum_pages) return render_template('search.html', form=form, model=THEMES, mode=mode, selected_themes=app.config['selected_themes'], ranking=app.config['ranking']['similarity_rank'][ranking_range[0]:ranking_range[1]], page=page, maximum_pages=maximum_pages) else: if app.config['SELECTED_LANGUAGE'] == 'en': ranking = list(reversed(app.config['ranking']['readability_rank'])) else: ranking = app.config['ranking']['readability_rank'] maximum_pages = math.ceil(len(ranking) / app.config['BOOKS_PER_PAGE']) if page > maximum_pages: page = maximum_pages ranking_range = calculate_range(len(ranking), page, maximum_pages) return render_template('search.html', form=form, model=THEMES, mode=mode, selected_themes=app.config['selected_themes'], ranking=ranking[ranking_range[0]:ranking_range[1]], page=page, maximum_pages=maximum_pages) else: return render_template('search.html', query='', form=form, model=THEMES)
def index(): ''' Handles the route for the index page.\n Serves the html and handles the requests for the flask wtf forms\n on the index page used to input the server ip and port. ''' form = QueryForm() if form.validate_on_submit(): return redirect(f'/query/{form.server_ip.data}:{form.server_port.data}/') return render_template('form.html', form=form)
def query(): form = QueryForm() if form.validate_on_submit(): campaign_number = form.campaign_number.data table_name = form.table_name.data start_time = form.start_time.data end_time = form.end_time.data url = 'http://localhost:5000/datatable?campaign_number=%s&table_name=%s&start_time=%s&end_time=%s' % ( campaign_number, table_name, start_time, end_time) return redirect(url) #flash() return render_template('query.html', title='Input Query', form=form)
def op_simulation(): query = QueryForm(request.form) if query.validate(): result = { 'errors': {}, 'result': ops.run(query) } else: result = { 'errors': query.errors, 'result': 'There are errors with your query.' } return Response(json.dumps(result), mimetype='application/json')
def query(): # selectedpart=[Partnum(partnumber='wr30x10093', location='v',quantity=5,description='none'),Partnum(partnumber='rt30x10093', location='va',quantity=56,description='norne')] selectedpart=[] form1 = QueryForm() if form1.validate_on_submit(): partnumber = request.form['partnumber'] location = request.form['location'] if partnumber: selectedpart = Partnum.query.filter_by(partnumber=partnumber).all() elif location: selectedpart = Partnum.query.filter_by(location=location).all() return render_template('query.html', form1=form1,selectedpart=selectedpart,location=location) return render_template('query.html', form1=form1, selectedpart=selectedpart)
def facebook(): form = QueryForm() if form.validate_on_submit(): your_name = form.my_username.data user_requested = form.username.data chat_counts = get_chats_and_messages(user_requested) message_count = get_message_count(user_requested) chat_participation = get_chat_participation(user_requested) chat_count_dict = get_chat_count_dict() total_p = sum(chat_participation.values()) my_part = get_chat_participation(your_name) return render_template('count.html', title= 'Facebook Message Counter', your_name = your_name, message_count = message_count, other_user = user_requested, chat_counts = chat_counts, chat_participation = chat_participation, chat_count_dict = chat_count_dict, total_p = total_p, my_part = my_part) #redirect(url_for('index')) return render_template('facebook.html', title='Facebook Message Counter', form=form)
def landing_view(request): name = 'Foo Bar' context = RequestContext(request) t = get_template('index.html') form = QueryForm() if request.POST: form = QueryForm(request.POST) if form.is_valid(): query = form.save() else: print 'not valid', form.errors #return render_to_response('landing.html','form':form,context_instance=RequestContext(request)) return render_to_response('landing.html', context)
def fetch_room_list(): cleaned_data = { 'csrf_token': request.args.get('csrf_token'), 'check_in_date' : request.args.get('check_in_date', default=date.today(), type=cast_date), 'check_out_date' : request.args.get('check_out_date', default=date.today(), type=cast_date), 'num_occupants' : request.args.get('num_occupants', default=1, type=int), 'room_type' : request.args.get('room_type') } f = QueryForm(data=cleaned_data) if f.validate(): rooms = Room.fetch_room_to_query(cleaned_data) session['check_in_date'] = request.args.get('check_in_date') session['check_out_date'] = request.args.get('check_out_date') session['num_occupants'] = cleaned_data['num_occupants'] return render_template('roomSearchResults.html', rooms=rooms) return 404
def query(): """Function for rendering the query form""" try: error = None db_connection = DbConnect(current_app.config) biomimic_type_choices = db_connection.fetch_biomimic_types() db_connection.close() form = QueryForm(request.form) form.biomimic_type.choices = biomimic_type_choices session['query'] = dict() if request.method == 'GET': form.process() else: pass return render_template('query.html', title='Query', form=form, error=error) except TemplateNotFound: abort(404)
def index(): form = QueryForm() # form submission actions if form.validate_on_submit(): # query yelp api yelp_results = search(API_KEY, form.food_type.data, form.location.data) total = len(yelp_results['businesses']) # if no results, redirect to error page if total == 0: return render_template('sorry.html') else: rand_restaurant = random.randint(0, total - 1) return render_template( 'results.html', yelp_results=yelp_results['businesses'][rand_restaurant]) return render_template('index.html', form=form)
def index(): result = db.session.query(summoners).all() form = QueryForm() form.summoner.choices = [(r.account_id, r.summoner_name) for r in result] if form.validate_on_submit(): account_id = form.summoner.data queue = form.queue.data begin_date = form.begin_date.data # begin date is an optional field and may not have a value if begin_date is None: time = 0 else: time = datetime.datetime(year=int(begin_date.year), month=int(begin_date.month), day=int(begin_date.day))\ .timestamp() * 1000 stat = form.stat.data stat_total = stat_fetcher.get_stat_total(stat, account_id, time, queue) if stat in stat_fetcher.by_champion_stats: stat_by_champ = stat_fetcher.get_stat_by_champ(stat, account_id, time, queue) stat_by_champ_arr = [] for r in stat_by_champ: stat_by_champ_arr.append({ 'champion': champion_parser.get_champion_name(r.champion), 'stat': r.stat, 'amount': r.amount }) else: stat_by_champ_arr = None # these parse the second part of the tuples in the choices that are more human readable stat_desc = [item[1] for item in form.stat.choices if item[0] == form.stat.data][0] summoner_name = [item[1] for item in form.summoner.choices if item[0] == account_id][0] else: stat_total = None stat_by_champ_arr = None stat_desc = None summoner_name = None return render_template('index.html', form=form, stat_total=stat_total, stat_by_champ=stat_by_champ_arr, stat_desc=stat_desc, summoner_name=summoner_name)
def runQuery(): form = QueryForm() results = [] if form.validate_on_submit(): querytype = form.querytype.data prov_docdb = ArtID.query.filter_by(artname=form.artname.data).first() if prov_docdb is None: flash('There is no provenance for this artwork') else: prov_doc = api.document.get(prov_docdb.docid) provn_doc = prov_doc.prov currentdir = os.getcwd() filename = form.artname.data.replace(" ", "") outputfile = currentdir + '/app/rdfqueryfiles/' + filename + '.rdf' provn_doc.serialize(outputfile, format='rdf', rdf_format='ttl') querystr = "" if querytype == 'location': querystr = location elif querytype == 'specialist': querystr = specialist elif querytype == 'hammerprice': querystr = hammerprice elif querytype == 'auctionhouse': querystr = auctionhouse elif querytype == 'gallery': querystr = gallery elif querytype == 'buyer': querystr = buyer g = rdflib.Graph() g.parse(outputfile, format='n3') qres = g.query(querystr) for row in qres: results.append(row) return render_template('query.html', title='Queries', form=form, results=results)
def index(): form2 = UpdateForm() form1 = QueryForm() form = PartForm() return render_template('index.html', title='Home',form=form)
def result(): """ Based on form data, perform the appropriate search or conversion operation and return results. """ if request.method == 'POST': print("\n>>>>>POST request received. Processing...") # Move form data into a dict form = { 'query': str(request.form.get('query')), 'have': request.form.get('have'), 'want': request.form.get('want') } RESULT_TYPES = ['nt', 'pro', 'hits', 'wiki'] # Create a dict of results, initialized to None results = {each: None for each in RESULT_TYPES} # If a radio button is unselected, return to a fresh Query page. if not (form['have'] and form['want']): return render_template( 'query.html', form=QueryForm(), error_message="You must choose a Have and a Want.") """ # This functionality will be reworked into the Results table. # Based on the form entry, update the "results" accordingly if form['want'] == 'wiki': # Wikipedia search results results['wiki'] = bio_pipelines.WikiSearch( form['query']).get_hrefs() """ # Have nt, want nt (do nothing) if form['have'] == 'nt' and form['want'] == 'nt': results['nt'] = Seq(form['query']) # Have nt, want pro (translation) elif form['have'] == 'nt' and form[ 'want'] == 'pro': # Protein translation print(f"Translating nucleic acid string to protein...") form['query'] = form['query'].replace('\r', '') if form['query'][0] == '>': # Check if FASTA-formatted print('>>>>>FASTA format detected') seq = ''.join( [line.strip() for line in form['query'].split('\n')][1:]) form['query'] = Seq(seq) else: print('>>>>>Sequence not in FASTA format. Sanitizing...') seq = ''.join( [line.strip() for line in form['query'].split('\n')]) form['query'] = Seq(seq) print(f">>>>>Now translating sequence:\n{form['query']}") results['pro'] = form['query'].translate() # Have nt, want BLAST (blastn) elif form['have'] == 'nt' and form['want'] == 'hits': # BLAST hits print(f"Getting BLAST data using input: {form['query']}") results['hits'] = bio_pipelines.BLASTSearch( query=form['query'], data_type=form['have']).data_table # Have pro, want nt (tblastn) elif form['have'] == 'pro' and form['want'] == 'nt': # BLAST hits print(f"Getting BLAST data using input: {form['query']}") results['hits'] = bio_pipelines.BLASTSearch( query=form['query'], data_type='pro-nt').data_table # Have pro, want BLAST (blastp) elif form['have'] == 'pro' and form['want'] == 'hits': # BLAST hits print(f"Getting BLAST data using input: {form['query']}") results['hits'] = bio_pipelines.BLASTSearch( query=form['query'], data_type=form['have']).data_table print(f">>>>>POST request received. Rendering result.html...") # Return results to render_template for HTML display return render_template('result.html', title='Results', query=form['query'], have=form['have'], want=form['want'], nucleotide_seq=results['nt'], protein_seq=results['pro'], blast_results=results['hits'], wiki_results=results['wiki']) else: return redirect('index')
def index(request): form = QueryForm() """ Pagination and data view logic """ host = request.get_host() page = request.GET.get('page') if page is not None: page = int(page) r = requests.get('http://%s/app/v1/tracks/' % host) total_pages, has_other_pages = get_pages(r.json()['count']) if total_pages >= page > 0: r = requests.get('http://%s/app/v1/tracks/?page=%d' % (host, page)) result = r.json()['results'] elif page < 0: r = requests.get('http://%s/app/v1/tracks/' % host) result = r.json()['results'] elif page is None: r = requests.get('http://%s/app/v1/tracks/' % host) result = r.json()['results'] else: r = requests.get('http://%s/app/v1/tracks/?page=%d' % (host, total_pages)) result = r.json()['results'] for i in result: i['rating'] = int(float(i['rating'])) i['rating_empty'] = range(5 - i['rating']) i['rating'] = range(i['rating']) for i in result: lis = [] for j in i['genres']: lis.append(j['name']) i['genre_list'] = " | ".join(lis) if request.GET.get('name'): name = request.GET.get('name') r = requests.get('http://%s/app/v1/tracks/?title=%s' % (host, name)) result = r.json()['results'] for i in result: i['rating'] = int(float(i['rating'])) i['rating_empty'] = range(5 - i['rating']) i['rating'] = range(i['rating']) for i in result: lis = [] for j in i['genres']: lis.append(j['name']) i['genre_list'] = " | ".join(lis) count = len(result) return render(request, 'app/index.html', { 'result': result, 'count': count, 'form': form }) count = len(result) total_pages = range(1, total_pages + 1) pagination = get_pagination_fields(r) pagination['total_pages'] = total_pages pagination['has_other_pages'] = has_other_pages return render(request, 'app/index.html', { 'form': form, 'count': count, 'result': result, 'pagination': pagination })
def index(): print(app.root_path) form = QueryForm() if form.validate_on_submit(): #search_term="Jimmy Hendrix" search_term = form.the_wik_search.data wik_page = wikipedia.search(search_term, results=1) try: p = wikipedia.page(wik_page[0]) except wikipedia.exceptions.DisambiguationError as e: #print(e.options) p = wikipedia.page(e.options[0]) paragraph_text = p.content # Content of page. #paragraph_text=paragraph_text[:350]# wik_url = p.url #print(wik_url) #query="When was he born?" query = form.the_query.data #paragraph_text=form.the_document.data def is_whitespace(c): if c == " " or c == "\t" or c == "\r" or c == "\n" or ord( c) == 0x202F: return True return False doc_tokens = [] char_to_word_offset = [] prev_is_whitespace = True for c in paragraph_text: if is_whitespace(c): prev_is_whitespace = True else: if prev_is_whitespace: doc_tokens.append(c) else: doc_tokens[-1] += c prev_is_whitespace = False char_to_word_offset.append(len(doc_tokens) - 1) doc_tokens = doc_tokens[:1000] # total_num_doc_tokens=len(doc_tokens) # cutup_doc_tokens=[] # start_token_inds=np.arange(0,total_num_doc_tokens-500,250).tolist() # num_batches=len(start_token_inds) #doc_tokens=doc_tokens[:500] #for batch_num in range(num_batches): # batch_num=0 # start_tok_ind=start_token_inds[batch_num] # batch_doc_tokens=doc_tokens[start_tok_ind:start_tok_ind+1500] #eval_examples is a list of 10570 'SquadExample' objects eval_examples_routes = [ run_squad.SquadExample(qas_id=0, question_text=query, doc_tokens=doc_tokens, orig_answer_text=None, start_position=None, end_position=None) ] eval_features_routes = run_squad.convert_examples_to_features( examples=eval_examples_routes, tokenizer=tokenizer, max_seq_length=400, #384, doc_stride=300, #128, max_query_length=64, is_training=False) #print(eval_features_routes) #all_input_ids, all_input_mask, and all_segment_ids are Tensors w/ size([100, 384]) #all_example_index is just list w/ #s 0:99 input_ids_routes = torch.tensor( [f.input_ids for f in eval_features_routes], dtype=torch.long) input_mask_routes = torch.tensor( [f.input_mask for f in eval_features_routes], dtype=torch.long) segment_ids_routes = torch.tensor( [f.segment_ids for f in eval_features_routes], dtype=torch.long) example_index_routes = torch.arange(input_ids_routes.size(0), dtype=torch.long) eval_data_routes = TensorDataset(input_ids_routes, input_mask_routes, segment_ids_routes, example_index_routes) model.eval() #input_ids_routes=all_input_ids_routes #input_mask_routes=all_input_mask_routes #segment_ids_routes=all_segment_ids_routes #example_indices_routes=all_example_index_routes input_ids_routes = input_ids_routes.to(device) input_mask_routes = input_mask_routes.to(device) segment_ids_routes = segment_ids_routes.to(device) #batch_start_logits and batch_end_logits are both size [bs,384] with torch.no_grad(): batch_start_logits_routes, batch_end_logits_routes = model( input_ids_routes, segment_ids_routes, input_mask_routes) #THIS SECTION TRYING A NEW APPROACH #THIS SECTION TRYING A NEW APPROACH #THIS SECTION TRYING A NEW APPROACH #THIS SECTION TRYING A NEW APPROACH RawResult = collections.namedtuple( "RawResult", ["unique_id", "start_logits", "end_logits"]) _PrelimPrediction = collections.namedtuple( # pylint: disable=invalid-name "PrelimPrediction", [ "feature_index", "start_index", "end_index", "start_logit", "end_logit" ]) predict_batch_size = 8 all_results = [] for i, example_index in enumerate(example_index_routes): #start_logits and end_logits are both lists of len 384 start_logits_routes = batch_start_logits_routes[i].detach().cpu( ).tolist() end_logits_routes = batch_end_logits_routes[i].detach().cpu( ).tolist() eval_feature_routes = eval_features_routes[example_index.item()] unique_id_routes = int(eval_feature_routes.unique_id) all_results.append( RawResult(unique_id=unique_id_routes, start_logits=start_logits_routes, end_logits=end_logits_routes)) unique_id_to_result = {} for result in all_results: unique_id_to_result[result.unique_id] = result #n_best_size: the total number of n-best predictions to generate in the nbest_predictions.json n_best_size = 20 max_answer_length = 30 prelim_predictions = [] for (feature_index, feature) in enumerate(eval_features_routes): result = unique_id_to_result[feature.unique_id] start_indexes = run_squad._get_best_indexes( result.start_logits, n_best_size) end_indexes = run_squad._get_best_indexes(result.end_logits, n_best_size) for start_index in start_indexes: for end_index in end_indexes: # We could hypothetically create invalid predictions, e.g., predict # that the start of the span is in the question. We throw out all # invalid predictions. if start_index >= len(feature.tokens): continue if end_index >= len(feature.tokens): continue if start_index not in feature.token_to_orig_map: continue if end_index not in feature.token_to_orig_map: continue if not feature.token_is_max_context.get( start_index, False): continue if end_index < start_index: continue length = end_index - start_index + 1 if length > max_answer_length: continue prelim_predictions.append( _PrelimPrediction( feature_index=feature_index, start_index=start_index, end_index=end_index, start_logit=result.start_logits[start_index], end_logit=result.end_logits[end_index])) #prelim_predictions is a list of PrelimPrediction's #example: PrelimPrediction(feature_index=0, start_index=30, end_index=31, start_logit=7.1346, end_logit=5.40855) prelim_predictions = sorted(prelim_predictions, key=lambda x: (x.start_logit + x.end_logit), reverse=True) _NbestPrediction = collections.namedtuple( # pylint: disable=invalid-name "NbestPrediction", ["text", "start_logit", "end_logit"]) seen_predictions = {} nbest = [] for pred in prelim_predictions: if len(nbest) >= n_best_size: break feature = eval_features_routes[pred.feature_index] tok_tokens = feature.tokens[pred.start_index:(pred.end_index + 1)] orig_doc_start = feature.token_to_orig_map[pred.start_index] orig_doc_end = feature.token_to_orig_map[pred.end_index] orig_tokens = doc_tokens[orig_doc_start:(orig_doc_end + 1)] tok_text = " ".join(tok_tokens) # De-tokenize WordPieces that have been split off. tok_text = tok_text.replace(" ##", "") tok_text = tok_text.replace("##", "") # Clean whitespace tok_text = tok_text.strip() tok_text = " ".join(tok_text.split()) orig_text = " ".join(orig_tokens) final_text = run_squad.get_final_text(tok_text, orig_text, do_lower_case=True, verbose_logging=False) if final_text in seen_predictions: continue seen_predictions[final_text] = True nbest.append( _NbestPrediction(text=final_text, start_logit=pred.start_logit, end_logit=pred.end_logit)) # In very rare edge cases we could have no valid predictions. So we # just create a nonce prediction in this case to avoid failure. if not nbest: nbest.append( _NbestPrediction(text="empty", start_logit=0.0, end_logit=0.0)) assert len(nbest) >= 1 total_scores = [] for entry in nbest: total_scores.append(entry.start_logit + entry.end_logit) probs = run_squad._compute_softmax(total_scores) #print(nbest[0].text) #END SECTION TRYING A NEW APPROACH #END SECTION TRYING A NEW APPROACH #END SECTION TRYING A NEW APPROACH #END SECTION TRYING A NEW APPROACH the_answer = nbest[0].text # #tokenized input # document_tokens = tokenizer.tokenize(form.the_document.data) # query_tokens = tokenizer.tokenize(form.the_query.data) # all_tokens= ['[CLS]'] + query_tokens + ['[SEP]'] + document_tokens + ['[SEP]'] # # Convert tokens to vocabulary indices # all_indices= tokenizer.convert_tokens_to_ids(all_tokens) # # Define sentence A and B indices associated to 1st and 2nd sentences # query_segids = [0 for i in range(len(query_tokens) +1 )] # document_segids = [1 for i in range(len(document_tokens) +2 )] # all_segids= query_segids + document_segids # assert len(all_segids) == len(all_indices) # # Convert inputs to PyTorch tensors # tokens_tensor = torch.tensor([all_indices]) # segments_tensors = torch.tensor([all_segids]) # input_mask = torch.tensor([1 for i in range(len(all_segids))]) # input_mask=input_mask.view(1,-1) # # Predict all tokens # with torch.no_grad(): #https://discuss.pytorch.org/t/model-eval-vs-with-torch-no-grad/19615/8 # start_logits, end_logits = model(tokens_tensor, segments_tensors,input_mask) # start_ind=torch.argmax(start_logits).item() # end_ind=torch.argmax(end_logits).item() #the_answer=all_tokens[start_ind:end_ind+1] return render_template('index.html', title='Home', form=form, wik_url=wik_url, the_wik_search=form.the_wik_search.data, the_query=form.the_query.data, the_answer=the_answer) #flash('Your Query: {}'.format( # form.the_query.data)) #flash('The Document: {}'.format( # form.the_document.data)) #return redirect('/index') return render_template( 'index.html', title='Home', form=form, wik_url="https://en.wikipedia.org/wiki/Janis_Joplin", the_wik_search=None, the_query=None, the_answer="January 19, 1943")
def index(): form = QueryForm() if form.validate_on_submit(): answer = Answer().answer_user_query(form.query.data) return jsonify(answer) return render_template('index.html', form=form)
def index(): form = QueryForm() if form.validate_on_submit(): return redirect( url_for("stats_fetch", location=f"{form.location.data}")) return render_template("index.html", title="Home", form=form)
def query(): form = QueryForm() return render_template('query.html', title='Submit a Query', form=form)