simple-text-analyzer / test /test_tmp_upload.py
egumasa's picture
emuTAALES
e7279e4
"""
Test script to verify /tmp file upload approach for Hugging Face Spaces
"""
import streamlit as st
import os
import sys
from pathlib import Path
# Add parent directory to path
sys.path.append(os.path.dirname(__file__))
from web_app.utils import FileUploadHandler
st.title("Test /tmp File Upload Approach")
st.write("This script tests the /tmp directory approach for handling file uploads on Hugging Face Spaces.")
# Show /tmp directory info
st.write("### /tmp Directory Information")
if os.path.exists("/tmp"):
st.write(f"βœ… /tmp exists")
st.write(f"- Writable: {os.access('/tmp', os.W_OK)}")
st.write(f"- Readable: {os.access('/tmp', os.R_OK)}")
# Check available space
try:
stat = os.statvfs("/tmp")
free_mb = (stat.f_frsize * stat.f_bavail) / (1024 * 1024)
st.write(f"- Free space: {free_mb:.1f} MB")
except:
st.write("- Free space: Unable to determine")
else:
st.error("❌ /tmp directory does not exist!")
# File upload test
st.write("### File Upload Test")
uploaded_file = st.file_uploader(
"Upload a test file",
type=['txt', 'csv', 'tsv'],
help="Upload any text file to test the /tmp approach"
)
if uploaded_file:
st.write("**File Details:**")
st.write(f"- Name: {uploaded_file.name}")
st.write(f"- Type: {uploaded_file.type}")
st.write(f"- Size: {uploaded_file.size} bytes")
# Test direct methods
col1, col2 = st.columns(2)
with col1:
st.write("**Direct Methods:**")
# Test read()
try:
uploaded_file.seek(0)
content = uploaded_file.read()
st.write(f"βœ… read(): {len(content)} bytes")
except Exception as e:
st.write(f"❌ read(): {str(e)}")
# Test getvalue()
try:
uploaded_file.seek(0)
value = uploaded_file.getvalue()
st.write(f"βœ… getvalue(): {len(value)} bytes")
except Exception as e:
st.write(f"❌ getvalue(): {str(e)}")
# Test getbuffer()
try:
buffer = uploaded_file.getbuffer()
st.write(f"βœ… getbuffer(): {len(buffer)} bytes")
except Exception as e:
st.write(f"❌ getbuffer(): {str(e)}")
with col2:
st.write("**/tmp Approach:**")
try:
# Save to temp
temp_path = FileUploadHandler.save_to_temp(uploaded_file, prefix="test")
if temp_path:
st.write(f"βœ… Saved to: {temp_path}")
# Check file exists
if os.path.exists(temp_path):
file_size = os.path.getsize(temp_path)
st.write(f"βœ… File exists: {file_size} bytes")
# Read from temp
content = FileUploadHandler.read_from_temp(temp_path)
if content:
st.write(f"βœ… Read content: {len(content)} bytes")
# Show first 100 chars
if isinstance(content, bytes):
preview = content.decode('utf-8', errors='ignore')[:100]
else:
preview = content[:100]
st.text(f"Preview: {preview}...")
else:
st.write("❌ Failed to read content")
# Cleanup
FileUploadHandler.cleanup_temp_file(temp_path)
if not os.path.exists(temp_path):
st.write("βœ… Cleanup successful")
else:
st.write("❌ Cleanup failed")
else:
st.write("❌ File not found after saving")
else:
st.write("❌ Failed to save to /tmp")
except Exception as e:
st.error(f"Error: {str(e)}")
import traceback
st.code(traceback.format_exc())
st.write("---")
st.info("If the /tmp approach works successfully, file uploads should function properly on Hugging Face Spaces.")