Spaces:
Sleeping
Sleeping
Update src/utils.py
Browse files- src/utils.py +122 -155
src/utils.py
CHANGED
|
@@ -13,6 +13,73 @@ DEFAULT_SESSION_STATE = {
|
|
| 13 |
}
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def configure_page() -> None:
|
| 17 |
"""
|
| 18 |
Configures the Streamlit page.
|
|
@@ -66,137 +133,43 @@ def breaks(n=1):
|
|
| 66 |
|
| 67 |
def load_background_image():
|
| 68 |
"""
|
| 69 |
-
|
|
|
|
| 70 |
"""
|
| 71 |
-
import os
|
| 72 |
-
import base64
|
| 73 |
-
from PIL import Image
|
| 74 |
-
import io
|
| 75 |
-
|
| 76 |
possible_paths = [
|
| 77 |
"../images/image6.jpg", # Local development (from src/ folder)
|
| 78 |
"images/image6.jpg", # Docker container (from /app)
|
| 79 |
"./images/image6.jpg", # Current directory
|
| 80 |
]
|
| 81 |
|
| 82 |
-
# Debug: Show current working directory and file structure
|
| 83 |
-
st.write("**Debug Information:**")
|
| 84 |
-
st.write(f"Current working directory: `{os.getcwd()}`")
|
| 85 |
-
|
| 86 |
-
# Show what files exist in current directory
|
| 87 |
-
try:
|
| 88 |
-
current_files = os.listdir('.')
|
| 89 |
-
st.write(f"Files in current directory: {current_files}")
|
| 90 |
-
except:
|
| 91 |
-
st.write("Cannot list current directory")
|
| 92 |
-
|
| 93 |
-
# Check if images directory exists
|
| 94 |
-
if os.path.exists('images'):
|
| 95 |
-
try:
|
| 96 |
-
image_files = os.listdir('images')
|
| 97 |
-
st.write(f"Files in images directory: {image_files}")
|
| 98 |
-
except:
|
| 99 |
-
st.write("Cannot list images directory")
|
| 100 |
-
else:
|
| 101 |
-
st.write("Images directory does not exist")
|
| 102 |
-
|
| 103 |
-
# Test each possible path
|
| 104 |
-
st.write("**Testing each path:**")
|
| 105 |
image_path = None
|
| 106 |
-
for
|
| 107 |
-
|
| 108 |
-
st.write(f"{i+1}. `{path}` - {'EXISTS' if exists else 'NOT FOUND'}")
|
| 109 |
-
if exists and not image_path:
|
| 110 |
image_path = path
|
|
|
|
| 111 |
|
| 112 |
if not image_path:
|
| 113 |
st.error("Could not find image6.jpg in any expected location")
|
| 114 |
-
st.write("**Suggestion:** Make sure image6.jpg is in the 'images' folder")
|
| 115 |
-
return
|
| 116 |
-
|
| 117 |
-
# Debug: File details
|
| 118 |
-
st.write(f"**Using image:** `{image_path}`")
|
| 119 |
-
|
| 120 |
-
try:
|
| 121 |
-
file_size = os.path.getsize(image_path)
|
| 122 |
-
st.write(f"File size: {file_size:,} bytes")
|
| 123 |
-
|
| 124 |
-
# Check file signature
|
| 125 |
-
with open(image_path, 'rb') as f:
|
| 126 |
-
first_bytes = f.read(10)
|
| 127 |
-
st.write(f"First 10 bytes: {first_bytes}")
|
| 128 |
-
|
| 129 |
-
# Check if it's a valid JPEG (should start with FF D8)
|
| 130 |
-
if first_bytes[:2] == b'\xff\xd8':
|
| 131 |
-
st.write("Valid JPEG header detected")
|
| 132 |
-
else:
|
| 133 |
-
st.write("Not a standard JPEG file (might still work)")
|
| 134 |
-
|
| 135 |
-
except Exception as e:
|
| 136 |
-
st.error(f"Error reading file details: {e}")
|
| 137 |
return
|
| 138 |
|
| 139 |
-
# Test 1: Try Streamlit's native image display
|
| 140 |
-
st.write("**Test 1: Streamlit native image display**")
|
| 141 |
-
try:
|
| 142 |
-
st.image(image_path, caption="Direct Streamlit image test", width=300)
|
| 143 |
-
st.write("Streamlit can display the image")
|
| 144 |
-
except Exception as e:
|
| 145 |
-
st.error(f"Streamlit image display failed: {e}")
|
| 146 |
-
|
| 147 |
-
# Test 2: Try PIL image opening
|
| 148 |
-
st.write("**Test 2: PIL image processing**")
|
| 149 |
try:
|
|
|
|
| 150 |
img = Image.open(image_path)
|
| 151 |
-
st.write(f"PIL opened successfully")
|
| 152 |
-
st.write(f"Format: {img.format}")
|
| 153 |
-
st.write(f"Size: {img.size}")
|
| 154 |
-
st.write(f"Mode: {img.mode}")
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
st.write("**Test 3: Converting to PNG in memory**")
|
| 158 |
img_buffer = io.BytesIO()
|
| 159 |
img.save(img_buffer, format='PNG')
|
| 160 |
img_bytes = img_buffer.getvalue()
|
| 161 |
-
st.write(f"PNG conversion successful, size: {len(img_bytes):,} bytes")
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
st.write("**Test 4: Base64 encoding**")
|
| 165 |
base64_image = base64.b64encode(img_bytes).decode()
|
| 166 |
-
st.write(f"Base64 encoding successful, length: {len(base64_image):,} characters")
|
| 167 |
-
st.write(f"First 50 chars: `{base64_image[:50]}...`")
|
| 168 |
-
|
| 169 |
-
# Test 5: Try simple CSS background
|
| 170 |
-
st.write("**Test 5: Simple CSS background test**")
|
| 171 |
-
st.markdown(
|
| 172 |
-
f"""
|
| 173 |
-
<div style="
|
| 174 |
-
background-image: url('data:image/png;base64,{base64_image}');
|
| 175 |
-
background-size: cover;
|
| 176 |
-
background-position: center;
|
| 177 |
-
height: 200px;
|
| 178 |
-
width: 100%;
|
| 179 |
-
border: 3px solid red;
|
| 180 |
-
color: white;
|
| 181 |
-
display: flex;
|
| 182 |
-
align-items: center;
|
| 183 |
-
justify-content: center;
|
| 184 |
-
font-size: 24px;
|
| 185 |
-
font-weight: bold;
|
| 186 |
-
text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
|
| 187 |
-
">
|
| 188 |
-
SIMPLE CSS TEST
|
| 189 |
-
</div>
|
| 190 |
-
""",
|
| 191 |
-
unsafe_allow_html=True
|
| 192 |
-
)
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
st.write("**Test 6: Your original CSS styling**")
|
| 196 |
st.markdown(
|
| 197 |
f"""
|
| 198 |
<style>
|
| 199 |
-
|
|
|
|
| 200 |
position: relative;
|
| 201 |
background-image: url("data:image/png;base64,{base64_image}");
|
| 202 |
background-size: cover;
|
|
@@ -207,10 +180,10 @@ def load_background_image():
|
|
| 207 |
margin: 0 auto;
|
| 208 |
filter: contrast(110%) brightness(210%);
|
| 209 |
border-radius: 100px;
|
| 210 |
-
overflow: hidden;
|
| 211 |
-
border: 2px solid blue;
|
| 212 |
}}
|
| 213 |
-
|
|
|
|
| 214 |
content: '';
|
| 215 |
position: absolute;
|
| 216 |
top: 0;
|
|
@@ -220,69 +193,63 @@ def load_background_image():
|
|
| 220 |
background-color: rgba(20, 10, 20, 0.44);
|
| 221 |
z-index: 1;
|
| 222 |
}}
|
| 223 |
-
|
|
|
|
| 224 |
position: absolute;
|
| 225 |
top: 50%;
|
| 226 |
left: 50%;
|
| 227 |
transform: translate(-50%, -50%);
|
| 228 |
-
color:
|
| 229 |
font-size: 50px;
|
| 230 |
font-weight: bold;
|
| 231 |
-
text-shadow:
|
| 232 |
text-align: center;
|
| 233 |
z-index: 2;
|
| 234 |
}}
|
| 235 |
</style>
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
</div>
|
| 239 |
""",
|
| 240 |
unsafe_allow_html=True
|
| 241 |
)
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
height: 200px;
|
| 262 |
-
width: 100%;
|
| 263 |
-
border: 3px solid green;
|
| 264 |
-
color: white;
|
| 265 |
-
display: flex;
|
| 266 |
-
align-items: center;
|
| 267 |
-
justify-content: center;
|
| 268 |
-
font-size: 24px;
|
| 269 |
-
font-weight: bold;
|
| 270 |
-
">
|
| 271 |
-
DIRECT FILE ENCODING TEST
|
| 272 |
-
</div>
|
| 273 |
-
""",
|
| 274 |
-
unsafe_allow_html=True
|
| 275 |
-
)
|
| 276 |
-
|
| 277 |
-
except Exception as direct_error:
|
| 278 |
-
st.error(f"Direct file encoding also failed: {direct_error}")
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
|
| 288 |
def initialise_session_state():
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
|
| 16 |
+
def configure_page() -> None:
|
| 17 |
+
"""
|
| 18 |
+
Configures the Streamlit page.
|
| 19 |
+
"""
|
| 20 |
+
st.set_page_config(page_title="myRAG",
|
| 21 |
+
layout="wide",
|
| 22 |
+
page_icon=":rocket:")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def apply_style():
|
| 26 |
+
st.markdown("""
|
| 27 |
+
<style>
|
| 28 |
+
@import url('https://fonts.googleapis.com/css2?family=Work+Sans:wght@400;700&display=swap');
|
| 29 |
+
html, body, .stApp,
|
| 30 |
+
.css-1v3fvcr, .css-ffhzg2, .css-1d391kg,
|
| 31 |
+
div[data-testid="stMarkdownContainer"],
|
| 32 |
+
div[data-testid="stText"],
|
| 33 |
+
div[data-testid="stTextInput"],
|
| 34 |
+
div[data-testid="stSelectbox"],
|
| 35 |
+
div[data-testid="stCheckbox"],
|
| 36 |
+
div[data-testid="stSlider"],
|
| 37 |
+
label, input, textarea, button, select,
|
| 38 |
+
.stButton, .stTextInput > div, .stMarkdown, .stCaption,
|
| 39 |
+
.streamlit-expanderHeader, .st-expander > div,
|
| 40 |
+
h1, h2, h3, h4, h5, h6,
|
| 41 |
+
.stMarkdown h1, .stMarkdown h2, .stMarkdown h3 {
|
| 42 |
+
font-family: 'Work Sans', sans-serif !important;
|
| 43 |
+
}
|
| 44 |
+
/* Ensure bold text uses the correct font weight */
|
| 45 |
+
strong, b, .stMarkdown strong, .stMarkdown b {
|
| 46 |
+
font-family: 'Work Sans', sans-serif !important;
|
| 47 |
+
font-weight: 700 !important;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
| 50 |
+
""", unsafe_allow_html=True)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def breaks(n=1):
|
| 54 |
+
"""
|
| 55 |
+
Creates a line break.
|
| 56 |
+
"""
|
| 57 |
+
if n == 1:
|
| 58 |
+
st.markdown("<br>",unsafe_allow_html=True)
|
| 59 |
+
elif n == 2:
|
| 60 |
+
st.markdown("<br><br>",unsafe_allow_html=True)
|
| 61 |
+
elif n == 3:
|
| 62 |
+
st.markdown("<br><br><br>",unsafe_allow_html=True)
|
| 63 |
+
else:
|
| 64 |
+
st.markdown("<br><br><br><br>",unsafe_allow_html=True)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
import streamlit as st
|
| 69 |
+
import os
|
| 70 |
+
import base64
|
| 71 |
+
from PIL import Image
|
| 72 |
+
import io
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
DEFAULT_SESSION_STATE = {
|
| 76 |
+
# PDF Upload
|
| 77 |
+
'uploaded_files_name': [],
|
| 78 |
+
'collections_files_name': [],
|
| 79 |
+
'uploaded_files_raw': [],
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
def configure_page() -> None:
|
| 84 |
"""
|
| 85 |
Configures the Streamlit page.
|
|
|
|
| 133 |
|
| 134 |
def load_background_image():
|
| 135 |
"""
|
| 136 |
+
Loads and displays a background image with an overlaid title.
|
| 137 |
+
Uses PIL for safer image processing that passes HF security scans.
|
| 138 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
possible_paths = [
|
| 140 |
"../images/image6.jpg", # Local development (from src/ folder)
|
| 141 |
"images/image6.jpg", # Docker container (from /app)
|
| 142 |
"./images/image6.jpg", # Current directory
|
| 143 |
]
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
image_path = None
|
| 146 |
+
for path in possible_paths:
|
| 147 |
+
if os.path.exists(path):
|
|
|
|
|
|
|
| 148 |
image_path = path
|
| 149 |
+
break
|
| 150 |
|
| 151 |
if not image_path:
|
| 152 |
st.error("Could not find image6.jpg in any expected location")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
return
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
try:
|
| 156 |
+
# Use PIL for safe image processing (avoids HF pickle scanner issues)
|
| 157 |
img = Image.open(image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
# Convert to PNG format in memory for consistency
|
|
|
|
| 160 |
img_buffer = io.BytesIO()
|
| 161 |
img.save(img_buffer, format='PNG')
|
| 162 |
img_bytes = img_buffer.getvalue()
|
|
|
|
| 163 |
|
| 164 |
+
# Encode to base64
|
|
|
|
| 165 |
base64_image = base64.b64encode(img_bytes).decode()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
# Inject CSS for the background and title overlay
|
|
|
|
| 168 |
st.markdown(
|
| 169 |
f"""
|
| 170 |
<style>
|
| 171 |
+
/* Background container with image */
|
| 172 |
+
.bg-container {{
|
| 173 |
position: relative;
|
| 174 |
background-image: url("data:image/png;base64,{base64_image}");
|
| 175 |
background-size: cover;
|
|
|
|
| 180 |
margin: 0 auto;
|
| 181 |
filter: contrast(110%) brightness(210%);
|
| 182 |
border-radius: 100px;
|
| 183 |
+
overflow: hidden;
|
|
|
|
| 184 |
}}
|
| 185 |
+
/* Overlay for dimming effect */
|
| 186 |
+
.bg-container::after {{
|
| 187 |
content: '';
|
| 188 |
position: absolute;
|
| 189 |
top: 0;
|
|
|
|
| 193 |
background-color: rgba(20, 10, 20, 0.44);
|
| 194 |
z-index: 1;
|
| 195 |
}}
|
| 196 |
+
/* Overlay title styling */
|
| 197 |
+
.overlay-title {{
|
| 198 |
position: absolute;
|
| 199 |
top: 50%;
|
| 200 |
left: 50%;
|
| 201 |
transform: translate(-50%, -50%);
|
| 202 |
+
color: black;
|
| 203 |
font-size: 50px;
|
| 204 |
font-weight: bold;
|
| 205 |
+
text-shadow: 1px 1px 3px rgba(255, 255, 255, .0);
|
| 206 |
text-align: center;
|
| 207 |
z-index: 2;
|
| 208 |
}}
|
| 209 |
</style>
|
| 210 |
+
""",
|
| 211 |
+
unsafe_allow_html=True
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
# Create the background container with an overlaid title
|
| 215 |
+
st.markdown(
|
| 216 |
+
"""
|
| 217 |
+
<div class="bg-container">
|
| 218 |
+
<div class="overlay-title">Mistral-RAG</div>
|
| 219 |
</div>
|
| 220 |
""",
|
| 221 |
unsafe_allow_html=True
|
| 222 |
)
|
| 223 |
|
| 224 |
+
except Exception as e:
|
| 225 |
+
st.error(f"Error loading background image: {e}")
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def initialise_session_state():
|
| 229 |
+
"""
|
| 230 |
+
Initializes the session state variables if not already set.
|
| 231 |
+
"""
|
| 232 |
+
for key, default_val in DEFAULT_SESSION_STATE.items():
|
| 233 |
+
if key not in st.session_state:
|
| 234 |
+
st.session_state[key] = default_val
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def file_uploader():
|
| 238 |
+
uploaded_files = st.file_uploader(
|
| 239 |
+
"",
|
| 240 |
+
type=["txt", "pdf"],
|
| 241 |
+
accept_multiple_files=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
+
if uploaded_files: # Check if list is not empty
|
| 244 |
+
for file in uploaded_files: # Process each file
|
| 245 |
+
if file.name not in st.session_state.uploaded_files_name:
|
| 246 |
+
# Append to session state lists safely
|
| 247 |
+
st.session_state.uploaded_files_name.append(file.name)
|
| 248 |
+
st.session_state.uploaded_files_raw.append(file)
|
| 249 |
+
st.success(f"Added new file: {file.name}")
|
| 250 |
+
|
| 251 |
+
else:
|
| 252 |
+
st.info("Please upload a PDF file to proceed.")
|
| 253 |
|
| 254 |
|
| 255 |
def initialise_session_state():
|