File size: 3,290 Bytes
4b36911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
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.")