App
app ¶
Assembly: Chonky
Filename: app.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
app.py
Copyright © 2022 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
style_subheaders ¶
Style Streamlit subheaders.
Purpose
Injects the CSS used to keep Chonky's secondary headings visually aligned with the application accent color. The helper affects Streamlit markdown containers and chat message headings without changing application state or downstream workflow data.
Source code in app.py
encode_image_base64 ¶
Encode an image file as base64 text.
Purpose
Reads an image from disk and converts its bytes into a base64 string for Streamlit HTML or markdown rendering where inline image content is required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Filesystem path to the image file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Base64-encoded image content. |
Source code in app.py
clear_if_active ¶
Clear loader-specific session state when a loader is active.
Purpose
Resets document, token, chunking, embedding, dataframe, and table state only when the requested loader is the active loader. The helper protects unrelated workflow state while allowing individual loader panels to clear their own downstream outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader_name
|
str
|
Name of the loader whose state should be cleared when active. |
required |
Source code in app.py
metric_with_tooltip ¶
Render a Streamlit metric with optional hover guidance.
Purpose
Displays a metric value beside an informational tooltip icon for Chonky analysis panels. The helper keeps metric layout consistent while suppressing tooltip icons for core text length metrics that do not require additional explanation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
Metric label displayed by Streamlit. |
required |
value
|
str
|
Metric value displayed by Streamlit. |
required |
tooltip
|
str
|
Tooltip text rendered in the hover title attribute. |
required |
Source code in app.py
normalize_embeddings ¶
Normalize an embedding array to two dimensions.
Purpose
Converts a single one-dimensional embedding vector into a row-shaped array so Chonky embedding diagnostics, dimensionality reduction, and vector persistence paths can operate on a consistent matrix-like structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
emb_array
|
ndarray
|
NumPy embedding array to normalize. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Original array or reshaped two-dimensional embedding array. |
Source code in app.py
rebuild_raw_text_from_documents ¶
Rebuild raw text from loaded documents.
Purpose
Reconstructs the shared raw-text buffer from the current Streamlit documents state after
loader-specific clear operations. The helper preserves downstream processing compatibility
by joining non-empty page_content values from remaining LangChain documents.
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: Rebuilt raw text, or |
Source code in app.py
chunk_characters ¶
Chunk text by character windows.
Purpose
Splits source text into overlapping character-based chunks for semantic analysis, embedding preparation, and display workflows. The function clamps chunk size and overlap to safe values before producing non-empty chunk strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Source text to chunk. |
required |
size
|
int
|
Maximum number of characters in each chunk. |
required |
overlap
|
int
|
Number of overlapping characters between adjacent chunks. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Character-based chunk strings. |
Source code in app.py
chunk_tokens ¶
Chunk tokens by overlapped token windows.
Purpose
Splits a token list into overlapping windows and rejoins each window into a string for Chonky tokenization, semantic-analysis, and embedding-readiness workflows. The function validates list input and clamps chunk parameters to safe values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[str]
|
Token values to chunk. |
required |
size
|
int
|
Maximum number of tokens in each chunk. |
required |
overlap
|
int
|
Number of overlapping tokens between adjacent chunks. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Token-window chunk strings. |
Source code in app.py
rebuild_token_text ¶
Rebuild display text from token values.
Purpose
Reconstructs readable text from tokenized output while avoiding spaces before common sentence delimiters and normalizing punctuation spacing. The helper supports Chonky's tokenization and processed-text display panels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
List[str]
|
Token values produced by tokenization, stemming, or lemmatization. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Reconstructed display text with normalized punctuation spacing. |
Source code in app.py
refresh_processing_display ¶
Purpose:¶
Increment the processing display version so Streamlit recreates the read-only text-area widgets after processing, reset, or clear actions.
Parameters:¶
None
Returns:¶
None
Source code in app.py
coerce_text ¶
Purpose:¶
Convert parser output into a display-safe string without allowing None values to break later processing steps.
Parameters:¶
value : object The value returned by a processing method.
Returns:¶
str The string representation of the value, or an empty string.
Source code in app.py
pad_or_trim_row ¶
Purpose:¶
Normalizes a token row to a fixed width by trimming or right-padding with blanks.
Parameters:¶
row : list The token row to normalize.
int
The required fixed width.
Returns:¶
list A list of length == size.
Source code in app.py
project_chunks_for_embedding ¶
Purpose:¶
Project chunked inputs into a clean list of embedding-ready strings.
Guarantees:¶
- Output is strictly List[str]
- No None, type, NaN, or non-string objects
- No empty or whitespace-only strings
Parameters:¶
chunks : list Input chunks (token grids, strings, or mixed structures)
Returns:¶
list[str] Clean embedding documents