|
|
import streamlit as st |
|
|
from scraping_utils import search_bing |
|
|
|
|
|
|
|
|
st.title("Electronics Component Chatbot") |
|
|
st.write("Search for electronics components using Bing Search API!") |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
for message in st.session_state.messages: |
|
|
with st.chat_message(message["role"]): |
|
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
if prompt := st.chat_input("What component are you looking for?"): |
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
with st.chat_message("user"): |
|
|
st.markdown(prompt) |
|
|
|
|
|
|
|
|
try: |
|
|
search_results = search_bing(prompt) |
|
|
if not search_results: |
|
|
response = "Sorry, no results were found for your query. Please try again with a different keyword." |
|
|
else: |
|
|
response = "### Search Results:\n" |
|
|
for result in search_results[:5]: |
|
|
response += f"- **[{result['title']}]({result['link']})**\n {result['snippet']}\n\n" |
|
|
|
|
|
except Exception as e: |
|
|
response = f"An error occurred during the search: {str(e)}" |
|
|
|
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
with st.chat_message("assistant"): |
|
|
st.markdown(response) |
|
|
|