Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Commit
โข
145ad7d
1
Parent(s):
cfd9d13
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,42 @@
|
|
1 |
-
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
-
# Pexels API
|
5 |
-
|
|
|
6 |
|
7 |
def search_images(keyword):
|
8 |
-
headers = {
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
'
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
url = 'https://api.pexels.com/videos/search'
|
32 |
-
response = requests.get(url, headers=headers, params=params)
|
33 |
-
data = response.json()
|
34 |
-
|
35 |
-
# ๊ฒ์๋ ๋น๋์ค์ ๋ค์ด๋ก๋ ๊ฐ๋ฅํ URL์ ๋ฆฌ์คํธ๋ก ์ถ์ถ
|
36 |
-
video_download_links = [video['video_files'][0]['link'] for video in data['videos']]
|
37 |
-
return video_download_links
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
with gr.Tab("๋น๋์ค ๊ฒ์"):
|
47 |
-
video_search_input = gr.Textbox(label="๊ฒ์ ํค์๋")
|
48 |
-
video_search_output = gr.Markdown(label="๊ฒ์๋ ๋น๋์ค")
|
49 |
-
video_search_input.change(search_videos, inputs=video_search_input, outputs=video_search_output)
|
50 |
|
51 |
-
|
|
|
|
|
|
1 |
import requests
|
2 |
+
import gradio as gr
|
3 |
|
4 |
+
# Pexels API ํค๋ฅผ ์ฌ๊ธฐ์ ์
๋ ฅํ์ธ์
|
5 |
+
API_KEY = '5woz23MGx1QrSY0WHFb0BRi29JvbXPu97Hg0xnklYgHUI8G0w23FKH62'
|
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()
|