Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,92 +5,197 @@ from PIL import Image
|
|
| 5 |
import io
|
| 6 |
|
| 7 |
# API 기본 URL
|
| 8 |
-
BASE_URL = "https://
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
params = {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
-
if
|
| 20 |
-
params[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return "검색 결과가 없습니다."
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if artwork.get("image_id"):
|
| 31 |
-
# IIIF 이미지 URL 생성
|
| 32 |
-
image_url = f"https://www.artic.edu/iiif/2/{artwork['image_id']}/full/843,/0/default.jpg"
|
| 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 |
-
with gr.
|
| 72 |
-
with gr.
|
| 73 |
-
search_input = gr.Textbox(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
)
|
| 84 |
|
| 85 |
-
with gr.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
demo.launch()
|
|
|
|
| 5 |
import io
|
| 6 |
|
| 7 |
# API 기본 URL
|
| 8 |
+
BASE_URL = "https://collectionapi.metmuseum.org/public/collection/v1"
|
| 9 |
|
| 10 |
+
def get_departments():
|
| 11 |
+
"""Get all departments from the Met API"""
|
| 12 |
+
response = requests.get(f"{BASE_URL}/departments")
|
| 13 |
+
return response.json()['departments']
|
| 14 |
+
|
| 15 |
+
def search_artworks(query, department_id=None, is_highlight=False, has_images=True,
|
| 16 |
+
is_on_view=False, medium=None, geo_location=None):
|
| 17 |
+
"""Search artworks with various filters"""
|
| 18 |
+
search_url = f"{BASE_URL}/search"
|
| 19 |
params = {
|
| 20 |
+
'q': query,
|
| 21 |
+
'hasImages': has_images,
|
| 22 |
+
'isHighlight': is_highlight,
|
| 23 |
+
'isOnView': is_on_view
|
| 24 |
}
|
| 25 |
|
| 26 |
+
if department_id:
|
| 27 |
+
params['departmentId'] = department_id
|
| 28 |
+
if medium:
|
| 29 |
+
params['medium'] = medium
|
| 30 |
+
if geo_location:
|
| 31 |
+
params['geoLocation'] = geo_location
|
| 32 |
|
| 33 |
+
try:
|
| 34 |
+
# Get object IDs from search
|
| 35 |
+
response = requests.get(search_url, params=params)
|
| 36 |
+
results = response.json()
|
|
|
|
| 37 |
|
| 38 |
+
if not results.get('objectIDs'):
|
| 39 |
+
return [], "No results found."
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Limit to first 12 objects for performance
|
| 42 |
+
object_ids = results['objectIDs'][:12]
|
| 43 |
+
|
| 44 |
+
images = []
|
| 45 |
+
captions = []
|
| 46 |
+
|
| 47 |
+
# Get details for each object
|
| 48 |
+
for object_id in object_ids:
|
| 49 |
+
object_url = f"{BASE_URL}/objects/{object_id}"
|
| 50 |
+
object_response = requests.get(object_url)
|
| 51 |
+
artwork = object_response.json()
|
| 52 |
|
| 53 |
+
if artwork.get('primaryImage'):
|
| 54 |
+
try:
|
| 55 |
+
img_response = requests.get(artwork['primaryImage'], timeout=10)
|
| 56 |
+
img = Image.open(io.BytesIO(img_response.content))
|
| 57 |
+
|
| 58 |
+
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
| 59 |
+
img = img.convert('RGB')
|
| 60 |
+
|
| 61 |
+
artwork_info = f"""
|
| 62 |
+
Title: {artwork.get('title', 'Unknown')}
|
| 63 |
+
Artist: {artwork.get('artistDisplayName', 'Unknown')}
|
| 64 |
+
Date: {artwork.get('objectDate', 'Unknown')}
|
| 65 |
+
Medium: {artwork.get('medium', 'Unknown')}
|
| 66 |
+
Department: {artwork.get('department', 'Unknown')}
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
images.append(img)
|
| 70 |
+
captions.append(artwork_info)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error processing image for object {object_id}: {e}")
|
| 73 |
+
continue
|
| 74 |
|
| 75 |
+
return images, "\n\n".join(captions)
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return [], f"An error occurred: {str(e)}"
|
| 79 |
+
|
| 80 |
+
# Custom CSS
|
| 81 |
+
custom_css = """
|
| 82 |
+
.gradio-container {
|
| 83 |
+
background: linear-gradient(135deg, #1a1a1a, #2d2d2d) !important;
|
| 84 |
+
color: #ffffff !important;
|
| 85 |
+
}
|
| 86 |
+
.gr-button {
|
| 87 |
+
background: linear-gradient(135deg, #8e2de2, #4a00e0) !important;
|
| 88 |
+
border: none !important;
|
| 89 |
+
color: white !important;
|
| 90 |
+
font-weight: bold !important;
|
| 91 |
+
padding: 10px 20px !important;
|
| 92 |
+
font-size: 1.1em !important;
|
| 93 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.2) !important;
|
| 94 |
+
}
|
| 95 |
+
.gr-button:hover {
|
| 96 |
+
transform: translateY(-2px);
|
| 97 |
+
box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
|
| 98 |
+
transition: all 0.3s ease;
|
| 99 |
+
}
|
| 100 |
+
.gr-input, .gr-select {
|
| 101 |
+
border: 2px solid #4a00e0 !important;
|
| 102 |
+
background: rgba(255, 255, 255, 0.1) !important;
|
| 103 |
+
color: white !important;
|
| 104 |
+
font-size: 1.1em !important;
|
| 105 |
+
border-radius: 8px !important;
|
| 106 |
+
}
|
| 107 |
+
.gr-gallery {
|
| 108 |
+
background: rgba(0, 0, 0, 0.3) !important;
|
| 109 |
+
border-radius: 15px !important;
|
| 110 |
+
padding: 20px !important;
|
| 111 |
+
min-height: 800px !important;
|
| 112 |
+
box-shadow: 0 8px 32px rgba(0,0,0,0.3) !important;
|
| 113 |
+
}
|
| 114 |
+
.title-text {
|
| 115 |
+
text-align: center !important;
|
| 116 |
+
color: #ffffff !important;
|
| 117 |
+
font-size: 2.8em !important;
|
| 118 |
+
margin-bottom: 0.3em !important;
|
| 119 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.5) !important;
|
| 120 |
+
font-weight: bold !important;
|
| 121 |
+
}
|
| 122 |
+
.subtitle-text {
|
| 123 |
+
text-align: center !important;
|
| 124 |
+
color: #cccccc !important;
|
| 125 |
+
font-size: 1.3em !important;
|
| 126 |
+
margin-bottom: 2em !important;
|
| 127 |
+
font-style: italic !important;
|
| 128 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.3) !important;
|
| 129 |
+
}
|
| 130 |
+
"""
|
| 131 |
|
| 132 |
+
# Get departments for dropdown
|
| 133 |
+
departments = get_departments()
|
| 134 |
+
department_choices = {dept['displayName']: dept['departmentId'] for dept in departments}
|
| 135 |
+
|
| 136 |
+
# Gradio interface
|
| 137 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 138 |
+
gr.HTML(
|
| 139 |
+
"""
|
| 140 |
+
<div class="title-text">🎨 The Met Art Explorer</div>
|
| 141 |
+
<div class="subtitle-text">Explore over 5,000 years of art from The Metropolitan Museum of Art's collection</div>
|
| 142 |
+
"""
|
| 143 |
+
)
|
| 144 |
|
| 145 |
+
with gr.Row():
|
| 146 |
+
with gr.Column(scale=3):
|
| 147 |
+
search_input = gr.Textbox(
|
| 148 |
+
label="Search artwork",
|
| 149 |
+
placeholder="Enter keywords (e.g., sunflowers, portrait, landscape...)"
|
| 150 |
+
)
|
| 151 |
+
with gr.Column(scale=2):
|
| 152 |
+
department_dropdown = gr.Dropdown(
|
| 153 |
+
choices=list(department_choices.keys()),
|
| 154 |
+
label="Department",
|
| 155 |
+
value=None
|
| 156 |
+
)
|
|
|
|
| 157 |
|
| 158 |
+
with gr.Row():
|
| 159 |
+
with gr.Column():
|
| 160 |
+
highlight_checkbox = gr.Checkbox(label="Show Highlights Only", value=False)
|
| 161 |
+
on_view_checkbox = gr.Checkbox(label="Currently On View", value=False)
|
| 162 |
+
with gr.Column():
|
| 163 |
+
medium_input = gr.Textbox(
|
| 164 |
+
label="Medium",
|
| 165 |
+
placeholder="e.g., Paintings, Sculpture, Ceramics"
|
| 166 |
+
)
|
| 167 |
+
location_input = gr.Textbox(
|
| 168 |
+
label="Geographic Location",
|
| 169 |
+
placeholder="e.g., France, Japan, Egypt"
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
search_btn = gr.Button("🔍 Search Collection", variant="primary")
|
| 173 |
+
|
| 174 |
+
gallery = gr.Gallery(
|
| 175 |
+
label="Search Results",
|
| 176 |
+
show_label=True,
|
| 177 |
+
elem_id="gallery",
|
| 178 |
+
columns=3,
|
| 179 |
+
rows=4,
|
| 180 |
+
height="800px",
|
| 181 |
+
object_fit="contain"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
info = gr.Textbox(
|
| 185 |
+
label="Artwork Details",
|
| 186 |
+
lines=10,
|
| 187 |
+
show_label=True
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
def handle_search(query, dept, highlight, on_view, medium, location):
|
| 191 |
+
dept_id = department_choices.get(dept) if dept else None
|
| 192 |
+
return search_artworks(query, dept_id, highlight, True, on_view, medium, location)
|
| 193 |
+
|
| 194 |
+
search_btn.click(
|
| 195 |
+
fn=handle_search,
|
| 196 |
+
inputs=[search_input, department_dropdown, highlight_checkbox,
|
| 197 |
+
on_view_checkbox, medium_input, location_input],
|
| 198 |
+
outputs=[gallery, info]
|
| 199 |
+
)
|
| 200 |
|
| 201 |
demo.launch()
|