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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# ---------------------------------------------  Libraries  ----------------------------------------------------------#
import gradio as gr
from PyPDF2 import PdfReader
import nbformat

from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter, MarkdownTextSplitter, PythonCodeTextSplitter, Language
from langchain.docstore.document import Document
from langchain_community.document_loaders import Docx2txtLoader, CSVLoader

# ---------------------------------------------  Functions  ----------------------------------------------------------#

def process_uploaded_file(uploaded_file):
    text = ""
    display_content = ""
    file_extension = uploaded_file.name.split(".")[-1]

    if file_extension == "pdf":
        try:
            # Gradio's uploaded_file.name provides the path to the temporary file
            pdf = PdfReader(uploaded_file.name)
            for page in pdf.pages:
                page_text = page.extract_text()
                text += page_text + "\n"
                display_content += page_text + "\n"
        except Exception as e:
            display_content = f"Error reading PDF file: {e}"
            text = ""

    elif file_extension == "docx":
        try:
            docx_loader = Docx2txtLoader(uploaded_file.name)
            documents = docx_loader.load()
            text = "\n".join([doc.page_content for doc in documents])
            display_content = text
        except Exception as e:
            display_content = f"Error reading DOCX file: {e}"
            text = ""

    elif file_extension in ["html", "css", "py", "txt"]:
        try:
            with open(uploaded_file.name, "r", encoding="utf-8") as f:
                file_content = f.read()
            display_content = file_content  # Display as plain text in Textbox
            text = file_content
        except Exception as e:
            display_content = f"Error reading {file_extension.upper()} file: {e}"
            text = ""

    elif file_extension == "ipynb":
        try:
            # nbformat.read can take a file path
            nb_content = nbformat.read(uploaded_file.name, as_version=4)
            nb_filtered = [cell for cell in nb_content["cells"] if cell["cell_type"] in ["code", "markdown"]]
            
            for cell in nb_filtered:
                if cell["cell_type"] == "code":
                    display_content += f"```python\n{cell['source']}\n```\n"
                    text += cell["source"] + "\n"
                elif cell["cell_type"] == "markdown":
                    display_content += f"{cell['source']}\n"
                    text += cell["source"] + "\n"
        except Exception as e:
            display_content = f"Error reading IPYNB file: {e}"
            text = ""

    elif file_extension == "csv":
        try:
            loader = CSVLoader(file_path=uploaded_file.name, encoding="utf-8", csv_args={'delimiter': ','})
            documents = loader.load()
            text = "\n".join([doc.page_content for doc in documents])
            display_content = text # For CSV, display the concatenated text
        except Exception as e:
            display_content = f"Error reading CSV file: {e}"
            text = ""
    else:
        display_content = "Unsupported file type."
        text = ""

    return text, display_content


def chunk_recursive(text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace):
    if not text:
        return [], ""
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        length_function=len,
        keep_separator=keep_separator,
        add_start_index=add_start_index,
        strip_whitespace=strip_whitespace,
    )
    chunks = text_splitter.create_documents([text])
    formatted_chunks = []
    for chunk in chunks:
        if isinstance(chunk, Document):
            formatted_chunks.append({"content": chunk.page_content, "metadata": chunk.metadata})
        else:
            formatted_chunks.append({"content": str(chunk), "metadata": {}})
    
    code_example = f"""
from langchain.text_splitter import RecursiveCharacterTextSplitter

text_content = \"\"\"{text[:50]}...\"\"\" # Truncated for example

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size={chunk_size},
    chunk_overlap={chunk_overlap},
    length_function=len,
    keep_separator={keep_separator},
    add_start_index={add_start_index},
    strip_whitespace={strip_whitespace},
)
chunks = text_splitter.create_documents([text_content])
# Access chunks: chunks[0].page_content, chunks[0].metadata
"""
    return formatted_chunks, code_example

