seawolf2357 commited on
Commit
145ad7d
โ€ข
1 Parent(s): cfd9d13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -45
app.py CHANGED
@@ -1,51 +1,42 @@
1
- import gradio as gr
2
  import requests
 
3
 
4
- # Pexels API ํ‚ค ์„ค์ •
5
- PEXELS_API_KEY = '์—ฌ๊ธฐ์—_๋‹น์‹ ์˜_API_ํ‚ค๋ฅผ_์ž…๋ ฅํ•˜์„ธ์š”'
 
6
 
7
  def search_images(keyword):
8
- headers = {
9
- 'Authorization': PEXELS_API_KEY
10
- }
11
- params = {
12
- 'query': keyword,
13
- 'per_page': 80 # ํ•œ ๋ฒˆ์— ๋ฐ˜ํ™˜๋ฐ›์„ ์ด๋ฏธ์ง€ ์ˆ˜
14
- }
15
- url = 'https://api.pexels.com/v1/search'
16
- response = requests.get(url, headers=headers, params=params)
17
- data = response.json()
18
-
19
- # ๊ฒ€์ƒ‰๋œ ์ด๋ฏธ์ง€์˜ URL์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ถ”์ถœ
20
- image_urls = [photo['src']['original'] for photo in data['photos']]
21
- return image_urls
22
-
23
- def search_videos(keyword):
24
- headers = {
25
- 'Authorization': PEXELS_API_KEY
26
- }
27
- params = {
28
- 'query': keyword,
29
- 'per_page': 80 # ํ•œ ๋ฒˆ์— ๋ฐ˜ํ™˜๋ฐ›์„ ๋น„๋””์˜ค ์ˆ˜
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
- with gr.Blocks() as demo:
40
- with gr.Tabs():
41
- with gr.Tab("์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰"):
42
- image_search_input = gr.Textbox(label="๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ")
43
- image_search_output = gr.Gallery(label="๊ฒ€์ƒ‰๋œ ์ด๋ฏธ์ง€")
44
- image_search_input.change(search_images, inputs=image_search_input, outputs=image_search_output)
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
- demo.launch()
 
 
 
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()