File size: 1,744 Bytes
449a6d3
145ad7d
ec5aa0b
145ad7d
 
 
c78b25e
 
145ad7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c363e3
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
import requests
import gradio as gr

# Pexels API ํ‚ค๋ฅผ ์—ฌ๊ธฐ์— ์ž…๋ ฅํ•˜์„ธ์š”
API_KEY = '5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62'
API_URL = 'https://api.pexels.com/v1/search'

def search_images(keyword):
    headers = {'Authorization': API_KEY}
    params = {'query': keyword, 'per_page': 8}  # per_page ๊ฐ’์„ 8๋กœ ์„ค์ •ํ•˜์—ฌ 8๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฒ€์ƒ‰
    response = requests.get(API_URL, headers=headers, params=params)
   
    if response.status_code == 200 and response.json()['photos']:
        photos = response.json()['photos']
        result_html = ""
        for photo in photos:
            image_url = photo['src']['original']
            photographer = photo['photographer']
            photographer_url = photo['photographer_url']
            photo_url = photo['url']
           
            # ๊ฐ ์ด๋ฏธ์ง€์™€ ํฌ๋ ˆ๋”ง์„ HTML ํ˜•์‹์œผ๋กœ ์ถ”๊ฐ€
            result_html += f"""
            <div style='margin-bottom: 20px;'>
                <img src='{image_url}' width='100%' />
                <p>This <a href='{photo_url}'>Photo</a> was taken by <a href='{photographer_url}'>{photographer}</a> on <a href='https://www.pexels.com'>Pexels</a>.</p>
            </div>
            """
        return result_html
    else:
        return "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."

interface = gr.Interface(
    fn=search_images,
    inputs=gr.Textbox(lines=2, placeholder="๊ฒ€์ƒ‰ํ•  ์ด๋ฏธ์ง€ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"),
    outputs=gr.HTML(label="๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ"),
    title="Pexels ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰๊ธฐ",
    description="Pexels์—์„œ ํ‚ค์›Œ๋“œ์— ๋งž๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ํ•ด๋‹น ์ด๋ฏธ์ง€์˜ ์ถœ์ฒ˜์™€ ์ž‘๊ฐ€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค."
)

if __name__ == "__main__":
    interface.launch()