immunobiotech
commited on
Create app.ph
Browse files
app.ph
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
# API 기본 URL
|
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,
|
15 |
+
"limit": 10,
|
16 |
+
"fields": "id,title,artist_display,date_display,image_id,is_public_domain",
|
17 |
+
}
|
18 |
+
|
19 |
+
if is_public_domain:
|
20 |
+
params["query"] = {"term": {"is_public_domain": True}}
|
21 |
+
|
22 |
+
response = requests.get(search_url, params=params)
|
23 |
+
results = response.json()
|
24 |
+
|
25 |
+
if "data" not in results:
|
26 |
+
return "검색 결과가 없습니다."
|
27 |
+
|
28 |
+
output = []
|
29 |
+
for artwork in results["data"]:
|
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 |
+
artwork_info = f"""
|
36 |
+
제목: {artwork.get('title', 'Unknown')}
|
37 |
+
작가: {artwork.get('artist_display', 'Unknown')}
|
38 |
+
제작연도: {artwork.get('date_display', 'Unknown')}
|
39 |
+
"""
|
40 |
+
|
41 |
+
# 이미지 다운로드 및 PIL Image로 변환
|
42 |
+
try:
|
43 |
+
img_response = requests.get(image_url)
|
44 |
+
img = Image.open(io.BytesIO(img_response.content))
|
45 |
+
output.append((img, artwork_info))
|
46 |
+
except:
|
47 |
+
continue
|
48 |
+
|
49 |
+
return output if output else "검색 결과가 없습니다."
|
50 |
+
|
51 |
+
def get_gallery_info(gallery_id):
|
52 |
+
# 갤러리 정보 API 호출
|
53 |
+
gallery_url = f"{BASE_URL}/galleries/{gallery_id}"
|
54 |
+
response = requests.get(gallery_url)
|
55 |
+
gallery = response.json()
|
56 |
+
|
57 |
+
if "data" not in gallery:
|
58 |
+
return "갤러리 정보를 찾을 수 없습니다."
|
59 |
+
|
60 |
+
gallery_data = gallery["data"]
|
61 |
+
return f"""
|
62 |
+
갤러리 이름: {gallery_data.get('title', 'Unknown')}
|
63 |
+
위치: {gallery_data.get('location', 'Unknown')}
|
64 |
+
설명: {gallery_data.get('description', 'No description available')}
|
65 |
+
"""
|
66 |
+
|
67 |
+
# Gradio 인터페이스 구성
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
gr.Markdown("# Art Institute of Chicago Gallery")
|
70 |
+
|
71 |
+
with gr.Tab("작품 검색"):
|
72 |
+
with gr.Row():
|
73 |
+
search_input = gr.Textbox(label="검색어를 입력하세요")
|
74 |
+
public_domain = gr.Checkbox(label="공개 도메인 작품만 보기")
|
75 |
+
search_btn = gr.Button("검색")
|
76 |
+
gallery = gr.Gallery(label="검색 결과")
|
77 |
+
info = gr.Textbox(label="작품 정보", lines=3)
|
78 |
+
|
79 |
+
search_btn.click(
|
80 |
+
fn=search_artworks,
|
81 |
+
inputs=[search_input, public_domain],
|
82 |
+
outputs=[gallery, info]
|
83 |
+
)
|
84 |
+
|
85 |
+
with gr.Tab("갤러리 정보"):
|
86 |
+
gallery_id = gr.Number(label="갤러리 ID를 입력하세요")
|
87 |
+
gallery_btn = gr.Button("정보 조회")
|
88 |
+
gallery_info = gr.Textbox(label="갤러리 정보", lines=5)
|
89 |
+
|
90 |
+
gallery_btn.click(
|
91 |
+
fn=get_gallery_info,
|
92 |
+
inputs=gallery_id,
|
93 |
+
outputs=gallery_info
|
94 |
+
)
|
95 |
+
|
96 |
+
demo.launch()
|