simple-text-analyzer / test /test_memory_upload.py
egumasa's picture
emuTAALES
e7279e4
"""
Test memory-based file upload approach
"""
import streamlit as st
import sys
import os
sys.path.append(os.path.dirname(__file__))
from web_app.utils.memory_file_handler import MemoryFileHandler
st.set_page_config(page_title="Memory Upload Test", layout="wide")
st.title("Memory-Based File Upload Test")
st.write("This approach keeps files in memory to avoid filesystem 403 errors")
# File upload
uploaded_file = st.file_uploader(
"Upload a test file",
type=['txt', 'csv', 'tsv'],
help="Files are processed entirely in memory"
)
if uploaded_file:
st.write("### File Information")
col1, col2 = st.columns(2)
with col1:
st.write("**File Details:**")
st.write(f"- Name: {uploaded_file.name}")
st.write(f"- Size: {uploaded_file.size:,} bytes")
st.write(f"- Type: {uploaded_file.type}")
with col2:
st.write("**Processing Status:**")
# Test text processing
with st.expander("Test 1: Text Processing"):
try:
content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=True)
if content:
st.success(f"βœ… Successfully read {len(content):,} characters")
st.text_area("Content Preview", content[:500] + "...", height=200)
else:
st.error("Failed to read file")
except Exception as e:
st.error(f"Error: {str(e)}")
# Test binary processing
with st.expander("Test 2: Binary Processing"):
try:
content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=False)
if content:
st.success(f"βœ… Successfully read {len(content):,} bytes")
st.write(f"First 100 bytes: {content[:100]}")
else:
st.error("Failed to read file")
except Exception as e:
st.error(f"Error: {str(e)}")
# Test DataFrame processing
if uploaded_file.name.endswith(('.csv', '.tsv', '.txt')):
with st.expander("Test 3: DataFrame Processing"):
try:
df = MemoryFileHandler.process_csv_tsv_file(uploaded_file)
if df is not None:
st.success(f"βœ… Successfully parsed {len(df):,} rows")
st.dataframe(df.head())
else:
st.error("Failed to parse as DataFrame")
except Exception as e:
st.error(f"Error: {str(e)}")
# Test session storage
with st.expander("Test 4: Session Storage"):
try:
# Store in session
MemoryFileHandler.store_in_session(f"test_file_{uploaded_file.name}", uploaded_file.read())
st.success("βœ… Stored in session")
# Retrieve from session
retrieved = MemoryFileHandler.retrieve_from_session(f"test_file_{uploaded_file.name}")
if retrieved:
st.success(f"βœ… Retrieved {len(retrieved):,} bytes from session")
else:
st.error("Failed to retrieve from session")
except Exception as e:
st.error(f"Error: {str(e)}")
st.info("πŸ’‘ This approach processes files entirely in memory without touching the filesystem, avoiding 403 errors.")