Spaces:
Sleeping
Sleeping
Update view_images.py
Browse files- view_images.py +84 -84
view_images.py
CHANGED
@@ -1,84 +1,84 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from pymongo import MongoClient
|
3 |
-
import os
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
from datetime import datetime
|
6 |
-
|
7 |
-
# Load environment variables
|
8 |
-
load_dotenv()
|
9 |
-
MONGO_URI = os.getenv("MONGO_URI")
|
10 |
-
DB_NAME = os.getenv("DB_NAME")
|
11 |
-
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
|
12 |
-
|
13 |
-
mongo_client = MongoClient(MONGO_URI)
|
14 |
-
db = mongo_client[DB_NAME]
|
15 |
-
collection = db[COLLECTION_NAME]
|
16 |
-
|
17 |
-
def format_date(timestamp):
|
18 |
-
"""Convert timestamp to a readable date format."""
|
19 |
-
return datetime.fromtimestamp(timestamp).strftime("%B %d, %Y")
|
20 |
-
|
21 |
-
# Custom CSS to control image and expander container width and styling
|
22 |
-
|
23 |
-
|
24 |
-
def view_images():
|
25 |
-
if st.button("Back"):
|
26 |
-
st.session_state.page = "upload_main"
|
27 |
-
st.rerun()
|
28 |
-
st.title("Your Uploaded Images")
|
29 |
-
|
30 |
-
# Fetch all uploaded images from MongoDB
|
31 |
-
images = list(collection.find({"type": "Image"}))
|
32 |
-
|
33 |
-
if not images:
|
34 |
-
st.write("You have not uploaded any images yet.")
|
35 |
-
return
|
36 |
-
|
37 |
-
# Display images in a grid (4 images per row)
|
38 |
-
cols = st.columns(4)
|
39 |
-
for idx, image in enumerate(images):
|
40 |
-
col = cols[idx % 4]
|
41 |
-
|
42 |
-
with col:
|
43 |
-
# Container for each image and its expander
|
44 |
-
st.markdown("<div class='image-wrapper'>", unsafe_allow_html=True)
|
45 |
-
|
46 |
-
# Display the image using HTML
|
47 |
-
|
48 |
-
st.markdown(
|
49 |
-
f"""
|
50 |
-
<div style='text-align: center;'>
|
51 |
-
<img src='{image['object_url']}' alt='{image.get('name','Image')}' style='width:250px; height:250px; object-fit: cover; border-radius: 8px;' />
|
52 |
-
|
53 |
-
</div>
|
54 |
-
""",
|
55 |
-
unsafe_allow_html=True
|
56 |
-
)
|
57 |
-
|
58 |
-
st.markdown("</div>", unsafe_allow_html=True) # Close image container
|
59 |
-
|
60 |
-
# Expander for image details
|
61 |
-
with st.expander("View Image Details"):
|
62 |
-
st.write(f"**File Name:** {image.get('name', 'N/A')}")
|
63 |
-
st.write(f"**Date Uploaded:** {format_date(image.get('upload_date', datetime.now().timestamp()))}")
|
64 |
-
st.write(f"**Description:** {image.get('description', 'No description available')}")
|
65 |
-
|
66 |
-
# Display tags if available
|
67 |
-
tags = ", ".join(image.get("tags", []))
|
68 |
-
st.write(f"**Tags:** {tags if tags else 'No tags'}")
|
69 |
-
|
70 |
-
# Display categories if available
|
71 |
-
categories = ", ".join(image.get("categories", []))
|
72 |
-
st.write(f"**Categories:** {categories if categories else 'No categories'}")
|
73 |
-
|
74 |
-
# Download link
|
75 |
-
st.markdown(
|
76 |
-
f"<a href='{image['object_url']}' class='download-link' download>Download Image</a>",
|
77 |
-
unsafe_allow_html=True
|
78 |
-
)
|
79 |
-
|
80 |
-
# Move to a new row after every 4 images
|
81 |
-
if (idx + 1) % 4 == 0:
|
82 |
-
st.write("") # Line break to move to the next row
|
83 |
-
|
84 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from pymongo import MongoClient
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
10 |
+
DB_NAME = os.getenv("DB_NAME")
|
11 |
+
COLLECTION_NAME = os.getenv("COLLECTION_NAME")
|
12 |
+
|
13 |
+
mongo_client = MongoClient(MONGO_URI)
|
14 |
+
db = mongo_client[DB_NAME]
|
15 |
+
collection = db[COLLECTION_NAME]
|
16 |
+
|
17 |
+
def format_date(timestamp):
|
18 |
+
"""Convert timestamp to a readable date format."""
|
19 |
+
return datetime.fromtimestamp(timestamp).strftime("%B %d, %Y")
|
20 |
+
|
21 |
+
# Custom CSS to control image and expander container width and styling
|
22 |
+
|
23 |
+
|
24 |
+
def view_images():
|
25 |
+
if st.button("Back"):
|
26 |
+
st.session_state.page = "upload_main"
|
27 |
+
st.rerun()
|
28 |
+
st.title("Your Uploaded Images")
|
29 |
+
|
30 |
+
# Fetch all uploaded images from MongoDB
|
31 |
+
images = list(collection.find({"type": "Image","status":"processed"}))
|
32 |
+
|
33 |
+
if not images:
|
34 |
+
st.write("You have not uploaded any images yet.")
|
35 |
+
return
|
36 |
+
|
37 |
+
# Display images in a grid (4 images per row)
|
38 |
+
cols = st.columns(4)
|
39 |
+
for idx, image in enumerate(images):
|
40 |
+
col = cols[idx % 4]
|
41 |
+
|
42 |
+
with col:
|
43 |
+
# Container for each image and its expander
|
44 |
+
st.markdown("<div class='image-wrapper'>", unsafe_allow_html=True)
|
45 |
+
|
46 |
+
# Display the image using HTML
|
47 |
+
|
48 |
+
st.markdown(
|
49 |
+
f"""
|
50 |
+
<div style='text-align: center;'>
|
51 |
+
<img src='{image['object_url']}' alt='{image.get('name','Image')}' style='width:250px; height:250px; object-fit: cover; border-radius: 8px;' />
|
52 |
+
|
53 |
+
</div>
|
54 |
+
""",
|
55 |
+
unsafe_allow_html=True
|
56 |
+
)
|
57 |
+
|
58 |
+
st.markdown("</div>", unsafe_allow_html=True) # Close image container
|
59 |
+
|
60 |
+
# Expander for image details
|
61 |
+
with st.expander("View Image Details"):
|
62 |
+
st.write(f"**File Name:** {image.get('name', 'N/A')}")
|
63 |
+
st.write(f"**Date Uploaded:** {format_date(image.get('upload_date', datetime.now().timestamp()))}")
|
64 |
+
st.write(f"**Description:** {image.get('description', 'No description available')}")
|
65 |
+
|
66 |
+
# Display tags if available
|
67 |
+
tags = ", ".join(image.get("tags", []))
|
68 |
+
st.write(f"**Tags:** {tags if tags else 'No tags'}")
|
69 |
+
|
70 |
+
# Display categories if available
|
71 |
+
categories = ", ".join(image.get("categories", []))
|
72 |
+
st.write(f"**Categories:** {categories if categories else 'No categories'}")
|
73 |
+
|
74 |
+
# Download link
|
75 |
+
st.markdown(
|
76 |
+
f"<a href='{image['object_url']}' class='download-link' download>Download Image</a>",
|
77 |
+
unsafe_allow_html=True
|
78 |
+
)
|
79 |
+
|
80 |
+
# Move to a new row after every 4 images
|
81 |
+
if (idx + 1) % 4 == 0:
|
82 |
+
st.write("") # Line break to move to the next row
|
83 |
+
|
84 |
+
|