Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files3 unmodified lines456789101112131415161718192021222324252627282930312 unmodified lines3435363738394041424344454641 unmodified lines888990919293941 unmodified line969798991001011021031041053 unmodified lines1091101111121131141151161171181191201211221231241251261271281291301311321331341351361371385 unmodified lines1441451461471481491501511529 unmodified lines1621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102114 unmodified lines2162172182192202212222232242252262272282292302312322337 unmodified lines2412422432442452463 unmodified linesimport gradio as grimport spacesprint(f"Is CUDA available: {torch.cuda.is_available()}")# Trueif torch.cuda.is_available():print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")STYLE = """.container {width: 100%;display: grid;align-items: center;margin: 0!important;}.prose ul ul {margin: 0!important;font-size: 13px!important;}.tree {padding: 0px;margin: 0!important;box-sizing: border-box;font-size: 16px;width: 100%;height: auto;text-align: center;}2 unmodified linesposition: relative;transition: .5s;margin: 0!important;}.tree li {display: inline-table;text-align: center;list-style-type: none;position: relative;padding: 10px;transition: .5s;}.tree li::before, .tree li::after {41 unmodified lines}.tree li a {border: 1px solid #ccc;padding: 10px;display: inline-grid;border-radius: 5px;text-decoration-line: none;1 unmodified linetransition: .5s;}.tree li a span {border: 1px solid #ccc;border-radius: 5px;color: #666;padding: 8px;font-size: 12px;text-transform: uppercase;letter-spacing: 1px;3 unmodified lines.tree li a:hover, .tree li a:hover i, .tree li a:hover span, .tree li a:hover+ul li a {background: #c8e4f8;color: #000;border: 1px solid #94a0b4;}.tree li a:hover+ul li::after, .tree li a:hover+ul li::before, .tree li a:hover+ul::before, .tree li a:hover+ul ul::before {border-color: #94a0b4;}"""tokenizer = AutoTokenizer.from_pretrained("gpt2")model = AutoModelForCausalLM.from_pretrained("gpt2")tokenizer.pad_token_id = tokenizer.eos_token_idprint("Loading finished.")def generate_html(token, node):"""Recursively generate HTML for the tree."""html_content = f" <li> <a href='#'> <span> <b>{token}</b> </span> "html_content += node["table"] if node["table"] is not None else ""html_content += "</a>"if len(node["children"].keys()) > 0:html_content += "<ul> "for token, subnode in node["children"].items():html_content += generate_html(token, subnode)html_content += "</ul>"html_content += "</li>"return html_content5 unmodified lines<tr><th><b>Token</b></th><th><b>Step score</b></th><th><b>Cumulative score</b></th></tr>"""for token_idx in np.argsort(scores)[-top_k:]:token = tokenizer.decode([token_idx])style = ""if chosen_tokens and token in chosen_tokens:9 unmodified linesreturn markdown_tabledef display_tree(start_sentence, scores, sequences, beam_indices):display = """<div class="container"><div class="tree"><ul>"""sequences = sequences.cpu().numpy()print(tokenizer.batch_decode(sequences))original_tree = {"table": None, "children": {}}for sequence_ix in range(len(sequences)):current_sequence_score = 0current_tree = original_treefor step, step_scores in enumerate(scores):current_token_choice_ix = sequences[sequence_ix, step]current_token_choice = tokenizer.decode([current_token_choice_ix])current_beam = beam_indices[sequence_ix, step]if current_token_choice not in current_tree["children"]:current_tree["children"][current_token_choice] = {"table": None,"children": {},}# Rewrite the probs table even if it was there before, since new chosen nodes have appeared in the children of current treemarkdown_table = generate_markdown_table(step_scores[current_beam, :], current_sequence_score,chosen_tokens=current_tree["children"].keys(),)current_tree["table"] = markdown_tablecurrent_tree = current_tree["children"][current_token_choice]# Keep up to date the current cumulative scorecurrent_sequence_score += step_scores[current_beam, current_token_choice_ix]display += generate_html(start_sentence, original_tree)display += """</ul></div></body>"""return display@spaces.GPUdef get_tables(input_text, number_steps, number_beams):inputs = tokenizer([input_text], return_tensors="pt")outputs = model.generate(4 unmodified linesreturn_dict_in_generate=True,output_scores=True,top_k=5,temperature=1.0,do_sample=True,)print(outputs.sequences_scores)tables = display_tree(input_text,outputs.scores,outputs.sequences[:, len(inputs) :],outputs.beam_indices[:, : -len(inputs)],)return tableswith gr.Blocks(7 unmodified linesbeams = gr.Slider(label="Number of beams", minimum=2, maximum=4, step=1, value=3)button = gr.Button()out = gr.Markdown(label="Output")button.click(get_tables, inputs=[text, steps, beams], outputs=out)demo.launch()3 unmodified lines456789101112131415161718192021222324252627282930313233343536373839404142434445462 unmodified lines495051525354555657585960616263646541 unmodified lines1071081091101111121131 unmodified line1151161171181191201211223 unmodified lines1261271281291301311321331341351361371381391401411421431441451461471481491501515 unmodified lines1571581591601611621631641659 unmodified lines1751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133144 unmodified lines3193203213223233243253263273283293303313323333343353367 unmodified lines3443453463473483493 unmodified linesimport gradio as grimport spacestokenizer = AutoTokenizer.from_pretrained("gpt2")model = AutoModelForCausalLM.from_pretrained("gpt2")tokenizer.pad_token_id = tokenizer.eos_token_idprint("Loading finished.")print(f"Is CUDA available: {torch.cuda.is_available()}")# Trueif torch.cuda.is_available():print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")STYLE = """.custom-container {width: 100%;display: grid;align-items: center;margin: 0!important;overflow: scroll;}.prose ul ul {margin: 0!important;font-size: 10px!important;}.prose td, th {padding-left: 2px;padding-right: 2px;padding-top: 0;padding-bottom: 0;}.tree {padding: 0px;margin: 0!important;box-sizing: border-box;font-size: 10px;width: 100%;min-width: 2000px;height: auto;text-align: center;}2 unmodified linesposition: relative;transition: .5s;margin: 0!important;display: flex;flex-direction: row;justify-content: center;gap:10px;}.tree li {display: inline-table;text-align: center;list-style-type: none;position: relative;padding-top: 10px;transition: .5s;}.tree li::before, .tree li::after {41 unmodified lines}.tree li a {border: 1px solid #ccc;padding: 5px;display: inline-grid;border-radius: 5px;text-decoration-line: none;1 unmodified linetransition: .5s;}.tree li a span {color: #666;padding: 5px;font-size: 12px;text-transform: uppercase;letter-spacing: 1px;3 unmodified lines.tree li a:hover, .tree li a:hover i, .tree li a:hover span, .tree li a:hover+ul li a {background: #c8e4f8;color: #000;}.tree li a:hover+ul li::after, .tree li a:hover+ul li::before, .tree li a:hover+ul::before, .tree li a:hover+ul ul::before {border-color: #94a0b4;}.chosen {background-color: red;}"""def generate_nodes(token, node):"""Recursively generate HTML for the tree nodes."""html_content = f" <li> <a href='#' class={('chosen' if node.table is None else '')}> <span> <b>{token}</b> </span> "html_content += node.table if node.table is not None else ""html_content += "</a>"if len(node.children.keys()) > 0:html_content += "<ul> "for token, subnode in node.children.items():html_content += generate_nodes(token, subnode)html_content += "</ul>"html_content += "</li>"return html_content5 unmodified lines<tr><th><b>Token</b></th><th><b>Step score</b></th><th><b>Total score</b></th></tr>"""for token_idx in np.array(np.argsort(scores)[-top_k:])[::-1]:token = tokenizer.decode([token_idx])style = ""if chosen_tokens and token in chosen_tokens:9 unmodified linesreturn markdown_tabledef generate_html(start_sentence, original_tree):html_output = """<div class="custom-container"><div class="tree"><ul>"""html_output += generate_nodes(start_sentence, original_tree)html_output += """</ul></div></body>"""return html_outputimport pandas as pdfrom typing import Dictfrom dataclasses import dataclass@dataclassclass BeamNode:cumulative_score: floattable: strcurrent_sentence: strchildren: Dict[str, "BeamNode"]def generate_beams(start_sentence, scores, sequences, beam_indices):print(tokenizer.batch_decode(sequences))sequences = sequences.cpu().numpy()original_tree = BeamNode(cumulative_score=0, table=None, current_sentence=start_sentence, children={})n_beams = len(scores[0])beam_trees = [original_tree] * n_beamsfor step, step_scores in enumerate(scores):(top_token_indexes,top_cumulative_scores,beam_indexes,current_completions,top_tokens,) = ([], [], [], [], [])for beam_ix in range(n_beams):current_beam = beam_trees[beam_ix]# Get top cumulative scores for the current beamcurrent_top_token_indexes = list(np.array(scores[step][beam_ix].argsort()[-n_beams:])[::-1])top_token_indexes += current_top_token_indexestop_cumulative_scores += list(np.array(scores[step][beam_ix][current_top_token_indexes])+ current_beam.cumulative_score)beam_indexes += [beam_ix] * n_beamscurrent_completions += [beam_trees[beam_ix].current_sentence] * n_beamstop_tokens += [tokenizer.decode([el]) for el in current_top_token_indexes]top_df = pd.DataFrame.from_dict({"token_index": top_token_indexes,"cumulative_score": top_cumulative_scores,"beam_index": beam_indexes,"current_completions": current_completions,"token": top_tokens,})maxes = top_df.groupby(["token_index", "current_completions"])["cumulative_score"].idxmax()top_df = top_df.loc[maxes]# Sort all top probabilities and keep top n_beamstop_df_selected = top_df.sort_values("cumulative_score", ascending=False).iloc[:n_beams]print(step)display(top_df_selected)# Write the scores table - one per beam source?# Edge case: if several beam indexes are actually on the same beam, the selected tokens by beam_index for the second one will be empty. So we reversefor beam_ix in reversed(list(range(n_beams))):current_beam = beam_trees[beam_ix]selected_tokens = top_df_selected.loc[top_df_selected["beam_index"] == beam_ix]print(step, beam_ix)display(selected_tokens)markdown_table = generate_markdown_table(step_scores[beam_ix, :],current_beam.cumulative_score,chosen_tokens=list(selected_tokens["token"].values),)beam_trees[beam_ix].table = markdown_table# Add new children for each beamcumulative_scores = [beam.cumulative_score for beam in beam_trees]for beam_ix in range(n_beams):current_token_choice_ix = top_df_selected.iloc[beam_ix]["token_index"]current_token_choice = tokenizer.decode([current_token_choice_ix])# Update the source treesource_beam_ix = int(top_df_selected.iloc[beam_ix]["beam_index"])previous_len = len(str(original_tree))beam_trees[source_beam_ix].children[current_token_choice] = BeamNode(table=None,children={},current_sentence=beam_trees[source_beam_ix].current_sentence+ current_token_choice,cumulative_score=cumulative_scores[source_beam_ix]+ scores[step][source_beam_ix][current_token_choice_ix].numpy(),)assert (len(str(original_tree)) > previous_len), "Original tree has not increased size"# Reassign all beams at oncebeam_trees = [beam_trees[int(top_df_selected.iloc[beam_ix]["beam_index"])]for beam_ix in range(n_beams)]# Advance all beams by one tokenfor beam_ix in range(n_beams):current_token_choice_ix = top_df_selected.iloc[beam_ix]["token_index"]current_token_choice = tokenizer.decode([current_token_choice_ix])beam_trees[beam_ix] = beam_trees[beam_ix].children[current_token_choice]return original_tree@spaces.GPUdef get_beam_search_html(input_text, number_steps, number_beams):inputs = tokenizer([input_text], return_tensors="pt")outputs = model.generate(4 unmodified linesreturn_dict_in_generate=True,output_scores=True,top_k=5,do_sample=False,)original_tree = generate_beams(input_text,outputs.scores[:],outputs.sequences[:, :],outputs.beam_indices[:, :],)html = generate_html(input_text, original_tree)print(html)return htmlwith gr.Blocks(7 unmodified linesbeams = gr.Slider(label="Number of beams", minimum=2, maximum=4, step=1, value=3)button = gr.Button()out = gr.Markdown(label="Output")button.click(get_beam_search_html, inputs=[text, steps, beams], outputs=out)demo.launch()
app.py+167-64