File size: 5,789 Bytes
6f37261
 
 
 
 
 
 
 
 
 
 
 
 
638afb7
6f37261
 
 
 
 
 
1c276c4
 
8540b91
1c276c4
 
 
8540b91
1c276c4
 
 
6f37261
1c276c4
 
638afb7
 
6f37261
8540b91
1c276c4
 
638afb7
8540b91
1c276c4
 
8540b91
 
1c276c4
 
 
 
 
 
 
8540b91
1c276c4
 
 
8540b91
1c276c4
 
 
 
8540b91
 
6f37261
8540b91
 
 
d9d6958
 
8540b91
 
d9d6958
 
 
 
638afb7
 
8540b91
 
d9d6958
8540b91
 
 
 
d9d6958
 
 
638afb7
8540b91
 
d9d6958
 
 
8540b91
 
d9d6958
 
8540b91
 
d9d6958
 
638afb7
 
d9d6958
 
 
 
 
 
 
 
 
 
 
 
 
 
8540b91
638afb7
 
 
 
8540b91
6f37261
8540b91
 
d9d6958
8540b91
d9d6958
 
8540b91
 
f45edb4
 
 
8540b91
638afb7
8540b91
 
 
 
638afb7
8540b91
 
 
 
638afb7
8540b91
 
 
1c276c4
6f37261
8540b91
 
 
 
 
638afb7
 
 
8540b91
6f37261
8540b91
 
 
 
 
 
 
 
 
 
 
 
6f37261
d9d6958
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
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": 12,  # ๋” ๋งŽ์€ ๊ฒฐ๊ณผ ํ‘œ์‹œ
        "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"):
                # ๋” ํฐ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๋กœ ๋ณ€๊ฒฝ (843 -> 1686)
                image_url = f"https://www.artic.edu/iiif/2/{artwork['image_id']}/full/1686,/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=10)  # ํƒ€์ž„์•„์›ƒ ์ฆ๊ฐ€
                    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) !important;
    color: #ffffff !important;
}
.gr-button {
    background: linear-gradient(to right, #c94b4b, #4b134f) !important;
    border: none !important;
    color: white !important;
    font-weight: bold !important;
    padding: 10px 20px !important;
    font-size: 1.1em !important;
}
.gr-button:hover {
    background: linear-gradient(to right, #4b134f, #c94b4b) !important;
    transform: scale(1.05);
    transition: all 0.3s ease;
}
.gr-input {
    border: 2px solid #4b134f !important;
    background: rgba(255, 255, 255, 0.1) !important;
    color: white !important;
    font-size: 1.1em !important;
}
.gr-form {
    background: rgba(0, 0, 0, 0.2) !important;
    border-radius: 15px !important;
    padding: 20px !important;
}
.gr-box {
    border-radius: 15px !important;
    border: 2px solid #4b134f !important;
}
.gr-gallery {
    background: rgba(0, 0, 0, 0.3) !important;
    border-radius: 15px !important;
    padding: 20px !important;
    min-height: 800px !important;  /* ๊ฐค๋Ÿฌ๋ฆฌ ๋†’์ด ์ฆ๊ฐ€ */
}
.title-text {
    text-align: center !important;
    color: #ffffff !important;
    font-size: 2.5em !important;
    margin-bottom: 0.5em !important;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5) !important;
}
.subtitle-text {
    text-align: center !important;
    color: #cccccc !important;
    font-size: 1.2em !important;
    margin-bottom: 2em !important;
    font-style: italic !important;
}
.gallery-image {
    min-height: 400px !important;  /* ๊ฐœ๋ณ„ ์ด๋ฏธ์ง€ ์ตœ์†Œ ๋†’์ด ์„ค์ • */
    object-fit: contain !important;
}
"""

# Gradio interface
with gr.Blocks(css=custom_css) as demo:
    gr.HTML(
        """
        <div class="title-text">๐ŸŽจ Art Institute of Chicago Explorer</div>
        <div class="subtitle-text">Discover masterpieces from one of the world's premier art collections through an elegant and intuitive interface.</div>
        """
    )
    gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fimmunobiotech-ChicagoGallery.hf.space">
               <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fimmunobiotech-ChicagoGallery.hf.space&countColor=%23263759" />
               </a>""")    
    with gr.Row():
        with gr.Column(scale=4):
            search_input = gr.Textbox(
                label="Enter your search term",
                placeholder="e.g., Monet, Impressionism, landscape...",
            )
        with gr.Column(scale=1):
            public_domain = gr.Checkbox(
                label="Show only public domain artworks",
                value=False
            )
        with gr.Column(scale=1):
            search_btn = gr.Button(
                "๐Ÿ” Search",
                variant="primary"
            )
    
    with gr.Row():
        gallery = gr.Gallery(
            label="Search Results",
            show_label=True,
            elem_id="gallery",
            columns=3,  # ์—ด ์ˆ˜ ์ฆ๊ฐ€
            rows=4,     # ํ–‰ ์ˆ˜ ์ฆ๊ฐ€
            height="800px",  # ๊ฐค๋Ÿฌ๋ฆฌ ๋†’์ด ์ฆ๊ฐ€
            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]
    )

demo.launch()