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