Spaces:
Runtime error
Update app.py
Browse filesimport 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()
@@ -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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|