Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import io
|
|
8 |
BASE_URL = "https://api.artic.edu/api/v1"
|
9 |
|
10 |
def search_artworks(query, is_public_domain=False):
|
11 |
-
# ์ํธ์ํฌ ๊ฒ์ API ํธ์ถ
|
12 |
search_url = f"{BASE_URL}/artworks/search"
|
13 |
params = {
|
14 |
"q": query,
|
@@ -21,107 +20,136 @@ def search_artworks(query, is_public_domain=False):
|
|
21 |
|
22 |
try:
|
23 |
response = requests.get(search_url, params=params)
|
24 |
-
response.raise_for_status()
|
25 |
results = response.json()
|
26 |
|
27 |
if "data" not in results:
|
28 |
-
return [], "
|
29 |
|
30 |
images = []
|
31 |
captions = []
|
32 |
|
33 |
for artwork in results["data"]:
|
34 |
if artwork.get("image_id"):
|
35 |
-
# IIIF ์ด๋ฏธ์ง URL ์์ฑ
|
36 |
image_url = f"https://www.artic.edu/iiif/2/{artwork['image_id']}/full/843,/0/default.jpg"
|
37 |
|
38 |
-
|
39 |
-
artwork_info = f"""์ ๋ชฉ: {artwork.get('title', 'Unknown')}\n์๊ฐ: {artwork.get('artist_display', 'Unknown')}\n์ ์์ฐ๋: {artwork.get('date_display', 'Unknown')}"""
|
40 |
|
41 |
try:
|
42 |
-
img_response = requests.get(image_url, timeout=5)
|
43 |
-
img_response.raise_for_status()
|
44 |
|
45 |
-
# ์ด๋ฏธ์ง ๊ฒ์ฆ
|
46 |
img = Image.open(io.BytesIO(img_response.content))
|
47 |
-
img.verify()
|
48 |
-
img = Image.open(io.BytesIO(img_response.content))
|
49 |
|
50 |
-
# RGB ๋ชจ๋๋ก ๋ณํ (์ํ ์ฑ๋ ์ ๊ฑฐ)
|
51 |
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
52 |
img = img.convert('RGB')
|
53 |
|
54 |
images.append(img)
|
55 |
captions.append(artwork_info)
|
56 |
except Exception as e:
|
57 |
-
print(f"
|
58 |
continue
|
59 |
|
60 |
if not images:
|
61 |
-
return [], "
|
62 |
|
63 |
return images, "\n\n".join(captions)
|
64 |
|
65 |
except Exception as e:
|
66 |
-
print(f"API
|
67 |
-
return [], f"
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
-
# Gradio
|
89 |
-
with gr.Blocks() as demo:
|
90 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
with gr.
|
93 |
-
with gr.
|
94 |
-
search_input = gr.Textbox(
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
height="auto"
|
106 |
)
|
107 |
-
|
108 |
-
info = gr.Textbox(label="์ํ ์ ๋ณด", lines=10)
|
109 |
-
|
110 |
-
search_btn.click(
|
111 |
-
fn=search_artworks,
|
112 |
-
inputs=[search_input, public_domain],
|
113 |
-
outputs=[gallery, info]
|
114 |
-
)
|
115 |
|
116 |
-
with gr.
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
BASE_URL = "https://api.artic.edu/api/v1"
|
9 |
|
10 |
def search_artworks(query, is_public_domain=False):
|
|
|
11 |
search_url = f"{BASE_URL}/artworks/search"
|
12 |
params = {
|
13 |
"q": query,
|
|
|
20 |
|
21 |
try:
|
22 |
response = requests.get(search_url, params=params)
|
23 |
+
response.raise_for_status()
|
24 |
results = response.json()
|
25 |
|
26 |
if "data" not in results:
|
27 |
+
return [], "No search results found."
|
28 |
|
29 |
images = []
|
30 |
captions = []
|
31 |
|
32 |
for artwork in results["data"]:
|
33 |
if artwork.get("image_id"):
|
|
|
34 |
image_url = f"https://www.artic.edu/iiif/2/{artwork['image_id']}/full/843,/0/default.jpg"
|
35 |
|
36 |
+
artwork_info = f"""Title: {artwork.get('title', 'Unknown')}\nArtist: {artwork.get('artist_display', 'Unknown')}\nDate: {artwork.get('date_display', 'Unknown')}"""
|
|
|
37 |
|
38 |
try:
|
39 |
+
img_response = requests.get(image_url, timeout=5)
|
40 |
+
img_response.raise_for_status()
|
41 |
|
|
|
42 |
img = Image.open(io.BytesIO(img_response.content))
|
43 |
+
img.verify()
|
44 |
+
img = Image.open(io.BytesIO(img_response.content))
|
45 |
|
|
|
46 |
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
|
47 |
img = img.convert('RGB')
|
48 |
|
49 |
images.append(img)
|
50 |
captions.append(artwork_info)
|
51 |
except Exception as e:
|
52 |
+
print(f"Error processing image: {e}")
|
53 |
continue
|
54 |
|
55 |
if not images:
|
56 |
+
return [], "Unable to load images for the searched artworks."
|
57 |
|
58 |
return images, "\n\n".join(captions)
|
59 |
|
60 |
except Exception as e:
|
61 |
+
print(f"API request error: {e}")
|
62 |
+
return [], f"An error occurred during search: {str(e)}"
|
63 |
|
64 |
+
# Custom CSS for styling
|
65 |
+
custom_css = """
|
66 |
+
.gradio-container {
|
67 |
+
background: linear-gradient(to right, #1a1a1a, #2d2d2d);
|
68 |
+
color: #ffffff;
|
69 |
+
}
|
70 |
+
.gr-button {
|
71 |
+
background: linear-gradient(to right, #c94b4b, #4b134f);
|
72 |
+
border: none;
|
73 |
+
color: white;
|
74 |
+
font-weight: bold;
|
75 |
+
}
|
76 |
+
.gr-button:hover {
|
77 |
+
background: linear-gradient(to right, #4b134f, #c94b4b);
|
78 |
+
transform: scale(1.05);
|
79 |
+
transition: all 0.3s ease;
|
80 |
+
}
|
81 |
+
.gr-input {
|
82 |
+
border: 2px solid #4b134f;
|
83 |
+
background: rgba(255, 255, 255, 0.1);
|
84 |
+
color: white;
|
85 |
+
}
|
86 |
+
.gr-form {
|
87 |
+
background: rgba(0, 0, 0, 0.2);
|
88 |
+
border-radius: 15px;
|
89 |
+
padding: 20px;
|
90 |
+
}
|
91 |
+
.gr-box {
|
92 |
+
border-radius: 15px;
|
93 |
+
border: 2px solid #4b134f;
|
94 |
+
}
|
95 |
+
.gr-gallery {
|
96 |
+
background: rgba(0, 0, 0, 0.3);
|
97 |
+
border-radius: 15px;
|
98 |
+
padding: 10px;
|
99 |
+
}
|
100 |
+
"""
|
101 |
|
102 |
+
# Gradio interface
|
103 |
+
with gr.Blocks(css=custom_css) as demo:
|
104 |
+
gr.Markdown(
|
105 |
+
"""
|
106 |
+
# ๐จ Art Institute of Chicago Explorer
|
107 |
+
*Discover masterpieces from one of the world's premier art collections through an elegant and intuitive interface.*
|
108 |
+
"""
|
109 |
+
)
|
110 |
|
111 |
+
with gr.Row():
|
112 |
+
with gr.Column():
|
113 |
+
search_input = gr.Textbox(
|
114 |
+
label="Enter your search term",
|
115 |
+
placeholder="e.g., Monet, Impressionism, landscape...",
|
116 |
+
)
|
117 |
+
public_domain = gr.Checkbox(
|
118 |
+
label="Show only public domain artworks",
|
119 |
+
value=False
|
120 |
+
)
|
121 |
+
search_btn = gr.Button(
|
122 |
+
"๐ Search",
|
123 |
+
variant="primary"
|
|
|
124 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
+
with gr.Row():
|
127 |
+
gallery = gr.Gallery(
|
128 |
+
label="Search Results",
|
129 |
+
show_label=True,
|
130 |
+
elem_id="gallery",
|
131 |
+
columns=[2],
|
132 |
+
rows=[2],
|
133 |
+
height="500px",
|
134 |
+
object_fit="contain"
|
135 |
)
|
136 |
+
|
137 |
+
info = gr.Textbox(
|
138 |
+
label="Artwork Details",
|
139 |
+
lines=10,
|
140 |
+
show_label=True
|
141 |
+
)
|
142 |
+
|
143 |
+
search_btn.click(
|
144 |
+
fn=search_artworks,
|
145 |
+
inputs=[search_input, public_domain],
|
146 |
+
outputs=[gallery, info]
|
147 |
+
)
|
148 |
|
149 |
+
# Launch with a dark theme
|
150 |
+
demo.launch(theme=gr.themes.Monochrome(
|
151 |
+
primary_hue="purple",
|
152 |
+
secondary_hue="red",
|
153 |
+
neutral_hue="slate",
|
154 |
+
font=["Helvetica", "ui-sans-serif"]
|
155 |
+
))
|