Spaces:
Build error
Build error
Update app1.py
Browse files
app1.py
CHANGED
|
@@ -1,18 +1,40 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
-
# Digi-Key API
|
|
|
|
| 6 |
API_URL = "https://api.digikey.com/products/v4/search/keyword"
|
| 7 |
ACCESS_TOKEN = "f8QUVTVNAHeyLrLzZ8iARnrYQI1p" # 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 {
|
| 14 |
"Content-Type": "application/json",
|
| 15 |
-
"X-DIGIKEY-Client-Id": CLIENT_ID,
|
| 16 |
}
|
| 17 |
payload = {
|
| 18 |
"Keywords": keywords,
|
|
@@ -26,7 +48,6 @@ def search_digikey_components(keywords: str) -> dict:
|
|
| 26 |
"SortOrder": "Ascending",
|
| 27 |
},
|
| 28 |
}
|
| 29 |
-
|
| 30 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 31 |
if response.status_code == 200:
|
| 32 |
return response.json()
|
|
@@ -34,12 +55,28 @@ def search_digikey_components(keywords: str) -> dict:
|
|
| 34 |
st.error(f"API request failed: {response.status_code} - {response.text}")
|
| 35 |
return {}
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Streamlit app interface
|
| 38 |
-
st.title("Digi-Key Circuit Component Advisor")
|
| 39 |
st.write(
|
| 40 |
"""
|
| 41 |
-
This app helps circuit designers search for electronic components using the Digi-Key API
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
)
|
| 45 |
|
|
@@ -52,6 +89,7 @@ if 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"])
|
|
@@ -66,6 +104,11 @@ if keywords:
|
|
| 66 |
st.image(product["PhotoUrl"], width=200)
|
| 67 |
else:
|
| 68 |
st.write("_Image not available_")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
st.write("---")
|
| 70 |
else:
|
| 71 |
st.warning("No components found. Try using different keywords.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 5 |
|
| 6 |
+
# Digi-Key API and credentials
|
| 7 |
+
TOKEN_URL = "https://api.digikey.com/v1/oauth2/token"
|
| 8 |
API_URL = "https://api.digikey.com/products/v4/search/keyword"
|
| 9 |
ACCESS_TOKEN = "f8QUVTVNAHeyLrLzZ8iARnrYQI1p" # Replace with your valid access token
|
| 10 |
CLIENT_ID = "K9d4a2AaGwQcoAvdNDZVYEOB3sqL4bMg" # Replace with your Digi-Key Client ID
|
| 11 |
|
| 12 |
+
# Function to fetch access token
|
| 13 |
+
@st.cache_data(ttl=3500) # Cache the token for its lifespan (1 hour)
|
| 14 |
+
def fetch_access_token() -> str:
|
| 15 |
+
headers = {
|
| 16 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 17 |
+
}
|
| 18 |
+
data = {
|
| 19 |
+
"client_id": CLIENT_ID,
|
| 20 |
+
"client_secret": CLIENT_SECRET,
|
| 21 |
+
"grant_type": "client_credentials",
|
| 22 |
+
}
|
| 23 |
+
response = requests.post(TOKEN_URL, headers=headers, data=data)
|
| 24 |
+
if response.status_code == 200:
|
| 25 |
+
token_data = response.json()
|
| 26 |
+
return token_data["access_token"]
|
| 27 |
+
else:
|
| 28 |
+
st.error(f"Failed to retrieve access token: {response.status_code} - {response.text}")
|
| 29 |
+
st.stop()
|
| 30 |
+
|
| 31 |
# Function to make API requests to Digi-Key
|
| 32 |
def search_digikey_components(keywords: str) -> dict:
|
| 33 |
+
access_token = fetch_access_token() # Get the token automatically
|
| 34 |
headers = {
|
| 35 |
+
"Authorization": f"Bearer {access_token}",
|
| 36 |
"Content-Type": "application/json",
|
| 37 |
+
"X-DIGIKEY-Client-Id": CLIENT_ID,
|
| 38 |
}
|
| 39 |
payload = {
|
| 40 |
"Keywords": keywords,
|
|
|
|
| 48 |
"SortOrder": "Ascending",
|
| 49 |
},
|
| 50 |
}
|
|
|
|
| 51 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 52 |
if response.status_code == 200:
|
| 53 |
return response.json()
|
|
|
|
| 55 |
st.error(f"API request failed: {response.status_code} - {response.text}")
|
| 56 |
return {}
|
| 57 |
|
| 58 |
+
# Load T5 model and tokenizer
|
| 59 |
+
@st.cache_resource
|
| 60 |
+
def load_t5_model():
|
| 61 |
+
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
| 62 |
+
model = T5ForConditionalGeneration.from_pretrained("t5-small")
|
| 63 |
+
return tokenizer, model
|
| 64 |
+
|
| 65 |
+
# Generate advice for a component
|
| 66 |
+
def generate_advice(product: dict, tokenizer, model) -> str:
|
| 67 |
+
description = product.get("Description", {}).get("DetailedDescription", "No description available.")
|
| 68 |
+
prompt = f"Provide advice for using this electronic component: {description}"
|
| 69 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 70 |
+
outputs = model.generate(inputs, max_length=100, num_beams=4, early_stopping=True)
|
| 71 |
+
advice = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 72 |
+
return advice
|
| 73 |
+
|
| 74 |
# Streamlit app interface
|
| 75 |
+
st.title("Digi-Key Circuit Component Advisor with AI")
|
| 76 |
st.write(
|
| 77 |
"""
|
| 78 |
+
This app helps circuit designers search for electronic components using the Digi-Key API
|
| 79 |
+
and provides advice using a pretrained T5 model.
|
| 80 |
"""
|
| 81 |
)
|
| 82 |
|
|
|
|
| 89 |
|
| 90 |
if "Products" in data and data["Products"]:
|
| 91 |
st.header("Search Results")
|
| 92 |
+
tokenizer, model = load_t5_model()
|
| 93 |
|
| 94 |
for product in data["Products"]:
|
| 95 |
st.subheader(product["Description"]["ProductDescription"])
|
|
|
|
| 104 |
st.image(product["PhotoUrl"], width=200)
|
| 105 |
else:
|
| 106 |
st.write("_Image not available_")
|
| 107 |
+
|
| 108 |
+
# Generate advice for the product
|
| 109 |
+
advice = generate_advice(product, tokenizer, model)
|
| 110 |
+
st.write(f"**AI Advice:** {advice}")
|
| 111 |
+
|
| 112 |
st.write("---")
|
| 113 |
else:
|
| 114 |
st.warning("No components found. Try using different keywords.")
|