Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from scraping_utils import scrape_farnell, scrape_digikey
|
| 4 |
+
|
| 5 |
+
# Load an open-source conversational model
|
| 6 |
+
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 7 |
+
|
| 8 |
+
# Streamlit app
|
| 9 |
+
st.title("Electronics Component Chatbot")
|
| 10 |
+
st.write("Ask me anything about electronics components!")
|
| 11 |
+
|
| 12 |
+
# Initialize chat history
|
| 13 |
+
if "messages" not in st.session_state:
|
| 14 |
+
st.session_state.messages = []
|
| 15 |
+
|
| 16 |
+
# Display chat messages
|
| 17 |
+
for message in st.session_state.messages:
|
| 18 |
+
with st.chat_message(message["role"]):
|
| 19 |
+
st.markdown(message["content"])
|
| 20 |
+
|
| 21 |
+
# Accept user input
|
| 22 |
+
if prompt := st.chat_input("What is your question?"):
|
| 23 |
+
# Add user message to chat history
|
| 24 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 25 |
+
with st.chat_message("user"):
|
| 26 |
+
st.markdown(prompt)
|
| 27 |
+
|
| 28 |
+
# Check if the user is asking for component data
|
| 29 |
+
if "datasheet" in prompt.lower() or "component" in prompt.lower():
|
| 30 |
+
# Extract component name from the prompt
|
| 31 |
+
component_name = prompt.replace("datasheet", "").replace("component", "").strip()
|
| 32 |
+
|
| 33 |
+
# Scrape data from Farnell and DigiKey
|
| 34 |
+
farnell_data = scrape_farnell(component_name)
|
| 35 |
+
digikey_data = scrape_digikey(component_name)
|
| 36 |
+
|
| 37 |
+
# Combine and display results
|
| 38 |
+
response = "Here are the components I found:\n\n"
|
| 39 |
+
for data in farnell_data + digikey_data:
|
| 40 |
+
response += f"**Name:** {data['name']}\n"
|
| 41 |
+
response += f"**Description:** {data['description']}\n"
|
| 42 |
+
response += f"**Datasheet:** [Link]({data['datasheet_link']})\n\n"
|
| 43 |
+
else:
|
| 44 |
+
# Get chatbot response for general queries
|
| 45 |
+
response = chatbot(prompt)[0]['generated_text']
|
| 46 |
+
|
| 47 |
+
# Add chatbot response to chat history
|
| 48 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 49 |
+
with st.chat_message("assistant"):
|
| 50 |
+
st.markdown(response)
|