Ashhar commited on
Commit
e8511c9
·
1 Parent(s): 21999ba

image validations

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. helpers/sidebar.py +7 -18
  3. utils.py +12 -0
app.py CHANGED
@@ -400,7 +400,7 @@ def mainApp():
400
  avatar = C.AI_ICON if role == "assistant" else C.USER_ICON
401
  with st.chat_message(role, avatar=avatar):
402
  st.markdown(content)
403
- if imagePath:
404
  st.image(imagePath)
405
  if buttons:
406
  __showButtons(buttons)
 
400
  avatar = C.AI_ICON if role == "assistant" else C.USER_ICON
401
  with st.chat_message(role, avatar=avatar):
402
  st.markdown(content)
403
+ if imagePath and U.isValidImageUrl(imagePath):
404
  st.image(imagePath)
405
  if buttons:
406
  __showButtons(buttons)
helpers/sidebar.py CHANGED
@@ -1,22 +1,10 @@
1
  import streamlit as st
2
- import requests
3
  import datetime as DT
4
  from helpers.activities import resetActivity, restoreUserActivity, ActivityLogs
5
  import constants as C
6
  import utils as U
7
 
8
 
9
- def isValidImageUrl(url):
10
- if not url:
11
- return False
12
- try:
13
- imageResponse = requests.head(url, timeout=5)
14
- contentType = imageResponse.headers.get('Content-Type', '')
15
- return imageResponse.status_code == 200 and contentType.startswith('image/')
16
- except Exception:
17
- return False
18
-
19
-
20
  def showSidebar():
21
  with st.sidebar:
22
  if not ("user" in st.session_state and "token" in st.session_state):
@@ -54,8 +42,8 @@ def showSidebar():
54
  </style>
55
  """, unsafe_allow_html=True)
56
 
57
- userAvatar = st.session_state["user"].get("picture", "https://example.com/default-avatar.png")
58
- if not isValidImageUrl(userAvatar):
59
  userAvatar = C.AVATAR_ICON
60
  userName = st.session_state["user"].get("name", "User")
61
  userEmail = st.session_state["user"].get("email", "")
@@ -81,7 +69,10 @@ def showSidebar():
81
  if st.button("\\+ New Story", help="Your current story will be auto-saved 😊", key="save_activities_button", type="primary", use_container_width=True):
82
  resetActivity()
83
 
84
- userActivityLogs: ActivityLogs = st.session_state.get("userActivitiesLog", [])
 
 
 
85
  U.pprint(f"{userActivityLogs=}")
86
 
87
  if not userActivityLogs:
@@ -96,7 +87,5 @@ def showSidebar():
96
  updatedAt = DT.datetime.fromisoformat(log["updated_at"].replace("Z", "+00:00"))
97
  localTime = updatedAt.astimezone().strftime("%b %d, %I:%M %p")
98
  activityId = log["id"]
99
- if activityId == st.session_state.get("activityId"):
100
- continue
101
- if st.button(f"Saved ⌛ {localTime}", key=f"activity_{log['id']}", use_container_width=True):
102
  restoreUserActivity(log["id"])
 
1
  import streamlit as st
 
2
  import datetime as DT
3
  from helpers.activities import resetActivity, restoreUserActivity, ActivityLogs
4
  import constants as C
5
  import utils as U
6
 
7
 
 
 
 
 
 
 
 
 
 
 
 
8
  def showSidebar():
9
  with st.sidebar:
10
  if not ("user" in st.session_state and "token" in st.session_state):
 
42
  </style>
43
  """, unsafe_allow_html=True)
44
 
45
+ userAvatar = st.session_state["user"].get("picture")
46
+ if not U.isValidImageUrl(userAvatar):
47
  userAvatar = C.AVATAR_ICON
48
  userName = st.session_state["user"].get("name", "User")
49
  userEmail = st.session_state["user"].get("email", "")
 
69
  if st.button("\\+ New Story", help="Your current story will be auto-saved 😊", key="save_activities_button", type="primary", use_container_width=True):
70
  resetActivity()
71
 
72
+ userActivityLogs: ActivityLogs = [
73
+ log for log in st.session_state.get("userActivitiesLog", [])
74
+ if log["id"] != st.session_state.get("activityId")
75
+ ]
76
  U.pprint(f"{userActivityLogs=}")
77
 
78
  if not userActivityLogs:
 
87
  updatedAt = DT.datetime.fromisoformat(log["updated_at"].replace("Z", "+00:00"))
88
  localTime = updatedAt.astimezone().strftime("%b %d, %I:%M %p")
89
  activityId = log["id"]
90
+ if st.button(f"Saved ⌛ {localTime}", key=f"activity_{activityId}", use_container_width=True):
 
 
91
  restoreUserActivity(log["id"])
utils.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import datetime as DT
3
  import pytz
 
4
 
5
  FONTS = [
6
  # "Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900",
@@ -128,3 +129,14 @@ def applyCommonStyles():
128
  """,
129
  unsafe_allow_html=True
130
  )
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import datetime as DT
3
  import pytz
4
+ import requests
5
 
6
  FONTS = [
7
  # "Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900",
 
129
  """,
130
  unsafe_allow_html=True
131
  )
132
+
133
+
134
+ def isValidImageUrl(url):
135
+ if not url:
136
+ return False
137
+ try:
138
+ imageResponse = requests.head(url, timeout=5)
139
+ contentType = imageResponse.headers.get('Content-Type', '')
140
+ return imageResponse.status_code == 200 and contentType.startswith('image/')
141
+ except Exception:
142
+ return False