import gradio as gr from haystack import Document from haystack.nodes import PreProcessor from langchain.text_splitter import ( CharacterTextSplitter, RecursiveCharacterTextSplitter, ) preprocessor = PreProcessor( clean_empty_lines=True, clean_whitespace=True, clean_header_footer=True, remove_substrings=None, max_chars_check=10_000, ) def chunk(text, words, splitter_selection): if "Word" in splitter_selection: splits = preprocessor.split( Document(text), split_length=words, split_by="word", split_overlap=0, split_respect_sentence_boundary=( "respect sentence boundaries" in splitter_selection ), ) text_splits = [split.content for split in splits] elif splitter_selection == "LangChain's CharacterTextSplitter": text_splitter = CharacterTextSplitter( separator="", chunk_size=words, chunk_overlap=0, length_function=len, is_separator_regex=False, ) splits = text_splitter.create_documents([text]) text_splits = [split.page_content for split in splits] elif splitter_selection == "Langchain's RecursiveCharacterTextSplitter - vanilla": text_splitter = RecursiveCharacterTextSplitter( chunk_size=words, chunk_overlap=0, length_function=len, add_start_index=True, ) splits = text_splitter.create_documents([text]) text_splits = [split.page_content for split in splits] elif splitter_selection == "Langchain's RecursiveCharacterTextSplitter - with '.'": text_splitter = RecursiveCharacterTextSplitter( chunk_size=words, chunk_overlap=0, length_function=len, add_start_index=True, separators=["\n\n", "\n", ".", " ", ""], ) splits = text_splitter.create_documents([text]) text_splits = [split.page_content for split in splits] output = [(split, str(i)) for i, split in enumerate(text_splits)] return output ESSAY = """Chapter 6 WHAT SORT OF DESPOTISM DEMOCRATIC NATIONS HAVE TO FEAR I had remarked during my stay in the United States that a democratic state of society, similar to that of the Americans, might offer singular facilities for the establishment of despotism; and I perceived, upon my return to Europe, how much use had already been made, by most of our rulers, of the notions, the sentiments, and the wants created by this same social condition, for the purpose of extending the circle of their power. This led me to think that the nations of Christendom would perhaps eventually undergo some oppression like that which hung over several of the nations of the ancient world. A more accurate examination of the subject, and five years of further meditation, have not diminished my fears, but have changed their object. No sovereign ever lived in former ages so absolute or so powerful as to undertake to administer by his own agency, and without the assistance of intermediate powers, all the parts of a great empire; none ever attempted to subject all his subjects indiscriminately to strict uniformity of regulation and personally to tutor and direct every member of the community. The notion of such an undertaking never occurred to the human mind; and if any man had conceived it, the want of information, the imperfection of the administrative system, and, above all, the natural obstacles caused by the inequality of conditions would speedily have checked the execution of so vast a design. When the Roman emperors were at the height of their power, the different nations of the empire still preserved usages and customs of great diversity; although they were subject to the same monarch, most of the provinces were separately administered; they abounded in powerful and active municipalities; and although the whole government of the empire was centered in the hands of the Emperor alone and he always remained, in case of need, the supreme arbiter in all matters, yet the details of social life and private occupations lay for the most part beyond his control. The emperors possessed, it is true, an immense and unchecked power, which allowed them to gratify all their whimsical tastes and to employ for that purpose the whole strength of the state. They frequently abused that power arbitrarily to deprive their subjects of property or of life; their tyranny was extremely onerous to the few, but it did not reach the many; it was confined to some few main objects and neglected the rest; it was violent, but its range was limited. --- Then you can [Create a dataset repository](../huggingface_hub/quick-start#create-a-repository), for example using: ```python from huggingface_hub import HfApi HfApi().create_repo(repo_id="username/my_dataset", repo_type="dataset") ``` Finally, you can use [Hugging Face paths]([Hugging Face paths](https://huggingface.co/docs/huggingface_hub/guides/hf_file_system#integrations)) in Pandas: ```python import pandas as pd df.to_parquet("hf://datasets/username/my_dataset/data.parquet") # or write in separate files if the dataset has train/validation/test splits df_train.to_parquet("hf://datasets/username/my_dataset/train.parquet") df_valid.to_parquet("hf://datasets/username/my_dataset/validation.parquet") df_test .to_parquet("hf://datasets/username/my_dataset/test.parquet") ``` """ with gr.Blocks(theme=gr.themes.Soft()) as demo: text = gr.Textbox(label="Your text 🪶", value=ESSAY) split_selection = gr.Dropdown( choices=[ "LangChain's CharacterTextSplitter", "Langchain's RecursiveCharacterTextSplitter - vanilla", "Langchain's RecursiveCharacterTextSplitter - with '.'", "Haystack's PreProcessor - Word level, no sentence boundaries", "Haystack's PreProcessor - Word level, respect sentence boundaries", ], value="LangChain's CharacterTextSplitter", label="Chunking method ", info="How should we split our chunks", ) slider_count = gr.Slider( 20, 500, value=50, label="Count 🧮", info="Chunk size, in the chosen unit." ) out = gr.HighlightedText( label="Output", show_legend=True, show_label=False, ) text.change( fn=chunk, inputs=[text, slider_count, split_selection], outputs=out, ) split_selection.change( fn=chunk, inputs=[text, slider_count, split_selection], outputs=out, ) slider_count.change( fn=chunk, inputs=[text, slider_count, split_selection], outputs=out, ) demo.launch()