ckfrpark commited on
Commit
40d0b84
โ€ข
1 Parent(s): b36bb3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py CHANGED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Pexels API ํ‚ค ์„ค์ •. ์‹ค์ œ ์‚ฌ์šฉ์‹œ ์—ฌ๋Ÿฌ๋ถ„์˜ API ํ‚ค๋กœ ๊ต์ฒดํ•ด์ฃผ์„ธ์š”.
5
+ API_KEY = 'XDDeRl6U3IY8VRzv7YBGRrnGuOwqpOYD2yS9VTAYM7VEfZcr1aWXubZ1'
6
+ API_ENDPOINT = 'https://api.pexels.com/v1/search'
7
+
8
+ def fetch_high_quality_images(keyword):
9
+ """
10
+ Pexels API๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•œ ๊ณ ํ™”์งˆ ์ด๋ฏธ์ง€ URL ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
11
+ """
12
+ headers = {'Authorization': API_KEY}
13
+ params = {'query': keyword, 'per_page': 10, 'size': 'large'} # per_page๋Š” ์š”์ฒญํ•  ์ด๋ฏธ์ง€ ์ˆ˜๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
14
+ response = requests.get(API_ENDPOINT, headers=headers, params=params)
15
+ images_urls = []
16
+ if response.status_code == 200:
17
+ data = response.json()
18
+ images = data['photos']
19
+ for image in images:
20
+ images_urls.append(image['src']['original'])
21
+ else:
22
+ print("Error:", response.status_code, response.text)
23
+ return images_urls
24
+
25
+ def show_images(keyword):
26
+ """
27
+ ์ฃผ์–ด์ง„ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
28
+ """
29
+ urls = fetch_high_quality_images(keyword)
30
+ # Gradio์˜ Image ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ URL์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
31
+ return [gr.Image(src=url) for url in urls]
32
+
33
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
34
+ interface = gr.Interface(
35
+ fn=show_images,
36
+ inputs=gr.Textbox(label="Enter Keyword"),
37
+ outputs=gr.Gallery(),
38
+ title="Pexels Image Search",
39
+ description="Enter a keyword to search for high-quality images from Pexels."
40
+ )
41
+
42
+ if __name__ == "__main__":
43
+ interface.launch()