File size: 1,449 Bytes
1b203eb
19b7a1f
1b203eb
 
4c76280
19b7a1f
1b203eb
 
 
 
 
 
 
 
 
 
 
734958a
1b203eb
 
 
 
 
4c76280
 
19b7a1f
4c76280
 
 
 
 
19b7a1f
4c76280
 
 
734958a
1b203eb
 
 
734958a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)