Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer
|
| 3 |
+
|
| 4 |
+
bert_tokenizer = AutoTokenizer.from_pretrained('openai-community/gpt2')
|
| 5 |
+
|
| 6 |
+
def display_next_step_tokens(sentence, step):
|
| 7 |
+
return (
|
| 8 |
+
gr.Textbox.update(visible=(split_selection==LABEL_RECURSIVE)),
|
| 9 |
+
gr.Radio.update(visible=(split_selection==LABEL_RECURSIVE)),
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
with gr.Blocks(theme=gr.themes.Soft(text_size='lg', font=["monospace"], primary_hue=gr.themes.colors.green)) as demo:
|
| 14 |
+
text = gr.Textbox(label="Your prompt to start decoding", value="Ok, I")
|
| 15 |
+
|
| 16 |
+
with gr.Row():
|
| 17 |
+
split_selection = gr.Dropdown(
|
| 18 |
+
choices=[
|
| 19 |
+
LABEL_TEXTSPLITTER,
|
| 20 |
+
LABEL_RECURSIVE,
|
| 21 |
+
],
|
| 22 |
+
value=LABEL_RECURSIVE,
|
| 23 |
+
label="Method to split chunks π",
|
| 24 |
+
)
|
| 25 |
+
separators_selection = gr.Textbox(
|
| 26 |
+
elem_id="textbox_id",
|
| 27 |
+
value=["\n\n", "\n", " ", ""],
|
| 28 |
+
info="Separators used in RecursiveCharacterTextSplitter",
|
| 29 |
+
show_label=False, # or set label to an empty string if you want to keep its space
|
| 30 |
+
visible=True,
|
| 31 |
+
)
|
| 32 |
+
separator_preset_selection = gr.Radio(
|
| 33 |
+
['Default', 'Python', 'Markdown'],
|
| 34 |
+
label="Choose a preset",
|
| 35 |
+
info="This will apply a specific set of separators to RecursiveCharacterTextSplitter.",
|
| 36 |
+
visible=True,
|
| 37 |
+
)
|
| 38 |
+
with gr.Row():
|
| 39 |
+
length_unit_selection = gr.Dropdown(
|
| 40 |
+
choices=[
|
| 41 |
+
"Character count",
|
| 42 |
+
"Token count (BERT tokens)",
|
| 43 |
+
],
|
| 44 |
+
value="Character count",
|
| 45 |
+
label="Length function",
|
| 46 |
+
info="How should we measure our chunk lengths?",
|
| 47 |
+
)
|
| 48 |
+
slider_count = gr.Slider(
|
| 49 |
+
50, 500, value=200, step=1, label="Chunk length π", info="In the chosen unit."
|
| 50 |
+
)
|
| 51 |
+
chunk_overlap = gr.Slider(
|
| 52 |
+
0, 50, value=10, step=1, label="Overlap between chunks", info="In the chosen unit."
|
| 53 |
+
)
|
| 54 |
+
out = gr.HighlightedText(
|
| 55 |
+
label="Output",
|
| 56 |
+
show_legend=True,
|
| 57 |
+
show_label=False,
|
| 58 |
+
color_map={'Overlap': '#DADADA'}
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
split_selection.change(
|
| 62 |
+
fn=change_split_selection,
|
| 63 |
+
inputs=split_selection,
|
| 64 |
+
outputs=[separators_selection, separator_preset_selection],
|
| 65 |
+
)
|
| 66 |
+
separator_preset_selection.change(
|
| 67 |
+
fn=change_preset_separators,
|
| 68 |
+
inputs=separator_preset_selection,
|
| 69 |
+
outputs=separators_selection,
|
| 70 |
+
)
|
| 71 |
+
gr.on(
|
| 72 |
+
[text.change, length_unit_selection.change, separators_selection.change, split_selection.change, slider_count.change, chunk_overlap.change],
|
| 73 |
+
chunk,
|
| 74 |
+
[text, slider_count, split_selection, separators_selection, length_unit_selection, chunk_overlap],
|
| 75 |
+
outputs=out
|
| 76 |
+
)
|
| 77 |
+
demo.load(chunk, inputs=[text, slider_count, split_selection, separators_selection, length_unit_selection, chunk_overlap], outputs=out)
|
| 78 |
+
|
| 79 |
+
demo.launch()
|