Spaces:
Sleeping
Sleeping
Update src/bing_image_search.py
Browse files- 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
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|