Spaces:
Building
Building
config manager uses memory-based filehandling
Browse files- web_app/config_manager.py +5 -13
- web_app/debug_utils.py +15 -22
web_app/config_manager.py
CHANGED
|
@@ -5,14 +5,12 @@ Handles loading, validation, and management of frequency list configurations.
|
|
| 5 |
|
| 6 |
import streamlit as st
|
| 7 |
import pandas as pd
|
| 8 |
-
import tempfile
|
| 9 |
-
import os
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import Dict, List, Any, Optional, Tuple
|
| 12 |
import yaml
|
| 13 |
|
| 14 |
from web_app.session_manager import SessionManager
|
| 15 |
-
from web_app.utils import
|
| 16 |
|
| 17 |
|
| 18 |
class ConfigManager:
|
|
@@ -52,24 +50,18 @@ class ConfigManager:
|
|
| 52 |
try:
|
| 53 |
from io import StringIO
|
| 54 |
|
| 55 |
-
# Use
|
| 56 |
-
|
| 57 |
-
if not
|
| 58 |
-
st.error(f"Failed to
|
| 59 |
return None
|
| 60 |
|
| 61 |
-
# Read content from temp file
|
| 62 |
-
content = FileUploadHandler.read_from_temp(temp_path)
|
| 63 |
-
|
| 64 |
# Decode content if it's bytes
|
| 65 |
if isinstance(content, bytes):
|
| 66 |
text_content = content.decode('utf-8')
|
| 67 |
else:
|
| 68 |
text_content = content
|
| 69 |
|
| 70 |
-
# Cleanup temp file after reading
|
| 71 |
-
FileUploadHandler.cleanup_temp_file(temp_path)
|
| 72 |
-
|
| 73 |
# Determine delimiter from first 1024 chars
|
| 74 |
sample = text_content[:1024]
|
| 75 |
delimiter = ',' if sample.count(',') > sample.count('\t') else '\t'
|
|
|
|
| 5 |
|
| 6 |
import streamlit as st
|
| 7 |
import pandas as pd
|
|
|
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
from typing import Dict, List, Any, Optional, Tuple
|
| 10 |
import yaml
|
| 11 |
|
| 12 |
from web_app.session_manager import SessionManager
|
| 13 |
+
from web_app.utils import MemoryFileHandler
|
| 14 |
|
| 15 |
|
| 16 |
class ConfigManager:
|
|
|
|
| 50 |
try:
|
| 51 |
from io import StringIO
|
| 52 |
|
| 53 |
+
# Use memory-based approach for HF Spaces compatibility
|
| 54 |
+
content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=False)
|
| 55 |
+
if not content:
|
| 56 |
+
st.error(f"Failed to read file {uploaded_file.name}")
|
| 57 |
return None
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
# Decode content if it's bytes
|
| 60 |
if isinstance(content, bytes):
|
| 61 |
text_content = content.decode('utf-8')
|
| 62 |
else:
|
| 63 |
text_content = content
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
# Determine delimiter from first 1024 chars
|
| 66 |
sample = text_content[:1024]
|
| 67 |
delimiter = ',' if sample.count(',') > sample.count('\t') else '\t'
|
web_app/debug_utils.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
-
from web_app.utils import
|
| 7 |
|
| 8 |
def show_environment_info():
|
| 9 |
"""Display environment information for debugging."""
|
|
@@ -116,33 +116,26 @@ def debug_file_upload():
|
|
| 116 |
except Exception as e:
|
| 117 |
st.write(f"- GetValue method: β Failed - {str(e)}")
|
| 118 |
|
| 119 |
-
# Test
|
| 120 |
-
st.write("\n**
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# Try decoding
|
| 132 |
-
if isinstance(temp_content, bytes):
|
| 133 |
-
text = temp_content.decode('utf-8')
|
| 134 |
-
st.write(f"- Decode UTF-8: β
Success ({len(text)} chars)")
|
| 135 |
else:
|
| 136 |
-
st.write("-
|
| 137 |
-
|
| 138 |
-
# Cleanup
|
| 139 |
-
FileUploadHandler.cleanup_temp_file(temp_path)
|
| 140 |
-
st.write("- Cleanup: β
Success")
|
| 141 |
else:
|
| 142 |
-
st.write("-
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
-
st.error(f"Error with
|
| 146 |
import traceback
|
| 147 |
st.code(traceback.format_exc())
|
| 148 |
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
+
from web_app.utils import MemoryFileHandler
|
| 7 |
|
| 8 |
def show_environment_info():
|
| 9 |
"""Display environment information for debugging."""
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
st.write(f"- GetValue method: β Failed - {str(e)}")
|
| 118 |
|
| 119 |
+
# Test memory-based approach
|
| 120 |
+
st.write("\n**Memory-based Approach:**")
|
| 121 |
try:
|
| 122 |
+
uploaded_file.seek(0)
|
| 123 |
+
content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=False)
|
| 124 |
+
if content:
|
| 125 |
+
st.write(f"- Process file (binary): β
Success ({len(content)} bytes)")
|
| 126 |
|
| 127 |
+
# Try text mode
|
| 128 |
+
uploaded_file.seek(0)
|
| 129 |
+
text_content = MemoryFileHandler.process_uploaded_file(uploaded_file, as_text=True)
|
| 130 |
+
if text_content:
|
| 131 |
+
st.write(f"- Process file (text): β
Success ({len(text_content)} chars)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
else:
|
| 133 |
+
st.write("- Process file (text): β Failed")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
+
st.write("- Process file (binary): β Failed")
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
+
st.error(f"Error with memory-based approach: {e}")
|
| 139 |
import traceback
|
| 140 |
st.code(traceback.format_exc())
|
| 141 |
|