Spaces:
Build error
Build error
print fix
Browse filesFiles changed (2)
- app.py+1-1
- src/Surveyor.py+30-31
27 unmodified lines282930313233347 unmodified lines42434445464727 unmodified linesdef survey_space(surveyor):st.container().title('Auto-Research V0.1 - Automated Survey generation from research keywords')form = st.sidebar.form(key='survey_form')research_keywords = form.text_input("What would you like to research in today?")max_search = form.number_input("num_papers_to_search", help="maximium number of papers to glance through - defaults to 20",7 unmodified linesif __name__ == '__main__':global surveyorsurveyor_obj = Surveyor(print_fn=st.write)survey_space(surveyor_obj)27 unmodified lines2829303132337 unmodified lines4142434445464727 unmodified linesdef survey_space(surveyor):form = st.sidebar.form(key='survey_form')research_keywords = form.text_input("What would you like to research in today?")max_search = form.number_input("num_papers_to_search", help="maximium number of papers to glance through - defaults to 20",7 unmodified linesif __name__ == '__main__':st.container().title('Auto-Research V0.1 - Automated Survey generation from research keywords')global surveyorsurveyor_obj = Surveyor(print_fn=st.write)survey_space(surveyor_obj)
74 unmodified lines757677787980818283848567 unmodified lines15315415515615715815975 unmodified lines2352362372382392402412422434 unmodified lines2482492502512522532542552562572582592602612622636 unmodified lines27027127227327427527656 unmodified lines333334335336337338339340341342343424 unmodified lines76876977077177277377419 unmodified lines794795796797798799800801802803804805806807249 unmodified lines1057105810591060106110621063127 unmodified lines11911192119311941195119611971 unmodified line119912001201120212031204120536 unmodified lines1242124312441245124612471248105 unmodified lines13541355135613571358135913601361136213636 unmodified lines1370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139227 unmodified lines14201421142214231424142514262 unmodified lines14291430143114321433143414351 unmodified line143714381439144014411442144311 unmodified lines145514561457145814591460146110 unmodified lines147214731474147514761477147874 unmodified linesself.print_fn = printif print_fn is not None:self.print_fn = print_fnself.torch_device = 'cpu'self.print_fn("\nTorch_device: " + self.torch_device)if torch.cuda.is_available():self.print_fn("\nloading defaults for gpu")self.torch_device = 'cuda'spacy.require_gpu()67 unmodified linesif not no_save_models:self.embedder.save(models_dir + "/embedder")else:self.print_fn("\nInitializing from previously saved models at" + models_dir)self.title_tokenizer = AutoTokenizer.from_pretrained(title_model_name)self.title_model = AutoModelForSeq2SeqLM.from_pretrained(models_dir + "/title_model").to(self.torch_device)self.title_model.eval()75 unmodified linespapers = papers_meta[:self.num_papers]selected_papers = papersself.print_fn("\nFirst stage paper collection...")ids_none, papers, cites = self.fetch_papers(dump_dir, img_dir, papers, pdf_dir, tab_dir, txt_dir)self.print_fn("\nFirst stage paper collection complete, papers collected: \n" + ', '.join([p['id'] for p in papers]))new_papers = papers_meta[self.num_papers : self.num_papers + len(ids_none)]_ = self.get_freq_cited(cites)'''4 unmodified linesnew_papers.extend(new_searched_papers)'''selected_papers.extend(new_papers)self.print_fn("\nSecond stage paper collection...")_, new_papers, _ = self.fetch_papers(dump_dir, img_dir, new_papers, pdf_dir, tab_dir, txt_dir, repeat=True)self.print_fn("\nSecond stage paper collection complete, new papers collected: \n" + ', '.join([p['id'] for p in new_papers]))papers.extend(new_papers)joblib.dump(papers, dump_dir + 'papers_extracted_pdf_route.dmp')copy_tree(img_dir, dump_dir + os.path.basename(img_dir))copy_tree(tab_dir, dump_dir + os.path.basename(tab_dir))self.print_fn("\nExtracting section-wise highlights.. ")papers = self.extract_highlights(papers)return papers, selected_papers6 unmodified lines[cites_list.append(val) for val in v]cite_freqs = {cite: cites_list.count(cite) for cite in set(cites_list)}sorted_cites = dict(sorted(cite_freqs.items(), key=lambda item: item[1], reverse=True)[:5])self.print_fn("\nThe most cited paper ids are:\n" + str(sorted_cites))return sorted_cites.keys()56 unmodified linesdef build_doc(self, research_sections, papers, query=None, filename='survey.txt'):import arxiv2bibself.print_fn("\nbuilding bibliography entries.. ")bibentries = arxiv2bib.arxiv2bib([p['id'] for p in papers])bibentries = [r.bibtex() for r in bibentries]self.print_fn("\nbuilding final survey file .. at "+ filename)file = open(filename, 'w+')if query is None:query = 'Internal(existing) research'424 unmodified linesres = self.model(" ".join([l.lower() for l in lines]), ratio=0.5, )res_doc = self.nlp(res)res_lines = set([str(sent) for sent in list(res_doc.sents)])# self.print_fn("\n".join(res_sents))with torch.no_grad():keywords = self.kw_model.extract_keywords(str(" ".join([l.lower() for l in lines])), stop_words='english')keyphrases = self.kw_model.extract_keywords(str(" ".join([l.lower() for l in lines])),19 unmodified linesreturn papersdef extract_structure(self, papers, pdf_dir, txt_dir, img_dir, dump_dir, tab_dir, tables=False):self.print_fn("\nextracting sections.. ")papers, ids_none = self.extract_parts(papers, txt_dir, dump_dir)self.print_fn("\nextracting images.. for future correlation use-cases ")papers = self.extract_images(papers, pdf_dir, img_dir)if tables:self.print_fn("\nextracting tables.. for future correlation use-cases ")papers = self.extract_tables(papers, pdf_dir, tab_dir)return papers, ids_none249 unmodified linesfor p in papers:if p['id'] == pid:return pself.print_fn("\npaper not found by file, \nfile: "+file+"\nall papers: "+', '.join([p['id'] for p in papers]))def alpha_length(self, s):127 unmodified lineselse:discarded_ids.append(urlparse(result.entry_id).path.split('/')[-1].split('v')[0])self.print_fn("\nPapers discarded due to id error [arxiv api bug: #74] :\n" + str(discarded_ids))return results, searched_papers1 unmodified lineimport arxivfrom urllib.parse import urlparseids = [p['id'] for p in papers]self.print_fn("\ndownloading below selected papers: ")self.print_fn(ids)# asert(False)papers_filtered = arxiv.Search(id_list=ids).get()36 unmodified linescites = internal_citations.citation_list_parallel(N=multiprocessing.cpu_count(), directory=txt_dir)self.print_fn("\ncitation-network: ")self.print_fn(cites)for p in papers:105 unmodified linesif not num_papers:num_papers = self.DEFAULTS['num_papers']# arxiv api relevance search and data preparationself.print_fn("\nsearching arXiv for top 100 papers.. ")results, searched_papers = self.search(query, max_search=max_search)joblib.dump(searched_papers, self.dump_dir + 'papers_metadata.dmp')self.print_fn("\nfound " + str(len(searched_papers)) + " papers")# paper selection by scibert vector embedding relevance scores# papers_selected = select_papers(searched_papers, query, num_papers=num_papers)6 unmodified linesjoblib.dump(papers_highlighted, self.dump_dir + 'papers_highlighted.dmp')self.print_fn("\nStandardizing known section headings per paper.. ")papers_standardized = self.standardize_headings(papers_highlighted)joblib.dump(papers_standardized, self.dump_dir + 'papers_standardized.dmp')self.print_fn("\nBuilding paper-wise corpus.. ")corpus = self.build_corpus(papers_highlighted, searched_papers)joblib.dump(corpus, self.dump_dir + 'corpus.dmp')self.print_fn("\nBuilding section-wise corpus.. ")corpus_sectionwise = self.build_corpus_sectionwise(papers_standardized)joblib.dump(corpus_sectionwise, self.dump_dir + 'corpus_sectionwise.dmp')self.print_fn("\nBuilding basic research highlights.. ")research_blocks = self.build_basic_blocks(corpus_sectionwise, corpus)joblib.dump(research_blocks, self.dump_dir + 'research_blocks.dmp')self.print_fn("\nReducing corpus to lines.. ")corpus_lines = self.get_corpus_lines(corpus)joblib.dump(corpus_lines, self.dump_dir + 'corpus_lines.dmp')27 unmodified lines'''# self.print_fn("corpus types:"+ str(np.unique([type(txt) for k,txt in corpus.items()])))self.print_fn("\nBuilding abstract.. ")abstract_block = self.get_abstract(corpus_lines, corpus_sectionwise, research_blocks)joblib.dump(abstract_block, self.dump_dir + 'abstract_block.dmp')'''2 unmodified linesself.print_fn(abstract_block)'''self.print_fn("\nBuilding introduction.. ")intro_block = self.get_intro(corpus_sectionwise, research_blocks)joblib.dump(intro_block, self.dump_dir + 'intro_block.dmp')'''1 unmodified lineself.print_fn("intro_block:")self.print_fn(intro_block)'''self.print_fn("\nBuilding custom sections.. ")clustered_sections, clustered_sentences = self.get_clusters(papers_standardized, searched_papers)joblib.dump(clustered_sections, self.dump_dir + 'clustered_sections.dmp')joblib.dump(clustered_sentences, self.dump_dir + 'clustered_sentences.dmp')11 unmodified linesclustered_sections['introduction'] = intro_blockjoblib.dump(clustered_sections, self.dump_dir + 'research_sections.dmp')self.print_fn("\nBuilding conclusion.. ")conclusion_block = self.get_conclusion(clustered_sections)joblib.dump(conclusion_block, self.dump_dir + 'conclusion_block.dmp')clustered_sections['conclusion'] = conclusion_block10 unmodified linesshutil.copy(self.dump_dir + survey_file, survey_file)assert (os.path.exists(survey_file))output_zip = self.zip_outputs(self.dump_dir, query)self.print_fn("\nSurvey complete.. \nSurvey file path :" + os.path.abspath(survey_file) + "\nAll outputs zip path :" + os.path.abspath(self.dump_dir + output_zip))return os.path.abspath(self.dump_dir + output_zip), os.path.abspath(survey_file)74 unmodified lines7576777879808182838467 unmodified lines15215315415515615715875 unmodified lines2342352362372382392402412424 unmodified lines2472482492502512522532542552562572582592602612626 unmodified lines26927027127227327427556 unmodified lines332333334335336337338339340341342424 unmodified lines76776876977077177277319 unmodified lines793794795796797798799800801802803804805806249 unmodified lines1056105710581059106010611062127 unmodified lines11901191119211931194119511961 unmodified line119811991200120112021203120436 unmodified lines1241124212431244124512461247105 unmodified lines13531354135513561357135813591360136113626 unmodified lines1369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139127 unmodified lines14191420142114221423142414252 unmodified lines14281429143014311432143314341 unmodified line143614371438143914401441144211 unmodified lines145414551456145714581459146010 unmodified lines147114721473147414751476147774 unmodified linesself.print_fn = printif print_fn is not None:self.print_fn = print_fnself.torch_device = 'cpu'self.print_fn("\nTorch_device: " + self.torch_device)if torch.cuda.is_available():self.torch_device = 'cuda'spacy.require_gpu()67 unmodified linesif not no_save_models:self.embedder.save(models_dir + "/embedder")else:self.print_fn("\n-Initializing from previously saved models at" + models_dir)self.title_tokenizer = AutoTokenizer.from_pretrained(title_model_name)self.title_model = AutoModelForSeq2SeqLM.from_pretrained(models_dir + "/title_model").to(self.torch_device)self.title_model.eval()75 unmodified linespapers = papers_meta[:self.num_papers]selected_papers = papersself.print_fn("\n-First stage paper collection...")ids_none, papers, cites = self.fetch_papers(dump_dir, img_dir, papers, pdf_dir, tab_dir, txt_dir)self.print_fn("\n-First stage paper collection complete, papers collected: \n" + ', '.join([p['id'] for p in papers]))new_papers = papers_meta[self.num_papers : self.num_papers + len(ids_none)]_ = self.get_freq_cited(cites)'''4 unmodified linesnew_papers.extend(new_searched_papers)'''selected_papers.extend(new_papers)self.print_fn("\n-Second stage paper collection...")_, new_papers, _ = self.fetch_papers(dump_dir, img_dir, new_papers, pdf_dir, tab_dir, txt_dir, repeat=True)self.print_fn("\n-Second stage paper collection complete, new papers collected: \n" + ', '.join([p['id'] for p in new_papers]))papers.extend(new_papers)joblib.dump(papers, dump_dir + 'papers_extracted_pdf_route.dmp')copy_tree(img_dir, dump_dir + os.path.basename(img_dir))copy_tree(tab_dir, dump_dir + os.path.basename(tab_dir))self.print_fn("\n-Extracting section-wise highlights.. ")papers = self.extract_highlights(papers)return papers, selected_papers6 unmodified lines[cites_list.append(val) for val in v]cite_freqs = {cite: cites_list.count(cite) for cite in set(cites_list)}sorted_cites = dict(sorted(cite_freqs.items(), key=lambda item: item[1], reverse=True)[:5])self.print_fn("\n-The most cited paper ids are:\n" + str(sorted_cites))return sorted_cites.keys()56 unmodified linesdef build_doc(self, research_sections, papers, query=None, filename='survey.txt'):import arxiv2bibself.print_fn("\n-building bibliography entries.. ")bibentries = arxiv2bib.arxiv2bib([p['id'] for p in papers])bibentries = [r.bibtex() for r in bibentries]self.print_fn("\n-building final survey file .. at "+ filename)file = open(filename, 'w+')if query is None:query = 'Internal(existing) research'424 unmodified linesres = self.model(" ".join([l.lower() for l in lines]), ratio=0.5, )res_doc = self.nlp(res)res_lines = set([str(sent) for sent in list(res_doc.sents)])# self.print_fn("\n-".join(res_sents))with torch.no_grad():keywords = self.kw_model.extract_keywords(str(" ".join([l.lower() for l in lines])), stop_words='english')keyphrases = self.kw_model.extract_keywords(str(" ".join([l.lower() for l in lines])),19 unmodified linesreturn papersdef extract_structure(self, papers, pdf_dir, txt_dir, img_dir, dump_dir, tab_dir, tables=False):self.print_fn("\n-extracting sections.. ")papers, ids_none = self.extract_parts(papers, txt_dir, dump_dir)self.print_fn("\n-extracting images.. for future correlation use-cases ")papers = self.extract_images(papers, pdf_dir, img_dir)if tables:self.print_fn("\n-extracting tables.. for future correlation use-cases ")papers = self.extract_tables(papers, pdf_dir, tab_dir)return papers, ids_none249 unmodified linesfor p in papers:if p['id'] == pid:return pself.print_fn("\n-paper not found by file, \nfile: "+file+"\nall papers: "+', '.join([p['id'] for p in papers]))def alpha_length(self, s):127 unmodified lineselse:discarded_ids.append(urlparse(result.entry_id).path.split('/')[-1].split('v')[0])self.print_fn("\n-Papers discarded due to id error [arxiv api bug: #74] :\n" + str(discarded_ids))return results, searched_papers1 unmodified lineimport arxivfrom urllib.parse import urlparseids = [p['id'] for p in papers]self.print_fn("\n-downloading below selected papers: ")self.print_fn(ids)# asert(False)papers_filtered = arxiv.Search(id_list=ids).get()36 unmodified linescites = internal_citations.citation_list_parallel(N=multiprocessing.cpu_count(), directory=txt_dir)self.print_fn("\n-citation-network: ")self.print_fn(cites)for p in papers:105 unmodified linesif not num_papers:num_papers = self.DEFAULTS['num_papers']# arxiv api relevance search and data preparationself.print_fn("\n-searching arXiv for top 100 papers.. ")results, searched_papers = self.search(query, max_search=max_search)joblib.dump(searched_papers, self.dump_dir + 'papers_metadata.dmp')self.print_fn("\n-found " + str(len(searched_papers)) + " papers")# paper selection by scibert vector embedding relevance scores# papers_selected = select_papers(searched_papers, query, num_papers=num_papers)6 unmodified linesjoblib.dump(papers_highlighted, self.dump_dir + 'papers_highlighted.dmp')self.print_fn("\n-Standardizing known section headings per paper.. ")papers_standardized = self.standardize_headings(papers_highlighted)joblib.dump(papers_standardized, self.dump_dir + 'papers_standardized.dmp')self.print_fn("\n-Building paper-wise corpus.. ")corpus = self.build_corpus(papers_highlighted, searched_papers)joblib.dump(corpus, self.dump_dir + 'corpus.dmp')self.print_fn("\n-Building section-wise corpus.. ")corpus_sectionwise = self.build_corpus_sectionwise(papers_standardized)joblib.dump(corpus_sectionwise, self.dump_dir + 'corpus_sectionwise.dmp')self.print_fn("\n-Building basic research highlights.. ")research_blocks = self.build_basic_blocks(corpus_sectionwise, corpus)joblib.dump(research_blocks, self.dump_dir + 'research_blocks.dmp')self.print_fn("\n-Reducing corpus to lines.. ")corpus_lines = self.get_corpus_lines(corpus)joblib.dump(corpus_lines, self.dump_dir + 'corpus_lines.dmp')27 unmodified lines'''# self.print_fn("corpus types:"+ str(np.unique([type(txt) for k,txt in corpus.items()])))self.print_fn("\n-Building abstract.. ")abstract_block = self.get_abstract(corpus_lines, corpus_sectionwise, research_blocks)joblib.dump(abstract_block, self.dump_dir + 'abstract_block.dmp')'''2 unmodified linesself.print_fn(abstract_block)'''self.print_fn("\n-Building introduction.. ")intro_block = self.get_intro(corpus_sectionwise, research_blocks)joblib.dump(intro_block, self.dump_dir + 'intro_block.dmp')'''1 unmodified lineself.print_fn("intro_block:")self.print_fn(intro_block)'''self.print_fn("\n-Building custom sections.. ")clustered_sections, clustered_sentences = self.get_clusters(papers_standardized, searched_papers)joblib.dump(clustered_sections, self.dump_dir + 'clustered_sections.dmp')joblib.dump(clustered_sentences, self.dump_dir + 'clustered_sentences.dmp')11 unmodified linesclustered_sections['introduction'] = intro_blockjoblib.dump(clustered_sections, self.dump_dir + 'research_sections.dmp')self.print_fn("\n-Building conclusion.. ")conclusion_block = self.get_conclusion(clustered_sections)joblib.dump(conclusion_block, self.dump_dir + 'conclusion_block.dmp')clustered_sections['conclusion'] = conclusion_block10 unmodified linesshutil.copy(self.dump_dir + survey_file, survey_file)assert (os.path.exists(survey_file))output_zip = self.zip_outputs(self.dump_dir, query)self.print_fn("\n-Survey complete.. \nSurvey file path :" + os.path.abspath(survey_file) + "\nAll outputs zip path :" + os.path.abspath(self.dump_dir + output_zip))return os.path.abspath(self.dump_dir + output_zip), os.path.abspath(survey_file)
app.py+1-1
src/Surveyor.py+30-31