Spaces:
Sleeping
Sleeping
import requests | |
import gradio as gr | |
# Pexels API ํค๋ฅผ ์ฌ๊ธฐ์ ์ ๋ ฅํ์ธ์ | |
API_KEY = '5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62' | |
API_URL = 'https://api.pexels.com/v1/search' | |
def search_images(keyword): | |
headers = {'Authorization': API_KEY} | |
params = {'query': keyword, 'per_page': 8} # per_page ๊ฐ์ 8๋ก ์ค์ ํ์ฌ 8๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ๊ฒ์ | |
response = requests.get(API_URL, headers=headers, params=params) | |
if response.status_code == 200 and response.json()['photos']: | |
photos = response.json()['photos'] | |
result_html = "" | |
for photo in photos: | |
image_url = photo['src']['original'] | |
photographer = photo['photographer'] | |
photographer_url = photo['photographer_url'] | |
photo_url = photo['url'] | |
# ๊ฐ ์ด๋ฏธ์ง์ ํฌ๋ ๋ง์ HTML ํ์์ผ๋ก ์ถ๊ฐ | |
result_html += f""" | |
<div style='margin-bottom: 20px;'> | |
<img src='{image_url}' width='100%' /> | |
<p>This <a href='{photo_url}'>Photo</a> was taken by <a href='{photographer_url}'>{photographer}</a> on <a href='https://www.pexels.com'>Pexels</a>.</p> | |
</div> | |
""" | |
return result_html | |
else: | |
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค." | |
interface = gr.Interface( | |
fn=search_images, | |
inputs=gr.Textbox(lines=2, placeholder="๊ฒ์ํ ์ด๋ฏธ์ง ํค์๋๋ฅผ ์ ๋ ฅํ์ธ์"), | |
outputs=gr.HTML(label="๊ฒ์ ๊ฒฐ๊ณผ"), | |
title="Pexels ์ด๋ฏธ์ง ๊ฒ์๊ธฐ", | |
description="Pexels์์ ํค์๋์ ๋ง๋ ์ด๋ฏธ์ง๋ฅผ ๊ฒ์ํ๊ณ , ํด๋น ์ด๋ฏธ์ง์ ์ถ์ฒ์ ์๊ฐ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค." | |
) | |
if __name__ == "__main__": | |
interface.launch() |