Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
# Pexels API ํค ์ค์ . ์ค์ ์ฌ์ฉ์ ์ฌ๋ฌ๋ถ์ API ํค๋ก ๊ต์ฒดํด์ฃผ์ธ์.
|
5 |
+
API_KEY = 'XDDeRl6U3IY8VRzv7YBGRrnGuOwqpOYD2yS9VTAYM7VEfZcr1aWXubZ1'
|
6 |
+
API_ENDPOINT = 'https://api.pexels.com/v1/search'
|
7 |
+
|
8 |
+
def fetch_high_quality_images(keyword):
|
9 |
+
"""
|
10 |
+
Pexels API๋ฅผ ํธ์ถํ์ฌ ์ฃผ์ด์ง ํค์๋์ ๋ํ ๊ณ ํ์ง ์ด๋ฏธ์ง URL ๋ชฉ๋ก์ ๊ฐ์ ธ์ต๋๋ค.
|
11 |
+
"""
|
12 |
+
headers = {'Authorization': API_KEY}
|
13 |
+
params = {'query': keyword, 'per_page': 10, 'size': 'large'} # per_page๋ ์์ฒญํ ์ด๋ฏธ์ง ์๋ฅผ ์กฐ์ ํ ์ ์์ต๋๋ค.
|
14 |
+
response = requests.get(API_ENDPOINT, headers=headers, params=params)
|
15 |
+
images_urls = []
|
16 |
+
if response.status_code == 200:
|
17 |
+
data = response.json()
|
18 |
+
images = data['photos']
|
19 |
+
for image in images:
|
20 |
+
images_urls.append(image['src']['original'])
|
21 |
+
else:
|
22 |
+
print("Error:", response.status_code, response.text)
|
23 |
+
return images_urls
|
24 |
+
|
25 |
+
def show_images(keyword):
|
26 |
+
"""
|
27 |
+
์ฃผ์ด์ง ํค์๋์ ๋ํ ์ด๋ฏธ์ง ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
|
28 |
+
"""
|
29 |
+
urls = fetch_high_quality_images(keyword)
|
30 |
+
# Gradio์ Image ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง URL์ ํ์ํฉ๋๋ค.
|
31 |
+
return [gr.Image(src=url) for url in urls]
|
32 |
+
|
33 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
34 |
+
interface = gr.Interface(
|
35 |
+
fn=show_images,
|
36 |
+
inputs=gr.Textbox(label="Enter Keyword"),
|
37 |
+
outputs=gr.Gallery(),
|
38 |
+
title="Pexels Image Search",
|
39 |
+
description="Enter a keyword to search for high-quality images from Pexels."
|
40 |
+
)
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
interface.launch()
|