alfraser commited on
Commit
a565e7e
·
1 Parent(s): 06b78a1

Added a utility to join lists of items as strings with comma/and separation

Browse files
Files changed (1) hide show
  1. src/common.py +19 -1
src/common.py CHANGED
@@ -1,14 +1,32 @@
1
  import os
2
  import streamlit as st
3
 
 
 
4
 
5
  data_dir = os.path.join(os.path.dirname(__file__), '..', 'data')
6
  img_dir = os.path.join(os.path.dirname(__file__), '..', 'img')
7
  config_dir = os.path.join(os.path.dirname(__file__), '..', 'config')
8
 
 
9
  def hf_api_token() -> str:
10
- #TODO: Need to consider how to make this more generic to look for a token elsewhere
 
 
11
  token = st.secrets['hf_token']
12
  if token is None:
13
  raise ValueError('No HF access token found in streamlit secrets')
14
  return token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import streamlit as st
3
 
4
+ from typing import List
5
+
6
 
7
  data_dir = os.path.join(os.path.dirname(__file__), '..', 'data')
8
  img_dir = os.path.join(os.path.dirname(__file__), '..', 'img')
9
  config_dir = os.path.join(os.path.dirname(__file__), '..', 'config')
10
 
11
+
12
  def hf_api_token() -> str:
13
+ """
14
+ Utility single access point to look up the hugging face access token.
15
+ """
16
  token = st.secrets['hf_token']
17
  if token is None:
18
  raise ValueError('No HF access token found in streamlit secrets')
19
  return token
20
+
21
+
22
+ def join_items_lowercased_comma_and(items: List[str]) -> str:
23
+ """
24
+ Utility to convert a list of items to lowercase strings, comma separated and ending with and
25
+ """
26
+ items = [str(i).lower().strip() for i in items]
27
+ string_count = len(items)
28
+ if string_count == 0:
29
+ return ""
30
+ if string_count == 1:
31
+ return items[0]
32
+ return f"{', '.join(items[:-1])} and {items[-1]}"