-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
223 lines (185 loc) Β· 7.92 KB
/
Copy pathapp.py
File metadata and controls
223 lines (185 loc) Β· 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Streamlit app for generating emoji mosaics from Slack workspace emojis."""
import argparse
import zipfile
from pathlib import Path
import streamlit as st
from mosaic import build_index, build_visual_grid
def main(zip_file: str, har_file: str, output_file: str = "emoji_grid.png"):
# Page config
st.set_page_config(
page_title="Emoji Mosaic Generator",
page_icon="π¨",
layout="wide",
)
st.title("π¨ Emoji Mosaic Generator")
st.markdown(
"""
Generate mosaics from Slack workspace emojis using various sorting and clustering methods.
"""
)
# Sidebar for configuration
st.sidebar.header("Configuration")
# Sort method selection
sort_method = st.sidebar.selectbox(
"Sort Method",
options=[
"date",
"color",
"brightness",
"name",
"creator",
"pattern",
"color-cluster",
"tsne",
"umap",
],
index=0,
help="Method for organizing emojis. t-SNE and UMAP create coordinate-based layouts.",
)
# Feature selection (only for tsne/umap)
features = None
if sort_method in ["tsne", "umap", "color-cluster"]:
st.sidebar.subheader("Embedding Features")
st.sidebar.markdown(
"Select which features to include in the dimensionality reduction:"
)
feature_options = {
"visual": st.sidebar.checkbox(
"Visual (color/appearance)", value=True, key="visual"
),
"name": st.sidebar.checkbox(
"Name (emoji name text)", value=True, key="name"
),
"creator": st.sidebar.checkbox(
"Creator (creator name)", value=True, key="creator"
),
"year": st.sidebar.checkbox(
"Year (creation year)", value=False, key="year"
),
"quarter": st.sidebar.checkbox(
"Quarter (creation quarter)", value=False, key="quarter"
),
}
features = [k for k, v in feature_options.items() if v]
if not features:
st.sidebar.warning("β οΈ At least one feature must be selected")
# Labels option (only for tsne/umap with categorical features)
show_labels = False
if sort_method in ["tsne", "umap"] and features:
if any(f in features for f in ["year", "quarter", "creator"]):
show_labels = st.sidebar.checkbox(
"Show Cluster Labels",
value=False,
help="Display labels for year, quarter, and/or creator clusters",
)
# Generate button
st.sidebar.markdown("---")
generate_button = st.sidebar.button(
"π¨ Generate Mosaic", type="primary", width="stretch"
)
# Main content area
if generate_button:
# Validate inputs
har_path = Path(har_file)
zip_path = Path(zip_file)
if not har_path.exists():
st.error(f"β HAR file not found: {har_file}")
elif not zip_path.exists():
st.error(f"β ZIP file not found: {zip_file}")
elif sort_method in ["tsne", "umap", "color-cluster"] and not features:
st.error("β At least one feature must be selected for this sort method")
else:
# Create progress container
progress_container = st.container()
with progress_container:
status_text = st.empty()
progress_bar = st.progress(0)
try:
# Step 1: Build index
status_text.text("Building emoji index...")
progress_bar.progress(0.1)
emoji_meta = build_index(str(har_path))
status_text.text(
f"β Loaded {len(emoji_meta)} emoji metadata entries"
)
progress_bar.progress(0.2)
# Step 2: Load ZIP
status_text.text("Loading emoji images...")
progress_bar.progress(0.3)
with zipfile.ZipFile(str(zip_path), "r") as zf:
zip_names = set(zf.namelist())
# Step 3: Generate mosaic
status_text.text(f"Generating {sort_method} mosaic...")
progress_bar.progress(0.4)
# Store progress callback in session state for emojis.py to use
if "streamlit_progress" not in st.session_state:
st.session_state.streamlit_progress = {}
st.session_state.streamlit_progress["callback"] = (
lambda p, msg: (
progress_bar.progress(0.4 + p * 0.5),
status_text.text(msg),
)
)
# Generate the grid
build_visual_grid(
emoji_meta,
zf,
zip_names,
output_file=output_file,
sort_method=sort_method,
features=features,
show_labels=show_labels,
)
# Clean up callback
st.session_state.streamlit_progress["callback"] = None
progress_bar.progress(1.0)
status_text.text(f"β Mosaic generated successfully!")
# Display the generated image
if Path(output_file).exists():
st.image(
output_file,
caption=f"Emoji Mosaic - {sort_method.upper()}"
+ (f" ({', '.join(features)})" if features else ""),
width="stretch",
)
# Download button
with open(output_file, "rb") as f:
st.download_button(
label="π₯ Download Image",
data=f,
file_name=output_file,
mime="image/png",
width="stretch",
)
except Exception as e:
st.error(f"β Error generating mosaic: {e}")
import traceback
with st.expander("View error details"):
st.code(traceback.format_exc())
else:
# Show instructions when no generation has occurred
st.info(
"""
π Configure your mosaic options in the sidebar and click **Generate Mosaic** to create your emoji grid.
**Sort Methods:**
- **Simple sorts**: date, name, creator, color, brightness, pattern
- **Grid layouts**: Emojis arranged in a square grid
- **Advanced clustering**: color-cluster, t-SNE, UMAP
- **Coordinate layouts**: Emojis positioned based on similarity
**Features** (for t-SNE/UMAP):
- **Visual**: Color histograms and appearance
- **Name**: Emoji name text patterns
- **Creator**: Who created the emoji
- **Year/Quarter**: When the emoji was created
"""
)
# Show sample image if one exists
if Path(output_file).exists():
st.markdown("### Last Generated Mosaic")
st.image(output_file, width="stretch")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--zip", required=True, help="Path to the emoji zip file")
parser.add_argument("--har", required=True, help="Path to the Slack HAR file")
args = parser.parse_args()
main(args.zip, args.har)