Spaces:
Sleeping
Sleeping
File size: 1,685 Bytes
449a6d3 145ad7d f78f277 ec5aa0b 145ad7d 78392bc 145ad7d c78b25e 145ad7d f78f277 145ad7d f78f277 145ad7d f78f277 145ad7d 1c363e3 145ad7d f78f277 145ad7d f78f277 145ad7d 89ea00c 145ad7d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import requests
import gradio as gr
import os
import imageio
import numpy as np
from PIL import Image
import requests
from io import BytesIO
import tempfile
# Pexels API ํค๋ฅผ ์ฌ๊ธฐ์ ์
๋ ฅํ์ธ์
API_KEY = os.getenv('API_KEY')
API_URL = 'https://api.pexels.com/v1/search'
def search_images(keyword):
headers = {'Authorization': API_KEY}
params = {'query': keyword, 'per_page': 5} # per_page ๊ฐ์ 5๋ก ์ค์
response = requests.get(API_URL, headers=headers, params=params)
if response.status_code == 200 and response.json()['photos']:
photos = response.json()['photos']
images = []
for photo in photos:
image_url = photo['src']['original']
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
images.append(img)
# ์ด๋ฏธ์ง๋ฅผ ์์์ผ๋ก ํฉ์น๊ธฐ
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
with imageio.get_writer(tmpfile.name, fps=0.5) as video: # 2์ด ๊ฐ๊ฒฉ
for img in images:
video.append_data(np.array(img))
return tmpfile.name # ์์ ํ์ผ์ ๊ฒฝ๋ก ๋ฐํ
else:
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
interface = gr.Interface(
fn=search_images,
inputs=gr.Textbox(lines=2, placeholder="๊ฒ์ํ ์ด๋ฏธ์ง ํค์๋๋ฅผ ์
๋ ฅํ์ธ์"),
outputs=gr.Video(label="๊ฒ์ ๊ฒฐ๊ณผ"),
title="Pexels ์ด๋ฏธ์ง ๊ฒ์๊ธฐ",
description="Pexels์์ ํค์๋์ ๋ง๋ ์ด๋ฏธ์ง๋ฅผ ๊ฒ์ํ๊ณ , ํด๋น ์ด๋ฏธ์ง๋ฅผ ์์์ผ๋ก ๋ณํํฉ๋๋ค."
)
if __name__ == "__main__":
interface.launch() |