egumasa commited on
Commit
ce8e901
Β·
1 Parent(s): d59a2d7

config manager uses memory-based filehandling

Browse files
Files changed (2) hide show
  1. web_app/config_manager.py +5 -13
  2. 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 FileUploadHandler
16
 
17
 
18
  class ConfigManager:
@@ -52,24 +50,18 @@ class ConfigManager:
52
  try:
53
  from io import StringIO
54
 
55
- # Use temp file approach for HF Spaces compatibility
56
- temp_path = FileUploadHandler.save_to_temp(uploaded_file, prefix="config")
57
- if not temp_path:
58
- st.error(f"Failed to save file {uploaded_file.name}")
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 FileUploadHandler
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 /tmp approach
120
- st.write("\n**Temp File Approach:**")
121
  try:
122
- temp_path = FileUploadHandler.save_to_temp(uploaded_file, prefix="debug")
123
- if temp_path:
124
- st.write(f"- Save to /tmp: βœ… Success ({temp_path})")
 
125
 
126
- # Read from temp
127
- temp_content = FileUploadHandler.read_from_temp(temp_path)
128
- if temp_content:
129
- st.write(f"- Read from /tmp: βœ… Success ({len(temp_content)} bytes)")
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("- Read from /tmp: ❌ Failed")
137
-
138
- # Cleanup
139
- FileUploadHandler.cleanup_temp_file(temp_path)
140
- st.write("- Cleanup: βœ… Success")
141
  else:
142
- st.write("- Save to /tmp: ❌ Failed")
143
 
144
  except Exception as e:
145
- st.error(f"Error with temp file approach: {e}")
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