Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
API_KEY = 'XDDeRl6U3IY8VRzv7YBGRrnGuOwqpOYD2yS9VTAYM7VEfZcr1aWXubZ1' | |
API_ENDPOINT = 'https://api.pexels.com/v1/search' | |
def fetch_high_quality_images(keyword): | |
""" | |
Pexels API๋ฅผ ํธ์ถํ์ฌ ์ฃผ์ด์ง ํค์๋์ ๋ํ ๊ณ ํ์ง ์ด๋ฏธ์ง URL ๋ชฉ๋ก์ ๊ฐ์ ธ์ต๋๋ค. | |
""" | |
headers = {'Authorization': API_KEY} | |
params = {'query': keyword, 'per_page': 10, 'size': 'large'} # per_page๋ ์์ฒญํ ์ด๋ฏธ์ง ์๋ฅผ ์กฐ์ ํ ์ ์์ต๋๋ค. | |
response = requests.get(API_ENDPOINT, headers=headers, params=params) | |
images_urls = [] | |
if response.status_code == 200: | |
data = response.json() | |
images = data['photos'] | |
for image in images: | |
images_urls.append(image['src']['original']) | |
else: | |
print("Error:", response.status_code, response.text) | |
return images_urls | |
def show_images(keyword): | |
""" | |
์ฃผ์ด์ง ํค์๋์ ๋ํ ์ด๋ฏธ์ง ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค. | |
""" | |
urls = fetch_high_quality_images(keyword) | |
# Gradio์ Image ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง URL์ ํ์ํฉ๋๋ค. | |
return [gr.Image(src=url) for url in urls] | |
# Gradio ์ธํฐํ์ด์ค ์ค์ | |
interface = gr.Interface( | |
fn=show_images, | |
inputs=gr.Textbox(label="Enter Keyword"), | |
outputs=gr.Gallery(), | |
title="Pexels Image Search", | |
description="Enter a keyword to search for high-quality images from Pexels." | |
) | |
if __name__ == "__main__": | |
interface.launch() |