AamirMalik's picture
Update app.py
19b7a1f verified
import streamlit as st
from scraping_utils import search_bing
# Streamlit app
st.title("Electronics Component Chatbot")
st.write("Search for electronics components using Bing Search API!")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
if prompt := st.chat_input("What component are you looking for?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Perform web search
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]: # Show top 5 results
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)}"
# Add chatbot response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.markdown(response)