huggysynuo commited on
Commit
0c0f46c
ยท
verified ยท
1 Parent(s): eeaf866

Update src/bing_image_search.py

Browse files
Files changed (1) hide show
  1. src/bing_image_search.py +32 -29
src/bing_image_search.py CHANGED
@@ -1,29 +1,32 @@
1
- # bing_image_search.py
2
-
3
- import requests
4
-
5
- API_KEY = "5TLuzEb1qJfsIJj1REmI7nrO5VaQEhl0uh9NbjqQ7L7FKhcoPHs1JQQJ99BEACNns7RXJ3w3AAAEACOGtRAh" # Azure Cognitive Services ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ API ํ‚ค
6
- ENDPOINT = "https://image-search-genai.cognitiveservices.azure.com/" # Azure Cognitive Services ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ ์—”๋“œํฌ์ธํŠธ
7
-
8
- def search_bing_images(query: str, count: int = 10) -> list:
9
- headers = {"Ocp-Apim-Subscription-Key": API_KEY}
10
- params = {
11
- "q": query,
12
- "count": count,
13
- "imageType": "Photo",
14
- "safeSearch": "Moderate"
15
- }
16
-
17
- response = requests.get(f"{ENDPOINT}/bing/v7.0/images/search", headers=headers, params=params)
18
- response.raise_for_status()
19
-
20
- results = response.json()
21
- image_urls = [item["contentUrl"] for item in results.get("value", [])]
22
-
23
- return image_urls
24
-
25
- # ์˜ˆ์‹œ ์‹คํ–‰
26
- if __name__ == "__main__":
27
- images = search_bing_images("shadow couple heart pose")
28
- for i, url in enumerate(images):
29
- print(f"{i+1}. {url}")
 
 
 
 
1
+ # bing_image_search.py
2
+
3
+ import os
4
+ import requests
5
+ import streamlit as st
6
+
7
+ # ๐Ÿ’ก ์บ์‹ฑ๋œ ํ™˜๊ฒฝ ์„ค์ • ๋กœ๋”ฉ
8
+ @st.cache_resource
9
+ def load_bing_config():
10
+ api_key = os.environ["BING_API_KEY"]
11
+ endpoint = os.environ.get("BING_ENDPOINT", "https://api.bing.microsoft.com/")
12
+ return api_key, endpoint
13
+
14
+ api_key, endpoint = load_bing_config()
15
+
16
+ def search_bing_images(query: str, count: int = 10) -> list:
17
+ headers = {"Ocp-Apim-Subscription-Key": api_key}
18
+ params = {
19
+ "q": query,
20
+ "count": count,
21
+ "imageType": "Photo",
22
+ "safeSearch": "Moderate"
23
+ }
24
+
25
+ url = f"{endpoint}/bing/v7.0/images/search"
26
+ response = requests.get(url, headers=headers, params=params)
27
+ response.raise_for_status()
28
+
29
+ results = response.json()
30
+ image_urls = [item["contentUrl"] for item in results.get("value", [])]
31
+
32
+ return image_urls