Aymeric Roucher commited on
Commit
bb88228
1 Parent(s): e8ee5f1

Update app.py

Browse files

Working demo of the app.

Files changed (1) hide show
  1. app.py +57 -19
app.py CHANGED
@@ -1,33 +1,71 @@
1
  import gradio as gr
2
- from haystack.nodes import PreProcessor
3
  from haystack import Document
 
 
4
 
5
  preprocessor = PreProcessor(
6
  clean_empty_lines=True,
7
  clean_whitespace=True,
8
  clean_header_footer=True,
9
  remove_substrings=None,
10
- split_by="word",
11
- split_length=200,
12
- split_respect_sentence_boundary=True,
13
- split_overlap=0,
14
  max_chars_check=10_000
15
  )
16
 
17
- def chunk(text):
18
- splits = preprocessor.process(Document(text))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- return [
21
- (i%3, split.content) for i, split in enumerate(splits)
22
- ]
23
 
24
- iface = gr.Interface(
25
- fn=chunk,
26
- inputs="text",
27
- outputs=gr.HighlightedText(
28
- label="Highlights",
29
- combine_adjacent=False,
 
 
 
 
 
 
 
 
 
 
30
  show_legend=True,
31
- color_map={"0": "red", "1": "green", "2": "yellow"}),
32
- )
33
- iface.launch()
 
 
 
 
 
1
  import gradio as gr
 
2
  from haystack import Document
3
+ from haystack.nodes import PreProcessor
4
+ from langchain.text_splitter import CharacterTextSplitter
5
 
6
  preprocessor = PreProcessor(
7
  clean_empty_lines=True,
8
  clean_whitespace=True,
9
  clean_header_footer=True,
10
  remove_substrings=None,
 
 
 
 
11
  max_chars_check=10_000
12
  )
13
 
14
+ def chunk(text, words, splitter_selection):
15
+ if "Word" in splitter_selection:
16
+ splits = preprocessor.split(
17
+ Document(text),
18
+ split_length=words,
19
+ split_by="word",
20
+ split_overlap=0,
21
+ split_respect_sentence_boundary=("respect sentence boundaries" in splitter_selection),
22
+ )
23
+ text_splits = [split.content for split in splits]
24
+ if splitter_selection == "Character":
25
+ text_splitter=CharacterTextSplitter(
26
+ separator="",
27
+ chunk_size=words,
28
+ chunk_overlap=0,
29
+ length_function=len,
30
+ is_separator_regex=False,
31
+ )
32
+ splits = text_splitter.create_documents([text])
33
+ text_splits = [split.page_content for split in splits]
34
+
35
+
36
+
37
+ output = [(split, str(i)) for i, split in enumerate(text_splits)]
38
+ return(output)
39
+
40
+
41
+ ESSAY = """
42
+ Chapter 6
43
+ WHAT SORT OF DESPOTISM DEMOCRATIC NATIONS HAVE TO FEAR
44
+ 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. .
45
 
46
+ A more accurate examination of the subject, and five years of further meditation, have not diminished my fears, but have changed their object.
 
 
47
 
48
+ 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.
49
+
50
+ 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.
51
+
52
+ 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.
53
+
54
+ 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.
55
+
56
+ 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
57
+ """
58
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
59
+ text = gr.Textbox(label="Your text 🪶", value=ESSAY)
60
+ 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?')
61
+ slider = gr.Slider(20, 500, value=50, label="Count 🧮", info="Chunk size, in the chosen unit.")
62
+ out = gr.HighlightedText(
63
+ label="Output",
64
  show_legend=True,
65
+ show_label=False,
66
+ )
67
+ text.change(fn=chunk, inputs=[text, slider, split_selection], outputs=out)
68
+ split_selection.change(fn=chunk, inputs=[text, slider, split_selection], outputs=out)
69
+ slider.change(fn=chunk, inputs=[text, slider, split_selection], outputs=out)
70
+
71
+ demo.launch()