Spaces:
Sleeping
Sleeping
Merge pull request #4 from MrinalGoel643/abby_api_updates
Browse filesadded api and display for analyzing department for specific artworks
- .gitignore +2 -1
- main.py +20 -2
- met_api.py +28 -0
- requirements.txt +1 -0
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
venv
|
|
|
|
|
|
| 1 |
+
venv
|
| 2 |
+
final_project
|
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from met_api import get_objectsWithImages, get_images
|
| 3 |
|
| 4 |
# Caching Section
|
|
@@ -10,8 +11,6 @@ def cache_objectsWithImages():
|
|
| 10 |
def cache_images(total, objectIDs, limit):
|
| 11 |
return get_images(total, objectIDs, limit)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
# Page setup
|
| 16 |
st.set_page_config(page_title="The METrics", layout="centered")
|
| 17 |
|
|
@@ -82,6 +81,25 @@ st.markdown("""
|
|
| 82 |
</div>
|
| 83 |
""", unsafe_allow_html=True)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
st.write("")
|
| 86 |
|
| 87 |
# Footer
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
from met_api import get_objectsWithImages, get_images
|
| 4 |
|
| 5 |
# Caching Section
|
|
|
|
| 11 |
def cache_images(total, objectIDs, limit):
|
| 12 |
return get_images(total, objectIDs, limit)
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# Page setup
|
| 15 |
st.set_page_config(page_title="The METrics", layout="centered")
|
| 16 |
|
|
|
|
| 81 |
</div>
|
| 82 |
""", unsafe_allow_html=True)
|
| 83 |
|
| 84 |
+
# Department analytic
|
| 85 |
+
st.write("")
|
| 86 |
+
st.subheader("Departments for your search")
|
| 87 |
+
|
| 88 |
+
query = st.text_input("Search term (for images only)", value="cats", key="dept_query")
|
| 89 |
+
max_ids = st.slider("How many results to analyze", 20, 400, 150, 10, key="dept_max")
|
| 90 |
+
|
| 91 |
+
if st.button("Run department analytic", key="dept_run"):
|
| 92 |
+
with st.spinner("Fetching and tallying departments…"):
|
| 93 |
+
rows = met_api.department_counts(q=query, max_ids=max_ids)
|
| 94 |
+
|
| 95 |
+
if not rows:
|
| 96 |
+
st.info("No results found (or the API call failed). Try another term.")
|
| 97 |
+
else:
|
| 98 |
+
df = pd.DataFrame(rows, columns=["Department", "Count"])
|
| 99 |
+
# show a quick bar chart + table
|
| 100 |
+
st.bar_chart(df.set_index("Department"))
|
| 101 |
+
st.dataframe(df, use_container_width=True)
|
| 102 |
+
|
| 103 |
st.write("")
|
| 104 |
|
| 105 |
# Footer
|
met_api.py
CHANGED
|
@@ -31,3 +31,31 @@ def get_images(totalObjects, objectIDs, limit):
|
|
| 31 |
break
|
| 32 |
|
| 33 |
return images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
break
|
| 32 |
|
| 33 |
return images
|
| 34 |
+
|
| 35 |
+
def department_counts(q="*", max_ids=200):
|
| 36 |
+
"""
|
| 37 |
+
Analytic: return a list of (department, count) for search results.
|
| 38 |
+
- q: search query (default '*' = anything)
|
| 39 |
+
- max_ids: cap how many object IDs to inspect (keeps it fast)
|
| 40 |
+
|
| 41 |
+
Uses the Met search endpoint (images only), then tallies the 'department'
|
| 42 |
+
field from each object's metadata.
|
| 43 |
+
"""
|
| 44 |
+
try:
|
| 45 |
+
resp = requests.get(f"{URL}search", params={"q": q, "hasImages": True}, timeout=15)
|
| 46 |
+
resp.raise_for_status()
|
| 47 |
+
ids = (resp.json().get("objectIDs") or [])[:max_ids]
|
| 48 |
+
except Exception:
|
| 49 |
+
return []
|
| 50 |
+
|
| 51 |
+
counts = {}
|
| 52 |
+
for oid in ids:
|
| 53 |
+
try:
|
| 54 |
+
obj = get_object(oid) # uses your existing helper
|
| 55 |
+
dep = obj.get("department") or "(unknown)"
|
| 56 |
+
counts[dep] = counts.get(dep, 0) + 1
|
| 57 |
+
except Exception:
|
| 58 |
+
continue
|
| 59 |
+
|
| 60 |
+
# return sorted (department, count) pairs, highest first
|
| 61 |
+
return sorted(counts.items(), key=lambda x: x[1], reverse=True)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
streamlit==1.48.1
|
|
|
|
| 2 |
requests==2.32.5
|
|
|
|
| 1 |
streamlit==1.48.1
|
| 2 |
+
pandas
|
| 3 |
requests==2.32.5
|