yourusername commited on
Commit
aeb47d6
β€’
1 Parent(s): 6496e5a

:children_crossing: improve UX

Browse files
Files changed (1) hide show
  1. app.py +67 -92
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import logging
2
  import shutil
3
- import time
4
  from concurrent.futures import ThreadPoolExecutor
5
  from pathlib import Path
6
  from tempfile import TemporaryDirectory
@@ -36,7 +36,7 @@ def download_image(img_url, filename):
36
  img_file.write(img_bytes)
37
 
38
 
39
- def make_huggingpics_imagefolder(data_dir, search_terms, count=150, overwrite=False, transform=None, resume=False):
40
 
41
  data_dir = Path(data_dir)
42
 
@@ -49,7 +49,8 @@ def make_huggingpics_imagefolder(data_dir, search_terms, count=150, overwrite=Fa
49
  if not resume:
50
  return
51
 
52
- pbar = st.progress(0)
 
53
 
54
  for search_term_idx, search_term in enumerate(search_terms):
55
  search_term_dir = data_dir / search_term
@@ -67,93 +68,39 @@ def make_huggingpics_imagefolder(data_dir, search_terms, count=150, overwrite=Fa
67
  for i, url in enumerate(tqdm(urls)):
68
  executor.submit(download_image, url, search_term_dir / f'{i}.jpg')
69
 
70
- pbar.progress((search_term_idx + 1) / len(search_terms))
 
71
 
72
- pbar.empty()
 
73
 
74
 
75
- def create_dataset(terms):
76
-
77
- msg_placeholder = st.empty()
78
-
79
- for term in terms:
80
- show_images_of_term(term)
81
-
82
- with st.sidebar:
83
- with st.form('Push to Hub'):
84
- dataset_name = st.text_input('Dataset Name', value='huggingpics-data')
85
- do_push = st.form_submit_button("Push to πŸ€— Hub")
86
-
87
- if do_push:
88
- msg_placeholder.empty()
89
- if not st.session_state.get('is_logged_in'):
90
- msg_placeholder.error("You must login to push to the hub.")
91
- return
92
- else:
93
- msg_placeholder.empty()
94
-
95
- with st.sidebar:
96
- repo_url = create_repo(dataset_name, st.session_state.token, exist_ok=True, repo_type='dataset')
97
- hf_username = whoami(st.session_state.token)['name']
98
- with TemporaryDirectory() as tmp_dir:
99
- repo_owner, repo_name = hf_username, dataset_name
100
- repo_namespace = f"{repo_owner}/{repo_name}"
101
- repo_url = f'https://huggingface.co/datasets/{repo_namespace}'
102
-
103
- repo = Repository(
104
- tmp_dir,
105
- clone_from=repo_url,
106
- use_auth_token=st.session_state.token,
107
- git_user=hf_username,
108
- git_email=f'{hf_username}@users.noreply.huggingface.co',
109
- repo_type='dataset',
110
- )
111
-
112
- with st.spinner(f"Uploading files to [{repo_namespace}]({repo_url})..."):
113
- with repo.commit("Uploaded from HuggingPics Explorer"):
114
- make_huggingpics_imagefolder(Path(tmp_dir) / 'images', terms, count=150)
115
-
116
- st.success(f"View your dataset here πŸ‘‰ [{repo_namespace}]({repo_url})")
117
-
118
-
119
- def huggingface_auth_form():
120
- placeholder = st.empty()
121
-
122
- is_logged_in = st.session_state.get('is_logged_in', False)
123
-
124
- if is_logged_in:
125
- token = st.session_state.token
126
- with placeholder.container():
127
- st.markdown(f"βœ… Logged in as {whoami(token)['name']}")
128
- do_logout = st.button("Logout")
129
- if do_logout:
130
- st.session_state.token = None
131
- st.session_state.is_logged_in = False
132
- placeholder.empty()
133
- huggingface_auth_form()
134
- else:
135
- with placeholder.container():
136
- username = st.text_input('Username', value=st.session_state.get('username', ''))
137
- password = st.text_input('Password', value="", type="password")
138
- submit = st.button('Login')
139
- if submit:
140
- try:
141
- st.session_state.token = login(username, password)
142
- st.session_state.is_logged_in = True
143
- placeholder.empty()
144
- huggingface_auth_form()
145
- except HTTPError as e:
146
- st.session_state.token = None
147
- st.session_state.is_logged_in = False
148
- st.error("Invalid username or password.")
149
- time.sleep(2)
150
- # huggingface_auth_form() # ???
151
 
152
 
153
  def main():
154
 
155
  with st.sidebar:
156
- st.sidebar.title("πŸ€—πŸ–Ό HuggingPics Explorer")
157
  st.markdown(
158
  """
159
  <p align="center">
@@ -163,18 +110,46 @@ def main():
163
  unsafe_allow_html=True,
164
  )
165
 
166
- term_1 = st.sidebar.text_input('Search Term 1', value='shiba inu')
167
- term_2 = st.sidebar.text_input('Search Term 2', value='husky')
168
- term_3 = st.sidebar.text_input('Search Term 3', value='')
169
- term_4 = st.sidebar.text_input('Search Term 4', value='')
170
- term_5 = st.sidebar.text_input('Search Term 5', value='')
171
- terms = [t for t in [term_1, term_2, term_3, term_4, term_5] if t]
172
 
173
- st.markdown('---')
174
- huggingface_auth_form()
175
- st.markdown('---')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
- _ = create_dataset(terms)
 
 
178
 
179
 
180
  if __name__ == '__main__':
 
1
  import logging
2
  import shutil
3
+ import zipfile
4
  from concurrent.futures import ThreadPoolExecutor
5
  from pathlib import Path
6
  from tempfile import TemporaryDirectory
 
36
  img_file.write(img_bytes)
37
 
38
 
39
+ def make_huggingpics_imagefolder(data_dir, search_terms, count=150, overwrite=False, resume=False, streamlit=False):
40
 
41
  data_dir = Path(data_dir)
42
 
 
49
  if not resume:
50
  return
51
 
52
+ if streamlit:
53
+ pbar = st.progress(0)
54
 
55
  for search_term_idx, search_term in enumerate(search_terms):
56
  search_term_dir = data_dir / search_term
 
68
  for i, url in enumerate(tqdm(urls)):
69
  executor.submit(download_image, url, search_term_dir / f'{i}.jpg')
70
 
71
+ if streamlit:
72
+ pbar.progress((search_term_idx + 1) / len(search_terms))
73
 
74
+ if streamlit:
75
+ pbar.empty()
76
 
77
 
78
+ def zip_imagefolder(data_dir, zip_path='images.zip'):
79
+ data_dir = Path(data_dir)
80
+ zip_file = zipfile.ZipFile(zip_path, 'w')
81
+ for img_path in data_dir.glob('**/*.jpg'):
82
+ zip_file.write(img_path, arcname=f"{img_path.parent.name}/{img_path.name}")
83
+ zip_file.close()
84
+
85
+
86
+ def get_search_terms():
87
+ terms = [
88
+ st.sidebar.text_input("Term 1:"),
89
+ ]
90
+ while terms[-1] != "":
91
+ terms.append(
92
+ st.sidebar.text_input(
93
+ f"Term {len(terms) + 1}:",
94
+ )
95
+ )
96
+ terms = terms[:-1]
97
+ return terms
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
 
100
  def main():
101
 
102
  with st.sidebar:
103
+ st.title('πŸ€—πŸ–Ό HuggingPics Explorer')
104
  st.markdown(
105
  """
106
  <p align="center">
 
110
  unsafe_allow_html=True,
111
  )
112
 
113
+ names = get_search_terms()
114
+ for name in names:
115
+ show_images_of_term(name)
 
 
 
116
 
117
+ with st.sidebar:
118
+ with st.form("Upload to πŸ€— Hub"):
119
+ username = st.text_input('Username')
120
+ password = st.text_input('Password', type="password")
121
+ dataset_name = st.text_input('Dataset Name', value='huggingpics-data')
122
+ submit = st.form_submit_button('Upload to πŸ€— Hub')
123
+ if submit:
124
+ try:
125
+ token = login(username, password)
126
+ repo_url = create_repo(dataset_name, token, exist_ok=True, repo_type='dataset')
127
+ with TemporaryDirectory() as tmp_dir:
128
+ repo_owner, repo_name = username, dataset_name
129
+ repo_namespace = f"{repo_owner}/{repo_name}"
130
+
131
+ repo = Repository(
132
+ tmp_dir,
133
+ clone_from=repo_url,
134
+ use_auth_token=token,
135
+ git_user=username,
136
+ git_email=f'{username}@users.noreply.huggingface.co',
137
+ repo_type='dataset',
138
+ )
139
+ temp_path = Path(tmp_dir)
140
+ imagefolder_path = temp_path / 'images/'
141
+ zipfile_path = temp_path / 'images.zip'
142
+ with st.spinner(f"Uploading files to [{repo_namespace}]({repo_url})..."):
143
+
144
+ with repo.commit("Uploaded from HuggingPics Explorer"):
145
+ make_huggingpics_imagefolder(
146
+ imagefolder_path, names, count=20, overwrite=True, resume=False, streamlit=True
147
+ )
148
+ zip_imagefolder(imagefolder_path, zipfile_path)
149
 
150
+ st.success(f"View your dataset here πŸ‘‰ [{repo_namespace}]({repo_url})")
151
+ except HTTPError as e:
152
+ st.error("Invalid username or password.")
153
 
154
 
155
  if __name__ == '__main__':