AamirMalik commited on
Commit
734958a
·
verified ·
1 Parent(s): f4adabc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,18 +1,11 @@
1
  import streamlit as st
2
  import requests
3
-
4
- # Hugging Face Inference API endpoint
5
- API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
6
- headers = {"Authorization": "Bearer Component_Chatbot"}
7
-
8
- def query_chatbot(prompt):
9
- payload = {"inputs": prompt}
10
- response = requests.post(API_URL, headers=headers, json=payload)
11
- return response.json()[0]['generated_text']
12
 
13
  # Streamlit app
14
  st.title("Electronics Component Chatbot")
15
- st.write("Ask me anything about electronics components!")
16
 
17
  # Initialize chat history
18
  if "messages" not in st.session_state:
@@ -24,16 +17,33 @@ for message in st.session_state.messages:
24
  st.markdown(message["content"])
25
 
26
  # Accept user input
27
- if prompt := st.chat_input("What is your question?"):
28
  # Add user message to chat history
29
  st.session_state.messages.append({"role": "user", "content": prompt})
30
  with st.chat_message("user"):
31
  st.markdown(prompt)
32
 
33
- # Get chatbot response
34
- response = query_chatbot(prompt)
35
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  # Add chatbot response to chat history
37
  st.session_state.messages.append({"role": "assistant", "content": response})
38
  with st.chat_message("assistant"):
39
- st.markdown(response)
 
1
  import streamlit as st
2
  import requests
3
+ from scraping_utils import scrape_farnell, scrape_digikey
4
+ from urllib.parse import quote
 
 
 
 
 
 
 
5
 
6
  # Streamlit app
7
  st.title("Electronics Component Chatbot")
8
+ st.write("Search for electronics components from Farnell and Digi-Key!")
9
 
10
  # Initialize chat history
11
  if "messages" not in st.session_state:
 
17
  st.markdown(message["content"])
18
 
19
  # Accept user input
20
+ if prompt := st.chat_input("What component are you looking for?"):
21
  # Add user message to chat history
22
  st.session_state.messages.append({"role": "user", "content": prompt})
23
  with st.chat_message("user"):
24
  st.markdown(prompt)
25
 
26
+ # Scrape component data
27
+ try:
28
+ farnell_results = scrape_farnell(quote(prompt))
29
+ digikey_results = scrape_digikey(quote(prompt))
30
+
31
+ # Format response
32
+ response = "### Farnell Results:\n"
33
+ for component in farnell_results[:3]:
34
+ response += f"- **{component['name']}**: {component['description']} ([Datasheet]({component['datasheet_link']}))\n"
35
+
36
+ response += "\n### Digi-Key Results:\n"
37
+ for component in digikey_results[:3]:
38
+ response += f"- **{component['name']}**: {component['description']} ([Datasheet]({component['datasheet_link']}))\n"
39
+
40
+ if not farnell_results and not digikey_results:
41
+ response = "Sorry, no results found for your query."
42
+
43
+ except Exception as e:
44
+ response = f"An error occurred: {str(e)}"
45
+
46
  # Add chatbot response to chat history
47
  st.session_state.messages.append({"role": "assistant", "content": response})
48
  with st.chat_message("assistant"):
49
+ st.markdown(response)