query-app / chunk.py
Makima57's picture
Update chunk.py
a08cb71 verified
import streamlit as st
# Example function to split text into chunks of a specified size
def chunk_text(text, chunk_size=2000):
# Ensure text is non-empty
if not text:
return []
# Chunk the text into smaller parts
chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
return chunks
# Function to save and download chunked text
def save_and_download_chunked_data(chunked_text, file_name="chunked_data.txt"):
# Ensure chunked_text is non-empty
if not chunked_text:
st.warning("No chunked data to download.")
return
# Combine chunks with delimiters (optional)
chunked_data = "\n---\n".join(chunked_text) # Add a separator between chunks
# Display download button in Streamlit
st.download_button(
label="Download Chunked Data",
data=chunked_data,
file_name=file_name,
mime="text/plain"
)
return chunked_data # Return the combined chunked data if needed
# Function to display chunks in Streamlit
def display_chunks(text, chunk_size=1000):
for j, chunk in enumerate(chunk_text(text, chunk_size)):
st.write(f"**Chunk {j+1}:**")
st.write(chunk)