Free_Image2 / app.py
ckfrpark's picture
Update app.py
5272ca4 verified
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()