ckfrpark commited on
Commit
b36bb3c
โ€ข
1 Parent(s): 9f84ced

Update app.py

Browse files

import requests
import gradio as gr

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

def search_images(keyword):
headers = {'Authorization': API_KEY}
params = {'query': keyword, 'per_page': 8}
response = requests.get(IMAGE_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']

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 "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."

def search_videos(keyword):
headers = {'Authorization': API_KEY}
params = {'query': keyword, 'per_page': 8}
response = requests.get(VIDEO_API_URL, headers=headers, params=params)

if response.status_code == 200 and response.json()['videos']:
videos = response.json()['videos']
result_html = ""
for video in videos:
video_url = video['video_files'][0]['link']
photographer = video['user']['name']
photographer_url = video['user']['url']

result_html += f"""
<div style='margin-bottom: 20px;'>
<video width='100%' controls>
<source src='{video_url}' type='video/mp4'>
Your browser does not support the video tag.
</video>
<p>This video was created by <a href='{photographer_url}'>{photographer}</a> on <a href='https://www.pexels.com'>Pexels</a>.</p>
</div>
"""
return result_html
else:
return "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."

with gr.Blocks() as app:
with gr.Tabs():
with gr.Tab("์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰"):
image_search_input = gr.Textbox(lines=2, placeholder="๊ฒ€์ƒ‰ํ•  ์ด๋ฏธ์ง€ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”")
image_search_output = gr.HTML()
image_search_button = gr.Button("๊ฒ€์ƒ‰")
image_search_button.click(search_images, inputs=image_search_input, outputs=image_search_output)

with gr.Tab("๋น„๋””์˜ค ๊ฒ€์ƒ‰"):
video_search_input = gr.Textbox(lines=2, placeholder="๊ฒ€์ƒ‰ํ•  ๋น„๋””์˜ค ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”")
video_search_output = gr.HTML()
video_search_button = gr.Button("๊ฒ€์ƒ‰")
video_search_button.click(search_videos, inputs=video_search_input, outputs=video_search_output)

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

Files changed (1) hide show
  1. app.py +0 -42
app.py CHANGED
@@ -1,42 +0,0 @@
1
- import requests
2
- import gradio as gr
3
-
4
- # Pexels API ํ‚ค๋ฅผ ์—ฌ๊ธฐ์— ์ž…๋ ฅํ•˜์„ธ์š”
5
- API_KEY = '2FfqlDuW338fWTpvUD3JWaOWqSB4DSozKaFuFlH2HKxeZdKsSnqmyyyO'
6
- API_URL = 'https://api.pexels.com/v1/search'
7
-
8
- def search_images(keyword):
9
- headers = {'Authorization': API_KEY}
10
- params = {'query': keyword, 'per_page': 8} # per_page ๊ฐ’์„ 8๋กœ ์„ค์ •ํ•˜์—ฌ 8๊ฐœ์˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฒ€์ƒ‰
11
- response = requests.get(API_URL, headers=headers, params=params)
12
-
13
- if response.status_code == 200 and response.json()['photos']:
14
- photos = response.json()['photos']
15
- result_html = ""
16
- for photo in photos:
17
- image_url = photo['src']['original']
18
- photographer = photo['photographer']
19
- photographer_url = photo['photographer_url']
20
- photo_url = photo['url']
21
-
22
- # ๊ฐ ์ด๋ฏธ์ง€์™€ ํฌ๋ ˆ๋”ง์„ HTML ํ˜•์‹์œผ๋กœ ์ถ”๊ฐ€
23
- result_html += f"""
24
- <div style='margin-bottom: 20px;'>
25
- <img src='{image_url}' width='100%' />
26
- <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>
27
- </div>
28
- """
29
- return result_html
30
- else:
31
- return "๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
32
-
33
- interface = gr.Interface(
34
- fn=search_images,
35
- inputs=gr.Textbox(lines=2, placeholder="๊ฒ€์ƒ‰ํ•  ์ด๋ฏธ์ง€ ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”"),
36
- outputs=gr.HTML(label="๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ"),
37
- title="Pexels ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰๊ธฐ",
38
- description="Pexels์—์„œ ํ‚ค์›Œ๋“œ์— ๋งž๋Š” ์ด๋ฏธ์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ , ํ•ด๋‹น ์ด๋ฏธ์ง€์˜ ์ถœ์ฒ˜์™€ ์ž‘๊ฐ€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค."
39
- )
40
-
41
- if __name__ == "__main__":
42
- interface.launch()