def chunk_character(text, chunk_size, chunk_overlap, separator, keep_separator, add_start_index, strip_whitespace):
    if not text:
        return [], ""
    
    if isinstance(separator, list):
        separator_str = "".join(separator)
    else:
        separator_str = separator

    text_splitter = CharacterTextSplitter(
        separator=separator_str,
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        length_function=len,
        keep_separator=keep_separator,
        add_start_index=add_start_index,
        strip_whitespace=strip_whitespace,
    )
    chunks = text_splitter.create_documents([text])
    formatted_chunks = []
    for chunk in chunks:
        if isinstance(chunk, Document):
            formatted_chunks.append({"content": chunk.page_content, "metadata": chunk.metadata})
        else:
            formatted_chunks.append({"content": str(chunk), "metadata": {}})

    code_example = f"""
from langchain.text_splitter import CharacterTextSplitter

text_content = \"\"\"{text[:50]}...\"\"\" # Truncated for example

text_splitter = CharacterTextSplitter(
    separator=\"\"\"{separator_str}\"\"\",
    chunk_size={chunk_size},
    chunk_overlap={chunk_overlap},
    length_function=len,
    keep_separator={keep_separator},
    add_start_index={add_start_index},
    strip_whitespace={strip_whitespace},
)
chunks = text_splitter.create_documents([text_content])
# Access chunks: chunks[0].page_content, chunks[0].metadata
"""
    return formatted_chunks, code_example

def chunk_python_code(text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace):
    if not text:
        return [], ""
    text_splitter = PythonCodeTextSplitter(
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        keep_separator=keep_separator,
        add_start_index=add_start_index,
        strip_whitespace=strip_whitespace,
    )
    chunks = text_splitter.create_documents([text])
    formatted_chunks = []
    for chunk in chunks:
        if isinstance(chunk, Document):
            formatted_chunks.append({"content": chunk.page_content, "metadata": chunk.metadata})
        else:
            formatted_chunks.append({"content": str(chunk), "metadata": {}})

    code_example = f"""
from langchain.text_splitter import PythonCodeTextSplitter

text_content = \"\"\"{text[:50]}...\"\"\" # Truncated for example

text_splitter = PythonCodeTextSplitter(
    chunk_size={chunk_size},
    chunk_overlap={chunk_overlap},
    keep_separator={keep_separator},
    add_start_index={add_start_index},
    strip_whitespace={strip_whitespace},
)
chunks = text_splitter.create_documents([text_content])
# Access chunks: chunks[0].page_content, chunks[0].metadata
"""
    return formatted_chunks, code_example

def chunk_javascript_code(text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace):
    if not text:
        return [], ""
    text_splitter = RecursiveCharacterTextSplitter.from_language(
        language=Language.JS,
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        keep_separator=keep_separator,
        add_start_index=add_start_index,
        strip_whitespace=strip_whitespace,
    )
    chunks = text_splitter.create_documents([text])
    formatted_chunks = []
    for chunk in chunks:
        if isinstance(chunk, Document):
            formatted_chunks.append({"content": chunk.page_content, "metadata": chunk.metadata})
        else:
            formatted_chunks.append({"content": str(chunk), "metadata": {}})

    code_example = f"""
from langchain.text_splitter import RecursiveCharacterTextSplitter, Language

text_content = \"\"\"{text[:50]}...\"\"\" # Truncated for example

text_splitter = RecursiveCharacterTextSplitter.from_language(
    language=Language.JS,
    chunk_size={chunk_size},
    chunk_overlap={chunk_overlap},
    keep_separator={keep_separator},
    add_start_index={add_start_index},
    strip_whitespace={strip_whitespace},
)
chunks = text_splitter.create_documents([text_content])
# Access chunks: chunks[0].page_content, chunks[0].metadata
"""
    return formatted_chunks, code_example

