Spaces:
Running
Running
File size: 6,430 Bytes
e7cb6de db61c57 bb88228 e7cb6de db61c57 e8ee5f1 db61c57 bb88228 db61c57 bb88228 e7cb6de bb88228 37c61d6 bb88228 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import gradio as gr
from haystack import Document
from haystack.nodes import PreProcessor
from langchain.text_splitter import CharacterTextSplitter
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]
if splitter_selection == "Character":
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]
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.
It would seem that if despotism were to be established among the democratic nations of our days, it might assume a different character; it would be more extensive and more mild; it would degrade men without tormenting them. I do not question that, in an age of instruction and equality like our own, sovereigns might more easily succeed in collecting all political power into their own hands and might interfere more habitually and decidedly with the circle of private interests than any sovereign of antiquity could ever do. But this same principle of equality which facilitates despotism tempers its rigor. We have seen how the customs of society become more humane and gentle in proportion as men become more equal and alike. When no member of the community has much power or much wealth, tyranny is, as it were, without opportunities and a field of action. As all fortunes are scanty, the passions of men are naturally circumscribed, their imagination limited, their pleasures simple. This universal moderation moderates the sovereign himself and checks within certain limits the inordinate stretch of his desires.
Independently of these reasons, drawn from the nature of the state of society itself, I might add many others arising from causes beyond my subject; but I shall keep within the limits I have laid down.
Democratic governments may become violent and even cruel at certain periods of extreme effervescence or of great danger, but these crises will be rare and brief. When I consider the petty passions of our contemporaries, the mildness of their manners, the extent of their education, the purity of their religion, the gentleness of their morality, their regular and industrious habits, and the restraint which they almost all observe in their vices no less than in their virtues, I have no fear that they will meet with tyrants in their rulers, but rather with guardians.1
"""
with gr.Blocks(theme=gr.themes.Soft()) as demo:
text = gr.Textbox(label="Your text ๐ชถ", value=ESSAY)
split_selection = gr.Radio(["Word - respect sentence boundaries", "Word - no sentence boundaries", "Character"], value='Character', label='Chunking method ๐', info='How should we split our chunks?')
slider = 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, split_selection], outputs=out)
split_selection.change(fn=chunk, inputs=[text, slider, split_selection], outputs=out)
slider.change(fn=chunk, inputs=[text, slider, split_selection], outputs=out)
demo.launch() |