Spaces:
Sleeping
Sleeping
Update src/utils.py
Browse files- src/utils.py +87 -87
src/utils.py
CHANGED
|
@@ -80,22 +80,94 @@ def load_background_image():
|
|
| 80 |
]
|
| 81 |
|
| 82 |
# Debug: Show current working directory and file structure
|
| 83 |
-
st.write("
|
| 84 |
-
st.write(f"
|
| 85 |
|
| 86 |
# Show what files exist in current directory
|
| 87 |
try:
|
| 88 |
current_files = os.listdir('.')
|
| 89 |
-
st.write(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Test 4: Try base64 encoding
|
| 92 |
-
st.write("
|
| 93 |
base64_image = base64.b64encode(img_bytes).decode()
|
| 94 |
-
st.write(f"
|
| 95 |
-
st.write(f"
|
| 96 |
|
| 97 |
# Test 5: Try simple CSS background
|
| 98 |
-
st.write("
|
| 99 |
st.markdown(
|
| 100 |
f"""
|
| 101 |
<div style="
|
|
@@ -120,7 +192,7 @@ def load_background_image():
|
|
| 120 |
)
|
| 121 |
|
| 122 |
# Test 6: Try your original complex CSS
|
| 123 |
-
st.write("
|
| 124 |
st.markdown(
|
| 125 |
f"""
|
| 126 |
<style>
|
|
@@ -168,18 +240,18 @@ def load_background_image():
|
|
| 168 |
unsafe_allow_html=True
|
| 169 |
)
|
| 170 |
|
| 171 |
-
st.write("
|
| 172 |
|
| 173 |
except Exception as pil_error:
|
| 174 |
-
st.error(f"
|
| 175 |
|
| 176 |
-
st.write("
|
| 177 |
try:
|
| 178 |
with open(image_path, 'rb') as f:
|
| 179 |
img_data = f.read()
|
| 180 |
img_base64 = base64.b64encode(img_data).decode()
|
| 181 |
|
| 182 |
-
st.write(f"
|
| 183 |
st.markdown(
|
| 184 |
f"""
|
| 185 |
<div style="
|
|
@@ -203,86 +275,14 @@ def load_background_image():
|
|
| 203 |
)
|
| 204 |
|
| 205 |
except Exception as direct_error:
|
| 206 |
-
st.error(f"
|
| 207 |
|
| 208 |
st.write("---")
|
| 209 |
-
st.write("
|
| 210 |
st.write("1. If any of the tests above show images, your image file is fine")
|
| 211 |
st.write("2. If no images show, try uploading a different image file")
|
| 212 |
st.write("3. If simple CSS works but complex CSS doesn't, there might be a styling conflict")
|
| 213 |
-
st.write("4. Check browser console for any CSS errors")
|
| 214 |
-
except:
|
| 215 |
-
st.write("❌ Cannot list current directory")
|
| 216 |
-
|
| 217 |
-
# Check if images directory exists
|
| 218 |
-
if os.path.exists('images'):
|
| 219 |
-
try:
|
| 220 |
-
image_files = os.listdir('images')
|
| 221 |
-
st.write(f"🖼️ Files in images directory: {image_files}")
|
| 222 |
-
except:
|
| 223 |
-
st.write("❌ Cannot list images directory")
|
| 224 |
-
else:
|
| 225 |
-
st.write("❌ Images directory does not exist")
|
| 226 |
-
|
| 227 |
-
# Test each possible path
|
| 228 |
-
st.write("🔍 **Testing each path:**")
|
| 229 |
-
image_path = None
|
| 230 |
-
for i, path in enumerate(possible_paths):
|
| 231 |
-
exists = os.path.exists(path)
|
| 232 |
-
st.write(f"{i+1}. `{path}` - {'✅ EXISTS' if exists else '❌ NOT FOUND'}")
|
| 233 |
-
if exists and not image_path:
|
| 234 |
-
image_path = path
|
| 235 |
-
|
| 236 |
-
if not image_path:
|
| 237 |
-
st.error("❌ Could not find image6.jpg in any expected location")
|
| 238 |
-
st.write("💡 **Suggestion:** Make sure image6.jpg is in the 'images' folder")
|
| 239 |
-
return
|
| 240 |
-
|
| 241 |
-
# Debug: File details
|
| 242 |
-
st.write(f"🎯 **Using image:** `{image_path}`")
|
| 243 |
-
|
| 244 |
-
try:
|
| 245 |
-
file_size = os.path.getsize(image_path)
|
| 246 |
-
st.write(f"📏 File size: {file_size:,} bytes")
|
| 247 |
-
|
| 248 |
-
# Check file signature
|
| 249 |
-
with open(image_path, 'rb') as f:
|
| 250 |
-
first_bytes = f.read(10)
|
| 251 |
-
st.write(f"🔍 First 10 bytes: {first_bytes}")
|
| 252 |
-
|
| 253 |
-
# Check if it's a valid JPEG (should start with FF D8)
|
| 254 |
-
if first_bytes[:2] == b'\xff\xd8':
|
| 255 |
-
st.write("✅ Valid JPEG header detected")
|
| 256 |
-
else:
|
| 257 |
-
st.write("⚠️ Not a standard JPEG file (might still work)")
|
| 258 |
-
|
| 259 |
-
except Exception as e:
|
| 260 |
-
st.error(f"❌ Error reading file details: {e}")
|
| 261 |
-
return
|
| 262 |
-
|
| 263 |
-
# Test 1: Try Streamlit's native image display
|
| 264 |
-
st.write("🧪 **Test 1: Streamlit native image display**")
|
| 265 |
-
try:
|
| 266 |
-
st.image(image_path, caption="Direct Streamlit image test", width=300)
|
| 267 |
-
st.write("✅ Streamlit can display the image")
|
| 268 |
-
except Exception as e:
|
| 269 |
-
st.error(f"❌ Streamlit image display failed: {e}")
|
| 270 |
-
|
| 271 |
-
# Test 2: Try PIL image opening
|
| 272 |
-
st.write("🧪 **Test 2: PIL image processing**")
|
| 273 |
-
try:
|
| 274 |
-
img = Image.open(image_path)
|
| 275 |
-
st.write(f"✅ PIL opened successfully")
|
| 276 |
-
st.write(f"📊 Format: {img.format}")
|
| 277 |
-
st.write(f"📐 Size: {img.size}")
|
| 278 |
-
st.write(f"🎨 Mode: {img.mode}")
|
| 279 |
-
|
| 280 |
-
# Test 3: Try converting to PNG
|
| 281 |
-
st.write("🧪 **Test 3: Converting to PNG in memory**")
|
| 282 |
-
img_buffer = io.BytesIO()
|
| 283 |
-
img.save(img_buffer, format='PNG')
|
| 284 |
-
img_bytes = img_buffer.getvalue()
|
| 285 |
-
st.write(f"
|
| 286 |
|
| 287 |
|
| 288 |
def initialise_session_state():
|
|
|
|
| 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 i, path in enumerate(possible_paths):
|
| 107 |
+
exists = os.path.exists(path)
|
| 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 |
+
# Test 3: Try converting to PNG
|
| 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 |
# Test 4: Try base64 encoding
|
| 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="
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
# Test 6: Try your original complex CSS
|
| 195 |
+
st.write("**Test 6: Your original CSS styling**")
|
| 196 |
st.markdown(
|
| 197 |
f"""
|
| 198 |
<style>
|
|
|
|
| 240 |
unsafe_allow_html=True
|
| 241 |
)
|
| 242 |
|
| 243 |
+
st.write("**All tests completed!** If you can see the images above, the problem might be with your original CSS classes or styling conflicts.")
|
| 244 |
|
| 245 |
except Exception as pil_error:
|
| 246 |
+
st.error(f"PIL processing failed: {pil_error}")
|
| 247 |
|
| 248 |
+
st.write("**Test: Direct file base64 encoding (bypass PIL)**")
|
| 249 |
try:
|
| 250 |
with open(image_path, 'rb') as f:
|
| 251 |
img_data = f.read()
|
| 252 |
img_base64 = base64.b64encode(img_data).decode()
|
| 253 |
|
| 254 |
+
st.write(f"Direct base64 encoding successful")
|
| 255 |
st.markdown(
|
| 256 |
f"""
|
| 257 |
<div style="
|
|
|
|
| 275 |
)
|
| 276 |
|
| 277 |
except Exception as direct_error:
|
| 278 |
+
st.error(f"Direct file encoding also failed: {direct_error}")
|
| 279 |
|
| 280 |
st.write("---")
|
| 281 |
+
st.write("**Next Steps:**")
|
| 282 |
st.write("1. If any of the tests above show images, your image file is fine")
|
| 283 |
st.write("2. If no images show, try uploading a different image file")
|
| 284 |
st.write("3. If simple CSS works but complex CSS doesn't, there might be a styling conflict")
|
| 285 |
+
st.write("4. Check browser console for any CSS errors")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
|
| 288 |
def initialise_session_state():
|