def chunk_markdown(text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace):
    if not text:
        return [], ""
    text_splitter = MarkdownTextSplitter(
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        length_function=len,
        keep_separator=keep_separator,
        add_start_index=add_start_index,
        strip_whitespace=strip_whitespace,
    )
    chunks = text_splitter.create_documents([text])
    formatted_chunks = []
    for chunk in chunks:
        if isinstance(chunk, Document):
            formatted_chunks.append({"content": chunk.page_content, "metadata": chunk.metadata})
        else:
            formatted_chunks.append({"content": str(chunk), "metadata": {}})

    code_example = f"""
from langchain.text_splitter import MarkdownTextSplitter

text_content = \"\"\"{text[:50]}...\"\"\" # Truncated for example

text_splitter = MarkdownTextSplitter(
    chunk_size={chunk_size},
    chunk_overlap={chunk_overlap},
    length_function=len,
    keep_separator={keep_separator},
    add_start_index={add_start_index},
    strip_whitespace={strip_whitespace},
)
chunks = text_splitter.create_documents([text_content])
# Access chunks: chunks[0].page_content, chunks[0].metadata
"""
    return formatted_chunks, code_example

def main_interface(uploaded_file, chunk_size, chunk_overlap, separator, keep_separator, add_start_index, strip_whitespace):
    if uploaded_file is None:
        return "", "", [], [], [], [], [], "", "", "", "", "", "", "", "", "", "", ""

    # Ensure chunk_size and chunk_overlap are integers
    chunk_size = int(chunk_size)
    chunk_overlap = int(chunk_overlap)

    raw_text, display_content = process_uploaded_file(uploaded_file)

    recursive_chunks, recursive_code = chunk_recursive(raw_text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace)
    character_chunks, character_code = chunk_character(raw_text, chunk_size, chunk_overlap, separator, keep_separator, add_start_index, strip_whitespace)
    markdown_chunks, markdown_code = chunk_markdown(raw_text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace)
    python_chunks, python_code = chunk_python_code(raw_text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace)
    javascript_chunks, javascript_code = chunk_javascript_code(raw_text, chunk_size, chunk_overlap, keep_separator, add_start_index, strip_whitespace)

    return (
        display_content,
        raw_text,
        recursive_chunks,
        character_chunks,
        markdown_chunks,
        python_chunks,
        javascript_chunks,
        f"Number of chunks: {len(recursive_chunks)}",
        f"Number of chunks: {len(character_chunks)}",
        f"Number of chunks: {len(markdown_chunks)}",
        f"Number of chunks: {len(python_chunks)}",
        f"Number of chunks: {len(javascript_chunks)}",
        recursive_code,
        character_code,
        markdown_code,
        python_code,
        javascript_code
    )

# ---------------------------------------------  Gradio Interface  ----------------------------------------------------------#

