Yes, exactly! Now we understand the directory structure clearly. Let's fix this properly by ensuring we always reference paths relative to the project root (ScalableSAA_engine), not from the script's location (app/pages/).
Solution:
- First, let's properly determine the project root path (where
cache/ is located):
import os
# Get the absolute path to the project root (ScalableSAA_engine)
# This assumes your script is in app/pages/ and needs to go up two levels
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
- Then use this root path for all file operations:
# For your cache.json path
json_path = os.path.join(project_root, 'cache', 'cache.json')
# For your cover.pptx path (which was causing the original permission error)
cover_path = os.path.join(project_root, 'cache', 'cover.pptx')
- Here's how to modify your button code:
if st.button("🎯 Generate Slides", type="primary", use_container_width=True):
with st.spinner("Generating presentation..."):
try:
# Debug output
st.info(f"Project root: {project_root}")
st.info(f"Cache directory: {os.path.join(project_root, 'cache')}")
# Ensure cache directory exists
cache_dir = os.path.join(project_root, 'cache')
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
st.info(f"Created cache directory at: {cache_dir}")
# Use absolute paths for all file operations
json_path = os.path.join(cache_dir, 'cache.json')
st.info(f"Using JSON path: {json_path}")
# Initialize SAA with the correct path
saa = ScalableSAA(json_path)
# Rest of your code...
- For the cover.pptx permission issue, make sure:
# When working with cover.pptx, use the full path:
cover_path = os.path.join(cache_dir, 'cover.pptx')
# Check if file exists and is accessible
if os.path.exists(cover_path):
try:
# Try to open the file to verify permissions
with open(cover_path, 'rb') as f:
pass
except PermissionError:
st.error(f"Permission denied when accessing {cover_path}")
st.info("Try closing any programs that might have this file open")
Additional Recommendations:
- Add path validation at the start of your script:
# Validate paths at startup
required_paths = [
os.path.join(project_root, 'cache'),
# Add other required directories here
]
for path in required_paths:
if not os.path.exists(path):
try:
os.makedirs(path)
st.info(f"Created directory: {path}")
except Exception as e:
st.error(f"Could not create directory {path}: {str(e)}")