Spaces:
Sleeping
Sleeping
Download src/components/file_uploader.py from faseehsandhu/Digitalytics: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/spaces/faseehsandhu/Digitalytics/resolve/main/src/components/file_uploader.py
- Command line
-
hf download hf://spaces/faseehsandhu/Digitalytics/src/components/file_uploader.py
-
curl -L -o file_uploader.py https://huggingface.co/spaces/faseehsandhu/Digitalytics/resolve/main/src/components/file_uploader.py
2.73 kB
| import streamlit as st | |
| import os | |
| import tempfile | |
| from pathlib import Path | |
| class FileUploader: | |
| def __init__(self, data_service=None): | |
| self.data_service = data_service | |
| def _get_session_upload_dir(self): | |
| """Get or create a session-specific upload directory.""" | |
| # Create a unique directory for this session | |
| if 'session_upload_dir' not in st.session_state: | |
| # Create a temp directory that will exist only for this session | |
| session_dir = tempfile.mkdtemp(prefix="session_", dir="data/user_uploads") | |
| st.session_state.session_upload_dir = session_dir | |
| st.session_state.session_uploaded_files = [] | |
| return st.session_state.session_upload_dir | |
| def display(self): | |
| st.header("Upload Your Data Dictionary") | |
| uploaded_file = st.file_uploader("Choose a file", type=["pdf", "xlsx", "xls", "csv"]) | |
| if uploaded_file is not None: | |
| # Get session-specific directory | |
| session_dir = self._get_session_upload_dir() | |
| file_path = os.path.join(session_dir, uploaded_file.name) | |
| # Check if file already uploaded in this session | |
| if file_path not in st.session_state.session_uploaded_files: | |
| # Save the uploaded file to the session directory | |
| with open(file_path, "wb") as f: | |
| f.write(uploaded_file.getbuffer()) | |
| st.session_state.session_uploaded_files.append(file_path) | |
| st.success(f"โ File '{uploaded_file.name}' uploaded successfully!") | |
| # Reload user dictionaries if data_service is available | |
| if self.data_service: | |
| with st.spinner("Processing uploaded file..."): | |
| # Load only this session's dictionaries | |
| self.data_service.load_session_dictionaries(session_dir) | |
| st.info(f"๐ File added to your conversation") | |
| # Update session state to trigger UI refresh | |
| if 'data_service' in st.session_state: | |
| st.session_state.data_service = self.data_service | |
| # Show uploaded files in this session | |
| if 'session_uploaded_files' in st.session_state and st.session_state.session_uploaded_files: | |
| st.write("**Files in this session:**") | |
| for file_path in st.session_state.session_uploaded_files: | |
| st.write(f"- {Path(file_path).name}") | |
| st.info("๐ Supported formats: PDF, Excel (XLSX, XLS), CSV") | |
| st.caption("โ ๏ธ Files will be removed when you refresh the page") |