with gr.Blocks(theme=gr.themes.Soft(), title="πŸ¦œοΈπŸ”— LangChain Text Chunker") as demo:
    gr.Markdown(
        """
        # πŸ¦œοΈπŸ”— LangChain Text Chunker
        Welcome to the LangChain Text Chunker application! This tool allows you to upload various document types,
        extract their text content, and then apply different LangChain text splitting (chunking) methods.
        You can observe how each method breaks down the text into smaller, manageable chunks, along with their metadata.

        ### How to Use:
        1.  **Upload your document**: Select a file (PDF, DOCX, TXT, HTML, CSS, PY, IPYNB, CSV) using the file input.
        2.  **Adjust Chunking Parameters**: Use the sliders and dropdowns to customize `Chunk Size`, `Chunk Overlap`,
            `Character Splitter Separator`, `Keep Separator` behavior, `Add Start Index` to metadata, and `Strip Whitespace`.
        3.  **Process Document**: Click the "Process Document" button to see the extracted raw text and the results
            of various chunking methods in their respective tabs.
        4.  **Explore Chunks**: Each tab will display the chunks as JSON, along with the total number of chunks created.
        5.  **Python Example Code**: You can view dynamically generated Python 🐍 example code. 
        6.  **Inference**: This Gradio app is inferred from [Mervin Praison's work](https://mer.vin/2024/03/chunking-strategy/) about "Advanced Chunking Strategies".
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            file_input = gr.File(label="Upload your document", file_types=[".pdf", ".docx", ".txt", ".html", ".css", ".py", ".ipynb", ".csv"])
            process_button = gr.Button("Process Document", variant="primary")
            
            with gr.Accordion("Chunking Parameters", open=False):
                chunk_size_input = gr.Slider(minimum=100, maximum=2000, value=250, step=50, label="Chunk Size", info="Maximum size of chunks to return.")
                chunk_overlap_input = gr.Slider(minimum=0, maximum=500, value=0, step=10, label="Chunk Overlap", info="Overlap in characters between chunks.")
                separator_input = gr.Dropdown(
                    label="Character Splitter Separator",
                    choices=["\\n\\n", "\\n", " ", "", "\n", "." ,",", ";", ":", "!", "?", "-", 
                        "β€”", "(", ")", "[", "]", "{", "}", '"', "'", 
                        "β€œ", "”", "β€˜", "’", "..."], # Representing common separators
                    value="\\n\\n",
                    allow_custom_value=True,
                    multiselect=True,
                    info="Characters to split on for Character Chunking. Multiple selections will be joined."
                )
                keep_separator_input = gr.Dropdown(
                    label="Keep Separator",
                    choices=[True, False, "start", "end"],
                    value=False,
                    info="Whether to keep the separator and where to place it in each corresponding chunk (True='start')."
                )
                add_start_index_input = gr.Checkbox(label="Add Start Index to Metadata", value=True, info="If checked, includes chunk’s start index in metadata.")
                strip_whitespace_input = gr.Checkbox(label="Strip Whitespace", value=True, info="If checked, strips whitespace from the start and end of every document.")
        
        with gr.Column(scale=2):
            raw_text_display = gr.Textbox(label="Extracted Raw Text", lines=10, interactive=False, show_copy_button=True)
            hidden_raw_text = gr.State("") # To store the actual raw text for chunking

    with gr.Tabs():
        with gr.TabItem("Recursive Chunking"):
            recursive_count_output = gr.Markdown()
            recursive_output = gr.JSON(label="Recursive Chunks")
            recursive_code_output = gr.Code(label="Python Code Example", language="python", interactive=False)
        with gr.TabItem("Character Chunking"):
            character_count_output = gr.Markdown()
            character_output = gr.JSON(label="Character Chunks")
            character_code_output = gr.Code(label="Python Code Example", language="python", interactive=False)
        with gr.TabItem("Markdown Chunking"):
            markdown_count_output = gr.Markdown()
            markdown_output = gr.JSON(label="Markdown Chunks")
            markdown_code_output = gr.Code(label="Python Code Example", language="python", interactive=False)
        with gr.TabItem("Python Code Chunking"):
            python_count_output = gr.Markdown()
            python_output = gr.JSON(label="Python Code Chunks")
            python_code_output = gr.Code(label="Python Code Example", language="python", interactive=False)
        with gr.TabItem("JavaScript Code Chunking"):
            javascript_count_output = gr.Markdown()
            javascript_output = gr.JSON(label="JavaScript Code Chunks")
            javascript_code_output = gr.Code(label="Python Code Example", language="python", interactive=False)

    process_button.click(
        fn=main_interface,
        inputs=[
            file_input,
            chunk_size_input,
            chunk_overlap_input,
            separator_input,
            keep_separator_input,
            add_start_index_input,
            strip_whitespace_input
        ],
        outputs=[
            raw_text_display,
            hidden_raw_text,
            recursive_output,
            character_output,
            markdown_output,
            python_output,
            javascript_output,
            recursive_count_output,
            character_count_output,
            markdown_count_output,
            python_count_output,
            javascript_count_output,
            recursive_code_output,
            character_code_output,
            markdown_code_output,
            python_code_output,
            javascript_code_output
        ]
    )

demo.queue().launch(share=False, inbrowser=True)