Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,36 +3,16 @@ import requests
|
|
| 3 |
import json
|
| 4 |
|
| 5 |
# Digi-Key API details
|
| 6 |
-
TOKEN_URL = "https://api.digikey.com/v1/oauth2/token"
|
| 7 |
API_URL = "https://api.digikey.com/products/v4/search/keyword"
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
REDIRECT_URI = "https://nawal20-component-advisor.hf.space" # Replace with your registered redirect URI
|
| 11 |
-
|
| 12 |
-
# Function to get an access token
|
| 13 |
-
@st.cache_data
|
| 14 |
-
def get_access_token() -> str:
|
| 15 |
-
headers = {
|
| 16 |
-
"Content-Type": "application/x-www-form-urlencoded",
|
| 17 |
-
}
|
| 18 |
-
payload = {
|
| 19 |
-
"client_id": CLIENT_ID,
|
| 20 |
-
"client_secret": CLIENT_SECRET,
|
| 21 |
-
"grant_type": "client_credentials",
|
| 22 |
-
"redirect_uri": REDIRECT_URI,
|
| 23 |
-
}
|
| 24 |
-
response = requests.post(TOKEN_URL, headers=headers, data=payload)
|
| 25 |
-
if response.status_code == 200:
|
| 26 |
-
return response.json()["access_token"]
|
| 27 |
-
else:
|
| 28 |
-
st.error(f"Failed to fetch access token: {response.status_code} - {response.text}")
|
| 29 |
-
return ""
|
| 30 |
|
| 31 |
# Function to make API requests to Digi-Key
|
| 32 |
-
def search_digikey_components(keywords: str
|
| 33 |
headers = {
|
| 34 |
-
"Authorization": f"Bearer {
|
| 35 |
"Content-Type": "application/json",
|
|
|
|
| 36 |
}
|
| 37 |
payload = {
|
| 38 |
"Keywords": keywords,
|
|
@@ -40,7 +20,6 @@ def search_digikey_components(keywords: str, token: str) -> dict:
|
|
| 40 |
"Offset": 0,
|
| 41 |
"FilterOptionsRequest": {
|
| 42 |
"MarketPlaceFilter": "NoFilter",
|
| 43 |
-
"SearchOptions": ["ChipOutpost"],
|
| 44 |
},
|
| 45 |
"SortOptions": {
|
| 46 |
"Field": "None",
|
|
@@ -68,26 +47,20 @@ st.write(
|
|
| 68 |
keywords = st.text_input("Enter the name or keyword of the component:", "")
|
| 69 |
|
| 70 |
if keywords:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
token = get_access_token()
|
| 74 |
|
| 75 |
-
if
|
| 76 |
-
st.
|
| 77 |
-
data = search_digikey_components(keywords, token)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
st.
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
st.write("---")
|
| 91 |
-
else:
|
| 92 |
-
st.warning("No components found. Try using different keywords.")
|
| 93 |
-
|
|
|
|
| 3 |
import json
|
| 4 |
|
| 5 |
# Digi-Key API details
|
|
|
|
| 6 |
API_URL = "https://api.digikey.com/products/v4/search/keyword"
|
| 7 |
+
ACCESS_TOKEN = "whrDdRqwnczx4BhkoOHwPVHujScc" # Replace with your valid access token
|
| 8 |
+
CLIENT_ID = "K9d4a2AaGwQcoAvdNDZVYEOB3sqL4bMg" # Replace with your Digi-Key Client ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Function to make API requests to Digi-Key
|
| 11 |
+
def search_digikey_components(keywords: str) -> dict:
|
| 12 |
headers = {
|
| 13 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
| 14 |
"Content-Type": "application/json",
|
| 15 |
+
"X-DIGIKEY-Client-Id": CLIENT_ID, # Add the required header
|
| 16 |
}
|
| 17 |
payload = {
|
| 18 |
"Keywords": keywords,
|
|
|
|
| 20 |
"Offset": 0,
|
| 21 |
"FilterOptionsRequest": {
|
| 22 |
"MarketPlaceFilter": "NoFilter",
|
|
|
|
| 23 |
},
|
| 24 |
"SortOptions": {
|
| 25 |
"Field": "None",
|
|
|
|
| 47 |
keywords = st.text_input("Enter the name or keyword of the component:", "")
|
| 48 |
|
| 49 |
if keywords:
|
| 50 |
+
st.write(f"Searching for components matching: `{keywords}`...")
|
| 51 |
+
data = search_digikey_components(keywords)
|
|
|
|
| 52 |
|
| 53 |
+
if "Products" in data and data["Products"]:
|
| 54 |
+
st.header("Search Results")
|
|
|
|
| 55 |
|
| 56 |
+
for product in data["Products"]:
|
| 57 |
+
st.subheader(product["Description"]["ProductDescription"])
|
| 58 |
+
st.write(f"**Manufacturer**: {product['Manufacturer']['Name']}")
|
| 59 |
+
st.write(f"**Product Number**: {product['ManufacturerProductNumber']}")
|
| 60 |
+
st.write(f"**Unit Price**: ${product['UnitPrice']}")
|
| 61 |
+
st.write(f"[Datasheet]({product['DatasheetUrl']})")
|
| 62 |
+
st.write(f"[Product Link]({product['ProductUrl']})")
|
| 63 |
+
st.image(product["PhotoUrl"], width=200)
|
| 64 |
+
st.write("---")
|
| 65 |
+
else:
|
| 66 |
+
st.warning("No components found. Try using different keywords.")
|
|
|
|
|
|
|
|
|
|
|
|