aiqtest2 / app.py
seawolf2357's picture
Update app.py
cfd9d13 verified
raw
history blame
1.83 kB
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()