Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
# Pexels API ν€ μ€μ | |
PEXELS_API_KEY = 'μ¬κΈ°μ_λΉμ μ_API_ν€λ₯Ό_μ λ ₯νμΈμ' | |
def search_images(keyword): | |
headers = { | |
'Authorization': PEXELS_API_KEY | |
} | |
params = { | |
'query': keyword, | |
'per_page': 80 # ν λ²μ λ°νλ°μ μ΄λ―Έμ§ μ | |
} | |
url = 'https://api.pexels.com/v1/search' | |
response = requests.get(url, headers=headers, params=params) | |
data = response.json() | |
# κ²μλ μ΄λ―Έμ§μ URLμ 리μ€νΈλ‘ μΆμΆ | |
image_urls = [photo['src']['original'] for photo in data['photos']] | |
return image_urls | |
def search_videos(keyword): | |
headers = { | |
'Authorization': PEXELS_API_KEY | |
} | |
params = { | |
'query': keyword, | |
'per_page': 80 # ν λ²μ λ°νλ°μ λΉλμ€ μ | |
} | |
url = 'https://api.pexels.com/videos/search' | |
response = requests.get(url, headers=headers, params=params) | |
data = response.json() | |
# κ²μλ λΉλμ€μ λ€μ΄λ‘λ κ°λ₯ν URLμ 리μ€νΈλ‘ μΆμΆ | |
video_download_links = [video['video_files'][0]['link'] for video in data['videos']] | |
return video_download_links | |
with gr.Blocks() as demo: | |
with gr.Tabs(): | |
with gr.Tab("μ΄λ―Έμ§ κ²μ"): | |
image_search_input = gr.Textbox(label="κ²μ ν€μλ") | |
image_search_output = gr.Gallery(label="κ²μλ μ΄λ―Έμ§") | |
image_search_input.change(search_images, inputs=image_search_input, outputs=image_search_output) | |
with gr.Tab("λΉλμ€ κ²μ"): | |
video_search_input = gr.Textbox(label="κ²μ ν€μλ") | |
video_search_output = gr.Markdown(label="κ²μλ λΉλμ€") | |
video_search_input.change(search_videos, inputs=video_search_input, outputs=video_search_output) | |
demo.launch() |