Spaces:
Running
Running
File size: 20,436 Bytes
e004315 d707455 e004315 c39747a d707455 91c3d7f d707455 e004315 58fb3b6 08b2694 c39747a 3c67c1b c39747a 4e07134 db6dfb3 4e07134 3c67c1b c39747a 8957ac0 5a18d45 8957ac0 c39747a 3c67c1b 6080190 3c67c1b d6529a6 3c67c1b 8957ac0 2820156 c39747a 8957ac0 3c67c1b 8957ac0 3c67c1b c39747a 3c67c1b c39747a 3c67c1b c39747a e9e9f4c c39747a e9e9f4c 2820156 13513ba c39747a 3c67c1b 08b2694 e004315 58fb3b6 12da302 c39747a 3c67c1b c3039ab c39747a d707455 58fb3b6 083533c b948611 083533c c39747a 3c67c1b bd9c01d b948611 3c67c1b bc2c7d0 1ce0089 3c67c1b c39747a 3c67c1b 39d75ee 3c67c1b c39747a 3c67c1b e004315 3c67c1b e9e9f4c 3c67c1b e004315 9e7da33 e004315 9e7da33 e004315 e9e9f4c e004315 4d41b2b ce53185 6125674 d0a2375 62dd932 d0a2375 62dd932 d0a2375 795eab8 c3bd8c4 6d62724 fc8f7bb e004315 cb7e6f4 12bf69a cb7e6f4 58fb3b6 cb7e6f4 58fb3b6 cb7e6f4 12bf69a 58fb3b6 20087cc e9e9f4c 3c67c1b 20d01cc 791aa72 e004315 791aa72 e004315 791aa72 3c67c1b e004315 791aa72 e004315 31632ae e004315 791aa72 31632ae 791aa72 3c67c1b e004315 13513ba e004315 3c67c1b 52fe3c5 1b02b65 13513ba e004315 1b02b65 e004315 864044e e004315 864044e e004315 864044e 1b02b65 864044e 8f294a5 864044e 8f294a5 3c67c1b 8f294a5 e004315 8f294a5 1b02b65 8f294a5 864044e |
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
import os
import requests
import pandas as pd
import streamlit as st
import time
import matplotlib
import plotly.express as px
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
def is_missing(value):
return pd.isna(value) or str(value).strip() == ""
# Load the Hugging Face API key from environment
api_key = os.getenv('HF_API')
def get_huggingface_suggestions(title, description):
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
headers = {"Authorization": f"Bearer {api_key}"}
full_text = f"{title}. {description}".strip()
if not full_text:
return None
candidate_labels = [
"History", "Politics", "Science", "Technology", "Art", "Literature",
"Education", "Economics", "Military", "Geography", "Sociology",
"Philosophy", "Religion", "Law", "Medicine", "Engineering",
"Mathematics", "Computer Science", "Agriculture", "Environment",
"Maps", "United States", "Civil War", "Revolution", "Posters", "Women's Rights", "World War I"
]
payload = {
"inputs": full_text,
"parameters": {
"candidate_labels": candidate_labels,
"multi_label": True
}
}
try:
response = requests.post(API_URL, headers=headers, json=payload)
result = response.json()
if "error" in result:
st.error(f"API error: {result['error']}")
return None
labels = [
label for label, score in zip(result.get("labels", []), result.get("scores", []))
if score > 0.3
]
return ", ".join(labels) if labels else None
except Exception as e:
st.error(f"API Error: {e}")
return None
# Custom CSS
st.markdown("""
<style>
.main {
background-color: #1A1A1A !important; /* dark */
color: #D3D3D3 !important;
}
}
.block-container {
background-color: #D3D3D3 !important;
color: #cccccc !important;
padding-left: 3rem !important;
padding-right: 3rem !important;
max-width: 900px; /* widen main feed */
margin: auto; /* center it */
}
/* Headings */
h1, h2, h3, h4 {
color: #eeeeee !important; /* brighter light gray for headings */
font-weight: 700 !important; /* bold */
margin-bottom: 1rem !important;
}
p, span, div {
color: #cccccc !important;
}
/* Subheaders (optional) */
.stSubheader {
color: #dddddd !important;
font-size: 1.4rem !important;
}
/* Dataframes (optional tweak) */
.stDataFrame {
background-color: #2e2e2e !important;
border-radius: 10px;
padding: 1rem;
}
section[data-testid="stSidebar"] > div:first-child {
background-color: #808080 !important;
padding: 1rem;
border-radius: 0.5rem;
color: #808080 !important;
}
.stMarkdown, .stTextInput, .stDataFrame {
color: #1A1A1A!important;
}
img.banner {
width: 100%;
border-radius: 12px;
margin-bottom: 1rem;
}
.stAlert {
background-color: #f0f0f5 !important;
color: #1A1A1A !important;
padding: 1.25rem !important;
font-size: 1rem !important;
border-radius: 0.5rem !important;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05) !important;
}
header[data-testid="stHeader"] {
background-color: #1A1A1A !important;
}
section[data-testid="stSidebar"] > div:first-child {
background-color: #1A1A1A !important;
color: #FFFFFF !important;
padding: 2rem 1.5rem 1.5rem 1.5rem !important;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
font-size: 0.95rem;
line-height: 1.5;
}
;
html, body, [data-testid="stApp"] {
background-color: #1A1A1A !important;
}
.custom-table {
background-color: #D3D3D3;
color: #1A1A1A;
font-family: monospace;
padding: 1rem;
border-radius: 8px;
overflow-x: auto;
white-space: pre;
border: 1px solid #ccc;
}
.sidebar-stats {
color: lightgray !important;
font-size: 1.1rem !important;
margin-top: 1.5rem;
font-weight: 600;
}
.sidebar-contrast-block {
background-color: #2b2b2b !important;
padding: 1.25rem;
border-radius: 10px;
margin-top: 1.5rem;
}
section.main > div { /* widen main container */
max-width: 95%;
padding-left: 3rem;
padding-right: 3rem;
}
</style>
""", unsafe_allow_html=True)
# Function to get subject suggestions using Hugging Face API
def get_huggingface_suggestions(title, description):
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-mnli"
# Rest of the function code...
# Use an image from a URL for the banner
st.image("https://cdn-uploads.huggingface.co/production/uploads/67351c643fe51cb1aa28f2e5/7ThcAOjbuM8ajrP85bGs4.jpeg", use_container_width=True)
# Streamlit app header
st.title("MetaDiscovery Agent for Library of Congress Collections")
st.markdown("""
This tool connects to the LOC API, retrieves metadata from a selected collection, and performs
an analysis of metadata completeness, suggests enhancements, and identifies authority gaps.
""")
# Updated collection URLs using the correct LOC API
collections = {
"American Revolutionary War Maps": "american+revolutionary+war+maps",
"Civil War Maps": "civil+war+maps",
"Women's Suffrage": "women+suffrage",
"World War I Posters": "world+war+posters"
}
# Sidebar for selecting collection
#st.sidebar.markdown("## Settings")
# Create empty metadata_df variable to ensure it exists before checking
metadata_df = pd.DataFrame()
# Add a key to the selectbox to ensure it refreshes properly
with st.sidebar:
st.markdown("""
<div style='
background-color: #2b2b2b
padding: 1.5rem;
border-radius: 12px;
margin-bottom: 1.5rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
'>
""", unsafe_allow_html=True)
selected = st.radio("Select a Collection", list(collections.keys()), key="collection_selector")
st.markdown("</div>", unsafe_allow_html=True)
search_query = collections[selected]
# Define the collection URL
collection_url = f"https://www.loc.gov/search/?q={search_query}&fo=json"
# Create an empty placeholder for Quick Stats
stats_placeholder = st.sidebar.empty()
# Add a fetch button to make the action explicit
fetch_data = True
if fetch_data:
# Display a loading spinner while fetching data
with st.spinner(f"Fetching data for {selected}..."):
# Fetch data from LOC API with spoofed User-Agent header
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/110.0.0.0 Safari/537.36"
}
try:
response = requests.get(collection_url, headers=headers)
response.raise_for_status()
data = response.json()
if "results" in data:
records = data.get("results", [])
elif "items" in data:
records = data.get("items", [])
else:
records = []
st.error("Unexpected API response structure. No records found.")
st.write(f"Retrieved {len(records)} records")
except requests.exceptions.RequestException as e:
st.error(f"API Connection Error: {e}")
records = []
except ValueError:
st.error("Failed to parse API response as JSON")
records = []
# Extract selected metadata fields
items = []
for record in records:
if isinstance(record, dict):
description = record.get("description", "")
if isinstance(description, list):
description = " ".join([str(d) for d in description])
item = {
"id": record.get("id", ""),
"title": record.get("title", ""),
"date": record.get("date", ""),
"subject": ", ".join(record.get("subject", [])) if isinstance(record.get("subject"), list) else record.get("subject", ""),
"creator": record.get("creator", ""),
"description": description
}
if not item["title"] and "item" in record:
item["title"] = record.get("item", {}).get("title", "")
if not item["date"] and "item" in record:
item["date"] = record.get("item", {}).get("date", "")
items.append(item)
metadata_df = pd.DataFrame(items)
# Missing field detection
fields_to_check = ["subject", "creator", "date", "title", "description"]
missing_counts = {}
for field in fields_to_check:
if field in metadata_df.columns:
missing = metadata_df[field].apply(is_missing)
missing_counts[field] = missing.sum()
# Define custom completeness check
def is_incomplete(value):
return pd.isna(value) or value in ["", "N/A", "null", None]
if not metadata_df.empty:
# --- Unified Completeness and Missing Fields Analysis ---
#Define incompleteness at the cell level
is_incomplete = lambda value: pd.isna(value) or value in ["", "N/A", "null", None]
#Create a mask for missing values
missing_mask = metadata_df.map(is_incomplete)
#Compute overall record-level completeness
incomplete_count = missing_mask.any(axis=1).sum()
total_fields = metadata_df.size
filled_fields = (~missing_mask).sum().sum()
overall_percent = (filled_fields / total_fields) * 100
#Field-specific missing counts (for Missing Metadata Summary)
missing_counts = missing_mask.sum().sort_values(ascending=False)
missing_df = (
pd.DataFrame(missing_counts)
.reset_index()
.rename(columns={"index": "Field", 0: "Missing Count"})
)
# Field-level completeness
completeness = (~metadata_df.map(is_incomplete)).mean() * 100
completeness_df = pd.DataFrame({"Field": completeness.index, "Completeness (%)": completeness.values})
completeness_table = completeness_df.set_index("Field")
# Sidebar Quick Stats
quick_stats = pd.DataFrame({
"Metric": ["Total Records", "Incomplete Records", "Percent Complete"],
"Value": [len(metadata_df), incomplete_count, round(overall_percent, 1)]
})
styled_quick_stats = (
quick_stats.style
.hide(axis="index")
.background_gradient(cmap="Oranges", subset=["Value"])
.format({"Value": "{:.1f}"})
)
# Add an expander and put the dataframe inside it
with st.sidebar.expander("Quick Stats", expanded=True):
st.dataframe(
styled_quick_stats,
use_container_width=True,
hide_index=True
)
# Sidebar: Metadata Missing Stats
missing_df = (
pd.DataFrame(list(missing_counts.items()), columns=["Field", "Missing Count"])
.sort_values(by="Missing Count", ascending=False)
.reset_index(drop=True)
)
styled_missing_df = (
missing_df.style
.background_gradient(cmap="Blues", subset=["Missing Count"])
.hide(axis="index")
)
with st.sidebar.expander("🧹 Missing Metadata Summary", expanded=True):
st.dataframe(
styled_missing_df,
use_container_width=True,
hide_index=True, # <<< ADD THIS
height=min(300, len(missing_df) * 35 + 38)
)
# Calculate Top 10 Subjects
if 'subject' in metadata_df.columns:
top_subjects = (
metadata_df['subject']
.dropna()
.str.split(',')
.explode()
.str.strip()
.value_counts()
.head(10)
.to_frame(name="Count")
)
#Most Common Subjects in Sidebar
with st.sidebar.expander("Top 10 Most Common Subjects", expanded=True):
st.dataframe(
top_subjects.style.background_gradient(cmap="Greens").format("{:.0f}"),
use_container_width=True,
height=240
)
with st.sidebar.expander("Helpful Resources", expanded=False):
st.markdown("""
<style>
.sidebar-links a {
color: lightgray !important;
text-decoration: none !important;
}
.sidebar-links a:hover {
text-decoration: underline !important;
}
</style>
<div class="sidebar-links">
<ul style='padding-left: 1em'>
<li><a href="https://www.loc.gov/apis/" target="_blank">LOC API Info</a></li>
<li><a href="https://www.loc.gov/" target="_blank">Library of Congress Homepage</a></li>
<li><a href="https://www.loc.gov/collections/" target="_blank">LOC Digital Collections</a></li>
<li><a href="https://www.loc.gov/marc/" target="_blank">MARC Metadata Standards</a></li>
<li><a href="https://labs.loc.gov/about-labs/digital-strategy/" target="_blank">LOC Digital Strategy</a></li>
</ul>
</div>
""", unsafe_allow_html=True)
# Utility functions for deeper metadata quality analysis
def is_incomplete(value):
return pd.isna(value) or value in ["", "N/A", "null", None]
def is_valid_date(value):
try:
pd.to_datetime(value)
return True
except:
return False
if not metadata_df.empty:
st.subheader("Retrieved Metadata Sample")
st.dataframe(metadata_df.head())
st.subheader("Field Completeness Breakdown")
#DARK box for the Field Completeness Breakdown (MATCH others!)
st.markdown("""
<div style='
background-color: #2e2e2e;
padding: 1.5rem;
border-radius: 10px;
margin-top: 1.5rem;
color: lightgray;
'>
""", unsafe_allow_html=True)
#Dataframe inside the dark box
st.dataframe(
completeness_table.style
.background_gradient(cmap="Greens")
.format("{:.0f}%")
.hide(axis="index"),
use_container_width=True,
height=240
)
st.markdown("</div>", unsafe_allow_html=True)
# Identify incomplete records
incomplete_mask = metadata_df.map(is_incomplete).any(axis=1)
incomplete_records = metadata_df[incomplete_mask]
# --- Suggested Metadata Enhancements Section ---
st.subheader("Suggested Metadata Enhancements")
# Create a row with checkbox for AI suggestions - with proper label
use_ai = st.checkbox("Use AI Suggestions", value=True, label_visibility="hidden")
st.markdown("🤖 Use AI Suggestions (Hugging Face)")
# Check if records exist
incomplete_with_desc = metadata_df[
(metadata_df['description'].notnull() | metadata_df['title'].notnull()) &
(metadata_df['subject'].isnull())
]
if not incomplete_with_desc.empty:
if use_ai:
suggestions = []
records_to_process = min(10, len(incomplete_with_desc))
progress = st.progress(0)
status = st.empty()
for i, (idx, row) in enumerate(incomplete_with_desc.iterrows()):
if i >= records_to_process:
break
title = row['title'] if pd.notna(row['title']) else ""
description = row['description'] if pd.notna(row['description']) else ""
status.text(f"Analyzing {i+1}/{records_to_process}: {title[:30]}...")
suggested_subject = get_huggingface_suggestions(title, description)
if suggested_subject:
suggestions.append((title, suggested_subject))
progress.progress((i + 1) / records_to_process)
status.empty()
progress.empty()
if suggestions:
suggestions_df = pd.DataFrame(suggestions, columns=["Title", "Suggested Subject"])
# Create a custom dark-styled HTML table instead
html_table = """
<div style="background-color: #1e1e1e; padding: 1.5rem; border-radius: 10px; margin-top: 1rem;">
<table style="width: 100%; border-collapse: collapse; color: #e0e0e0;">
<thead>
<tr style="border-bottom: 1px solid #444;">
<th style="padding: 12px; text-align: left; color: #e0e0e0;">Title</th>
<th style="padding: 12px; text-align: left; color: #e0e0e0;">Suggested Subject</th>
</tr>
</thead>
<tbody>
"""
for _, row in suggestions_df.iterrows():
title = row['Title']
title_display = title[:50] + "..." if len(title) > 50 else title
subject = row['Suggested Subject']
# Calculate a shade of green based on confidence or some other metric
# For demonstration, using a fixed green shade
green_shade = "rgba(0, 100, 0, 0.3)"
html_table += f"""
<tr style="border-bottom: 1px solid #444;">
<td style="padding: 12px; text-align: left;">{title_display}</td>
<td style="padding: 12px; text-align: left; background-color: {green_shade};">{subject}</td>
</tr>
"""
html_table += """
</tbody>
</table>
</div>
"""
st.markdown(html_table, unsafe_allow_html=True)
else:
st.markdown("""
<div style="background-color: #1e1e1e; padding: 1.5rem; border-radius: 10px; margin-top: 1rem; color: #e0e0e0;">
No metadata enhancement suggestions available.
</div>
""", unsafe_allow_html=True)
else:
st.markdown("""
<div style="background-color: #1e1e1e; padding: 1.5rem; border-radius: 10px; margin-top: 1rem; color: #e0e0e0;">
Enable AI Suggestions to view recommendations.
</div>
""", unsafe_allow_html=True)
else:
st.markdown("""
<div style="background-color: #1e1e1e; padding: 1.5rem; border-radius: 10px; margin-top: 1rem; color: #e0e0e0;">
All records already have subjects or no usable text available.
</div>
""", unsafe_allow_html=True)
|