ChicagoGallery / app.py
immunobiotech's picture
Update app.py
8540b91 verified
raw
history blame
4.47 kB
import gradio as gr
import requests
import json
from PIL import Image
import io
# API 기본 URL
BASE_URL = "https://api.artic.edu/api/v1"
def search_artworks(query, is_public_domain=False):
search_url = f"{BASE_URL}/artworks/search"
params = {
"q": query,
"limit": 10,
"fields": "id,title,artist_display,date_display,image_id,is_public_domain",
}
if is_public_domain:
params["query"] = {"term": {"is_public_domain": True}}
try:
response = requests.get(search_url, params=params)
response.raise_for_status()
results = response.json()
if "data" not in results:
return [], "No search results found."
images = []
captions = []
for artwork in results["data"]:
if artwork.get("image_id"):
image_url = f"https://www.artic.edu/iiif/2/{artwork['image_id']}/full/843,/0/default.jpg"
artwork_info = f"""Title: {artwork.get('title', 'Unknown')}\nArtist: {artwork.get('artist_display', 'Unknown')}\nDate: {artwork.get('date_display', 'Unknown')}"""
try:
img_response = requests.get(image_url, timeout=5)
img_response.raise_for_status()
img = Image.open(io.BytesIO(img_response.content))
img.verify()
img = Image.open(io.BytesIO(img_response.content))
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
img = img.convert('RGB')
images.append(img)
captions.append(artwork_info)
except Exception as e:
print(f"Error processing image: {e}")
continue
if not images:
return [], "Unable to load images for the searched artworks."
return images, "\n\n".join(captions)
except Exception as e:
print(f"API request error: {e}")
return [], f"An error occurred during search: {str(e)}"
# Custom CSS for styling
custom_css = """
.gradio-container {
background: linear-gradient(to right, #1a1a1a, #2d2d2d);
color: #ffffff;
}
.gr-button {
background: linear-gradient(to right, #c94b4b, #4b134f);
border: none;
color: white;
font-weight: bold;
}
.gr-button:hover {
background: linear-gradient(to right, #4b134f, #c94b4b);
transform: scale(1.05);
transition: all 0.3s ease;
}
.gr-input {
border: 2px solid #4b134f;
background: rgba(255, 255, 255, 0.1);
color: white;
}
.gr-form {
background: rgba(0, 0, 0, 0.2);
border-radius: 15px;
padding: 20px;
}
.gr-box {
border-radius: 15px;
border: 2px solid #4b134f;
}
.gr-gallery {
background: rgba(0, 0, 0, 0.3);
border-radius: 15px;
padding: 10px;
}
"""
# Gradio interface
with gr.Blocks(css=custom_css) as demo:
gr.Markdown(
"""
# 🎨 Art Institute of Chicago Explorer
*Discover masterpieces from one of the world's premier art collections through an elegant and intuitive interface.*
"""
)
with gr.Row():
with gr.Column():
search_input = gr.Textbox(
label="Enter your search term",
placeholder="e.g., Monet, Impressionism, landscape...",
)
public_domain = gr.Checkbox(
label="Show only public domain artworks",
value=False
)
search_btn = gr.Button(
"🔍 Search",
variant="primary"
)
with gr.Row():
gallery = gr.Gallery(
label="Search Results",
show_label=True,
elem_id="gallery",
columns=[2],
rows=[2],
height="500px",
object_fit="contain"
)
info = gr.Textbox(
label="Artwork Details",
lines=10,
show_label=True
)
search_btn.click(
fn=search_artworks,
inputs=[search_input, public_domain],
outputs=[gallery, info]
)
# Launch with a dark theme
demo.launch(theme=gr.themes.Monochrome(
primary_hue="purple",
secondary_hue="red",
neutral_hue="slate",
font=["Helvetica", "ui-sans-serif"]
))