import streamlit as st import os import base64 from PIL import Image import io DEFAULT_SESSION_STATE = { # PDF Upload 'uploaded_files_name': [], 'collections_files_name': [], 'uploaded_files_raw': [], } def configure_page() -> None: """ Configures the Streamlit page. """ st.set_page_config(page_title="myRAG", layout="wide", page_icon=":rocket:") def apply_style(): st.markdown(""" """, unsafe_allow_html=True) def breaks(n=1): """ Creates a line break. """ if n == 1: st.markdown("
",unsafe_allow_html=True) elif n == 2: st.markdown("

",unsafe_allow_html=True) elif n == 3: st.markdown("


",unsafe_allow_html=True) else: st.markdown("



",unsafe_allow_html=True) def load_background_image(): """ Loads and displays a background image with an overlaid title. Uses PIL for safer image processing that passes HF security scans. """ possible_paths = [ "../images/image6.jpg", # Local development (from src/ folder) "images/image6.jpg", # Docker container (from /app) "./images/image6.jpg", # Current directory ] image_path = None for path in possible_paths: if os.path.exists(path): image_path = path break if not image_path: st.error("Could not find image6.jpg in any expected location") return try: # Use PIL for safe image processing (avoids HF pickle scanner issues) img = Image.open(image_path) # Convert to PNG format in memory for consistency img_buffer = io.BytesIO() img.save(img_buffer, format='PNG') img_bytes = img_buffer.getvalue() # Encode to base64 base64_image = base64.b64encode(img_bytes).decode() # Inject CSS for the background and title overlay st.markdown( f""" """, unsafe_allow_html=True ) # Create the background container with an overlaid title st.markdown( """
Mistral-RAG
""", unsafe_allow_html=True ) except Exception as e: st.error(f"Error loading background image: {e}") def initialise_session_state(): """ Initializes the session state variables if not already set. """ for key, default_val in DEFAULT_SESSION_STATE.items(): if key not in st.session_state: st.session_state[key] = default_val def file_uploader(): uploaded_files = st.file_uploader( "", type=["txt", "pdf"], accept_multiple_files=True) if uploaded_files: # Check if list is not empty for file in uploaded_files: # Process each file if file.name not in st.session_state.uploaded_files_name: # Append to session state lists safely st.session_state.uploaded_files_name.append(file.name) st.session_state.uploaded_files_raw.append(file) st.success(f"Added new file: {file.name}") else: st.info("Please upload a PDF file to proceed.") def initialise_session_state(): """ Initializes the session state variables if not already set. """ for key, default_val in DEFAULT_SESSION_STATE.items(): if key not in st.session_state: st.session_state[key] = default_val def file_uploader(): uploaded_files = st.file_uploader( "", type=["txt", "pdf"], accept_multiple_files=True) if uploaded_files: # Check if list is not empty for file in uploaded_files: # Process each file if file.name not in st.session_state.uploaded_files_name: # Append to session state lists safely st.session_state.uploaded_files_name.append(file.name) st.session_state.uploaded_files_raw.append(file) st.success(f"Added new file: {file.name}") else: st.info("Please upload a PDF file to proceed.")