Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
02e28ee
1
Parent(s):
85a3efb
docs: added docstrings and return type hints
Browse files- call_models/st_logs.py +70 -7
- call_models/whale_gallery.py +7 -1
- call_models/whale_viewer.py +11 -2
call_models/st_logs.py
CHANGED
|
@@ -22,8 +22,34 @@ log_pattern = re.compile(_log_n_re + _log_date_re + _sep + _log_mod_re + _sep +
|
|
| 22 |
|
| 23 |
|
| 24 |
class StreamlitLogHandler(logging.Handler):
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def __init__(self, container, maxlen:int=15, debug:bool=False):
|
|
|
|
| 27 |
super().__init__()
|
| 28 |
# Store the Streamlit container for log output
|
| 29 |
self.container = container
|
|
@@ -34,7 +60,16 @@ class StreamlitLogHandler(logging.Handler):
|
|
| 34 |
self.buffer = deque(maxlen=maxlen)
|
| 35 |
self._n = 0
|
| 36 |
|
| 37 |
-
def n_elems(self, verb:bool=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
''' return a string with num elements seen and num elements in buffer '''
|
| 39 |
if verb:
|
| 40 |
return f"total: {self._n}|| in buffer:{len(self.buffer)}"
|
|
@@ -49,13 +84,28 @@ class StreamlitLogHandler(logging.Handler):
|
|
| 49 |
if self.debug:
|
| 50 |
self.log_area.markdown(clean_msg)
|
| 51 |
|
| 52 |
-
def clear_logs(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
self.log_area.empty() # Clear previous logs
|
| 54 |
self.buffer.clear()
|
| 55 |
|
| 56 |
# Set up logging to capture all info level logs from the root logger
|
| 57 |
@st.cache_resource
|
| 58 |
-
def setup_logging(level:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
root_logger = logging.getLogger() # Get the root logger
|
| 60 |
log_container = st.container() # Create a container within which we display logs
|
| 61 |
handler = StreamlitLogHandler(log_container, maxlen=buffer_len)
|
|
@@ -70,7 +120,20 @@ def setup_logging(level: int=logging.INFO, buffer_len:int=15):
|
|
| 70 |
return handler
|
| 71 |
|
| 72 |
def parse_log_buffer(log_contents: deque) -> list:
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
j = 0
|
| 75 |
records = []
|
| 76 |
for line in log_contents:
|
|
@@ -100,7 +163,7 @@ def parse_log_buffer(log_contents: deque) -> list:
|
|
| 100 |
continue
|
| 101 |
return records
|
| 102 |
|
| 103 |
-
def
|
| 104 |
'''function to demo adding log entries'''
|
| 105 |
logger = logging.getLogger(__name__)
|
| 106 |
logger.setLevel(logging.DEBUG)
|
|
@@ -121,7 +184,7 @@ if __name__ == "__main__":
|
|
| 121 |
|
| 122 |
c1, c2 = st.columns([1, 3])
|
| 123 |
with c1:
|
| 124 |
-
button = st.button("do something", on_click=
|
| 125 |
with c2:
|
| 126 |
st.info(f"Length of records: {len(records)}")
|
| 127 |
#tab = st.table(records)
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
class StreamlitLogHandler(logging.Handler):
|
| 25 |
+
"""
|
| 26 |
+
Custom Streamlit log handler to display logs in a Streamlit container
|
| 27 |
+
|
| 28 |
+
A custom logging handler for Streamlit applications that displays log
|
| 29 |
+
messages in a Streamlit container.
|
| 30 |
+
|
| 31 |
+
Attributes:
|
| 32 |
+
container (streamlit.DeltaGenerator): The Streamlit container where log messages will be displayed.
|
| 33 |
+
debug (bool): A flag to indicate whether to display debug messages.
|
| 34 |
+
ansi_escape (re.Pattern): A compiled regular expression to remove ANSI escape sequences from log messages.
|
| 35 |
+
log_area (streamlit.DeltaGenerator): An empty Streamlit container for log output.
|
| 36 |
+
buffer (collections.deque): A deque buffer to store log messages with a maximum length.
|
| 37 |
+
_n (int): A counter to keep track of the number of log messages seen.
|
| 38 |
+
Methods:
|
| 39 |
+
__init__(container, maxlen=15, debug=False):
|
| 40 |
+
Initializes the StreamlitLogHandler with a Streamlit container, buffer length, and debug flag.
|
| 41 |
+
n_elems(verb=False):
|
| 42 |
+
Returns a string with the total number of elements seen and the number of elements in the buffer.
|
| 43 |
+
If verb is True, returns a verbose string; otherwise, returns a concise string.
|
| 44 |
+
emit(record):
|
| 45 |
+
Processes a log record, formats it, appends it to the buffer, and displays it in the Streamlit container.
|
| 46 |
+
Strips ANSI escape sequences from the log message if present.
|
| 47 |
+
clear_logs():
|
| 48 |
+
Clears the log messages from the Streamlit container and the buffer.
|
| 49 |
+
"""
|
| 50 |
+
# Initialize a custom log handler with a Streamlit container for displaying logs
|
| 51 |
def __init__(self, container, maxlen:int=15, debug:bool=False):
|
| 52 |
+
#TODO: find the type for streamlit generic containers
|
| 53 |
super().__init__()
|
| 54 |
# Store the Streamlit container for log output
|
| 55 |
self.container = container
|
|
|
|
| 60 |
self.buffer = deque(maxlen=maxlen)
|
| 61 |
self._n = 0
|
| 62 |
|
| 63 |
+
def n_elems(self, verb:bool=False) -> str:
|
| 64 |
+
"""
|
| 65 |
+
Return a string with the number of elements seen and the number of elements in the buffer.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
verb (bool): If True, returns a verbose string. Defaults to False.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
str: A string representing the total number of elements seen and the number of elements in the buffer.
|
| 72 |
+
"""
|
| 73 |
''' return a string with num elements seen and num elements in buffer '''
|
| 74 |
if verb:
|
| 75 |
return f"total: {self._n}|| in buffer:{len(self.buffer)}"
|
|
|
|
| 84 |
if self.debug:
|
| 85 |
self.log_area.markdown(clean_msg)
|
| 86 |
|
| 87 |
+
def clear_logs(self) -> None:
|
| 88 |
+
"""
|
| 89 |
+
Clears the log area and buffer.
|
| 90 |
+
|
| 91 |
+
This method empties the log area to remove any previous logs and clears the buffer to reset the log storage.
|
| 92 |
+
"""
|
| 93 |
self.log_area.empty() # Clear previous logs
|
| 94 |
self.buffer.clear()
|
| 95 |
|
| 96 |
# Set up logging to capture all info level logs from the root logger
|
| 97 |
@st.cache_resource
|
| 98 |
+
def setup_logging(level:int=logging.INFO, buffer_len:int=15) -> StreamlitLogHandler:
|
| 99 |
+
"""
|
| 100 |
+
Set up logging for the application using Streamlit's container for log display.
|
| 101 |
+
|
| 102 |
+
Args:
|
| 103 |
+
level (int): The logging level (e.g., logging.INFO, logging.DEBUG). Default is logging.INFO.
|
| 104 |
+
buffer_len (int): The maximum number of log messages to display in the Streamlit container. Default is 15.
|
| 105 |
+
|
| 106 |
+
Returns:
|
| 107 |
+
StreamlitLogHandler: The handler that has been added to the root logger.
|
| 108 |
+
"""
|
| 109 |
root_logger = logging.getLogger() # Get the root logger
|
| 110 |
log_container = st.container() # Create a container within which we display logs
|
| 111 |
handler = StreamlitLogHandler(log_container, maxlen=buffer_len)
|
|
|
|
| 120 |
return handler
|
| 121 |
|
| 122 |
def parse_log_buffer(log_contents: deque) -> list:
|
| 123 |
+
"""
|
| 124 |
+
Convert log buffer to a list of dictionaries.
|
| 125 |
+
Args:
|
| 126 |
+
log_contents (deque): A deque containing log lines as strings.
|
| 127 |
+
Returns:
|
| 128 |
+
list: A list of dictionaries, each representing a parsed log entry with the following keys:
|
| 129 |
+
- 'timestamp' (datetime): The timestamp of the log entry.
|
| 130 |
+
- 'n' (str): The log entry number.
|
| 131 |
+
- 'level' (str): The log level (e.g., INFO, ERROR).
|
| 132 |
+
- 'module' (str): The name of the module.
|
| 133 |
+
- 'func' (str): The name of the function.
|
| 134 |
+
- 'message' (str): The log message.
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
j = 0
|
| 138 |
records = []
|
| 139 |
for line in log_contents:
|
|
|
|
| 163 |
continue
|
| 164 |
return records
|
| 165 |
|
| 166 |
+
def demo_log_callback() -> None:
|
| 167 |
'''function to demo adding log entries'''
|
| 168 |
logger = logging.getLogger(__name__)
|
| 169 |
logger.setLevel(logging.DEBUG)
|
|
|
|
| 184 |
|
| 185 |
c1, c2 = st.columns([1, 3])
|
| 186 |
with c1:
|
| 187 |
+
button = st.button("do something", on_click=demo_log_callback)
|
| 188 |
with c2:
|
| 189 |
st.info(f"Length of records: {len(records)}")
|
| 190 |
#tab = st.table(records)
|
call_models/whale_gallery.py
CHANGED
|
@@ -5,7 +5,13 @@ import whale_viewer as sw_wv
|
|
| 5 |
|
| 6 |
def render_whale_gallery(n_cols:int = 4):
|
| 7 |
"""
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
def format_whale_name(name):
|
| 11 |
return name.replace("_", " ").capitalize()
|
|
|
|
| 5 |
|
| 6 |
def render_whale_gallery(n_cols:int = 4):
|
| 7 |
"""
|
| 8 |
+
Renders a gallery of whale images + urls in a grid format using Streamlit.
|
| 9 |
+
|
| 10 |
+
Parameters:
|
| 11 |
+
n_cols (int): Number of columns in the grid. Default is 4.
|
| 12 |
+
|
| 13 |
+
The function formats whale names, creates a grid layout for images, and applies custom CSS styles
|
| 14 |
+
Each image is displayed with a caption and a link to a reference URL.
|
| 15 |
"""
|
| 16 |
def format_whale_name(name):
|
| 17 |
return name.replace("_", " ").capitalize()
|
call_models/whale_viewer.py
CHANGED
|
@@ -22,7 +22,7 @@ WHALE_CLASSES = [
|
|
| 22 |
"melon_headed_whale",
|
| 23 |
"minke_whale",
|
| 24 |
"pantropic_spotted_dolphin",
|
| 25 |
-
"pygmy_killer_whale",
|
| 26 |
"rough_toothed_dolphin",
|
| 27 |
"sei_whale",
|
| 28 |
"short_finned_pilot_whale",
|
|
@@ -102,7 +102,16 @@ df_whale_img_ref = pd.DataFrame(
|
|
| 102 |
}
|
| 103 |
).set_index("WHALE_CLASSES")
|
| 104 |
|
| 105 |
-
def format_whale_name(whale_class:str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
whale_name = whale_class.replace("_", " ").title()
|
| 107 |
return whale_name
|
| 108 |
|
|
|
|
| 22 |
"melon_headed_whale",
|
| 23 |
"minke_whale",
|
| 24 |
"pantropic_spotted_dolphin",
|
| 25 |
+
"pygmy_killer_whale",
|
| 26 |
"rough_toothed_dolphin",
|
| 27 |
"sei_whale",
|
| 28 |
"short_finned_pilot_whale",
|
|
|
|
| 102 |
}
|
| 103 |
).set_index("WHALE_CLASSES")
|
| 104 |
|
| 105 |
+
def format_whale_name(whale_class:str) -> str:
|
| 106 |
+
"""
|
| 107 |
+
Formats a whale class name by replacing underscores with spaces and capitalizing each word.
|
| 108 |
+
|
| 109 |
+
Args:
|
| 110 |
+
whale_class (str): The class name of the whale, with words separated by underscores.
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
str: The formatted whale name with spaces instead of underscores and each word capitalized.
|
| 114 |
+
"""
|
| 115 |
whale_name = whale_class.replace("_", " ").title()
|
| 116 |
return whale_name
|
| 117 |
|