Spaces:
Sleeping
Sleeping
hanwang commited on
Commit ·
8acf7d6
1
Parent(s): 1ee3994
Clean up CLI, and added 3 tabs to streamlit for random, search, and analytics.
Browse files- main.py +56 -56
- met_api.py +6 -2
main.py
CHANGED
|
@@ -4,7 +4,7 @@ from met_api import search_for_images, get_objectsWithImages, get_images, depart
|
|
| 4 |
|
| 5 |
@st.cache_data
|
| 6 |
def cached_search_for_images(query):
|
| 7 |
-
return search_for_images(query, 2
|
| 8 |
|
| 9 |
@st.cache_data
|
| 10 |
def cache_objectsWithImages():
|
|
@@ -47,71 +47,71 @@ st.markdown(
|
|
| 47 |
unsafe_allow_html=True
|
| 48 |
)
|
| 49 |
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
config = {
|
| 61 |
-
"primaryImageSmall": st.column_config.ImageColumn(),
|
| 62 |
-
}
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
selected_index = event.selection.rows[0] # Get the index of the first selected row
|
| 68 |
-
selected_row_data = r.iloc[selected_index]
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
st.write(selected_row_data)
|
| 73 |
-
else:
|
| 74 |
-
st.info("Select a row in the table to see its details.")
|
| 75 |
-
|
| 76 |
-
with col1:
|
| 77 |
-
st.markdown(
|
| 78 |
-
"<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/ep/web-large/DP-29324-001.jpg'></div>",
|
| 79 |
-
unsafe_allow_html=True
|
| 80 |
-
)
|
| 81 |
-
with col2:
|
| 82 |
-
st.markdown(
|
| 83 |
-
"<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/ad/web-large/DP124705.jpg'></div>",
|
| 84 |
-
unsafe_allow_html=True
|
| 85 |
-
)
|
| 86 |
-
with col3:
|
| 87 |
-
st.markdown(
|
| 88 |
-
"<div class='img-box'><img src='https://images.metmuseum.org/CRDImages/gr/web-large/DP21847edited.jpg'></div>",
|
| 89 |
-
unsafe_allow_html=True
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
st.write("")
|
| 93 |
-
st.write("")
|
| 94 |
-
|
| 95 |
-
# Department analytic
|
| 96 |
-
st.write("")
|
| 97 |
-
st.subheader("Departments for your search")
|
| 98 |
-
|
| 99 |
-
query = st.text_input("Search term (for images only)", value="cats", key="dept_query")
|
| 100 |
-
max_ids = st.slider("How many results to analyze", 20, 400, 150, 10, key="dept_max")
|
| 101 |
-
|
| 102 |
-
if st.button("Run department analytic", key="dept_run"):
|
| 103 |
-
with st.spinner("Fetching and tallying departments…"):
|
| 104 |
-
rows = department_counts(q=query, max_ids=max_ids)
|
| 105 |
-
|
| 106 |
-
if not rows:
|
| 107 |
-
st.info("No results found (or the API call failed). Try another term.")
|
| 108 |
else:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
st.write("")
|
| 115 |
|
| 116 |
# Footer
|
| 117 |
st.markdown("""
|
|
|
|
| 4 |
|
| 5 |
@st.cache_data
|
| 6 |
def cached_search_for_images(query):
|
| 7 |
+
return search_for_images(query, 2)
|
| 8 |
|
| 9 |
@st.cache_data
|
| 10 |
def cache_objectsWithImages():
|
|
|
|
| 47 |
unsafe_allow_html=True
|
| 48 |
)
|
| 49 |
|
| 50 |
+
tab1, tab2, tab3 = st.tabs(["Random", "Search and Browse", "Dept Analytics"])
|
| 51 |
|
| 52 |
+
with tab1:
|
| 53 |
+
st.write("TBD")
|
| 54 |
+
total, ids = cache_objectsWithImages()
|
| 55 |
+
st.write(f"There are {total} images in the Met collection\nHere are 3 random ones for your enjoyment...")
|
| 56 |
|
| 57 |
+
imgs = cache_images(total, ids, 3)
|
| 58 |
|
| 59 |
+
col1, col2, col3 = st.columns(3)
|
| 60 |
|
| 61 |
+
with col1:
|
| 62 |
+
st.image(imgs[0][0])
|
| 63 |
+
st.write(imgs[0][1])
|
| 64 |
+
with col2:
|
| 65 |
+
st.image(imgs[1][0])
|
| 66 |
+
st.write(imgs[1][1])
|
| 67 |
+
with col3:
|
| 68 |
+
st.image(imgs[2][0])
|
| 69 |
+
st.write(imgs[2][1])
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
with tab2:
|
| 73 |
+
q = st.text_input("🔎 Search Met's Art Collection....",value="cats", key="search_query")
|
| 74 |
|
| 75 |
+
r = cached_search_for_images(q)
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
if r.empty:
|
| 78 |
+
st.write("No results found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
+
summary = r[["primaryImageSmall","title","department","objectName"]]
|
| 81 |
+
config = {
|
| 82 |
+
"primaryImageSmall": st.column_config.ImageColumn(),
|
| 83 |
+
}
|
| 84 |
+
event = st.dataframe(summary, column_config=config, use_container_width=True, on_select="rerun", selection_mode="single-row")
|
| 85 |
+
if event.selection.rows:
|
| 86 |
+
selected_index = event.selection.rows[0] # Get the index of the first selected row
|
| 87 |
+
selected_row_data = r.iloc[selected_index]
|
| 88 |
+
|
| 89 |
+
st.subheader("Details of Selected Piece:")
|
| 90 |
+
st.image(selected_row_data["primaryImage"], caption=selected_row_data["title"], width=500)
|
| 91 |
+
st.write(selected_row_data)
|
| 92 |
+
else:
|
| 93 |
+
st.info("Select a row in the table to see its details.")
|
| 94 |
+
|
| 95 |
+
with tab3:
|
| 96 |
+
# Department analytic
|
| 97 |
+
st.write("")
|
| 98 |
+
st.subheader("Departments for your search")
|
| 99 |
+
|
| 100 |
+
query = st.text_input("Search term (for images only)", value="cats", key="dept_query")
|
| 101 |
+
max_ids = st.slider("How many results to analyze", 20, 400, 150, 10, key="dept_max")
|
| 102 |
+
|
| 103 |
+
if st.button("Run department analytic", key="dept_run"):
|
| 104 |
+
with st.spinner("Fetching and tallying departments…"):
|
| 105 |
+
rows = department_counts(q=query, max_ids=max_ids)
|
| 106 |
+
|
| 107 |
+
if not rows:
|
| 108 |
+
st.info("No results found (or the API call failed). Try another term.")
|
| 109 |
+
else:
|
| 110 |
+
df = pd.DataFrame(rows, columns=["Department", "Count"])
|
| 111 |
+
# show a quick bar chart + table
|
| 112 |
+
st.bar_chart(df.set_index("Department"))
|
| 113 |
+
st.dataframe(df, use_container_width=True)
|
| 114 |
|
|
|
|
| 115 |
|
| 116 |
# Footer
|
| 117 |
st.markdown("""
|
met_api.py
CHANGED
|
@@ -103,7 +103,7 @@ def search_for_images(query,
|
|
| 103 |
#session = requests.Session()
|
| 104 |
|
| 105 |
print("Searching for", query)
|
| 106 |
-
print("
|
| 107 |
|
| 108 |
for dept_id in dept_ids:
|
| 109 |
# limit to only those with images and is highlighted
|
|
@@ -174,7 +174,11 @@ def main():
|
|
| 174 |
dept = None
|
| 175 |
else:
|
| 176 |
dept = [int(dept_no)]
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
if results.empty:
|
| 179 |
print("No results found.")
|
| 180 |
continue
|
|
|
|
| 103 |
#session = requests.Session()
|
| 104 |
|
| 105 |
print("Searching for", query)
|
| 106 |
+
print("Across departments:", dept_ids)
|
| 107 |
|
| 108 |
for dept_id in dept_ids:
|
| 109 |
# limit to only those with images and is highlighted
|
|
|
|
| 174 |
dept = None
|
| 175 |
else:
|
| 176 |
dept = [int(dept_no)]
|
| 177 |
+
query = input("Search the Met for: ").strip()
|
| 178 |
+
if query == '':
|
| 179 |
+
print("Please enter a query.")
|
| 180 |
+
continue
|
| 181 |
+
results = search_for_images(query,2, departments=dept)
|
| 182 |
if results.empty:
|
| 183 |
print("No results found.")
|
| 184 |
continue
|