Spaces:
Building
Building
File size: 4,220 Bytes
eab1374 |
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
"""
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.") |