Drop files here
or click to upload
import streamlit as st
import requests
import json
# Set up Streamlit page configuration
st.set_page_config(page_title="CRM Chatbot", page_icon="💬")
st.title("CRM Chatbot 🤖")
st.write("Ask me questions about our CRM data!")
# API details
API_URL = "https://emea.snaplogic.com/api/1/rest/slsched/feed/ConnectFasterInc/snapLogic4snapLogic/Bootcamp_EMEA_August_2025/CRM_Retriever_Anjana%20Task"
AUTHORIZATION = "Bearer 12345"
def get_api_response(question):
"""
Sends a question to the API and returns the response body.
"""
headers = {
"Authorization": AUTHORIZATION,
"Content-Type": "application/json"
}
payload = [{"Question": question}]
try:
response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
response.raise_for_status() # Raise an exception for bad status codes
# The API returns a list with a single dictionary, so we access the first element
response_data = response.json()
if response_data and isinstance(response_data, list) and 'body' in response_data[0]:
return response_data[0]['body']
else:
return "Sorry, I couldn't get a valid response from the API."
except requests.exceptions.RequestException as e:
return f"An error occurred: {e}"
# Initialize chat history in Streamlit's session state
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
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 is up?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Get and display the chatbot's response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
response = get_api_response(prompt)
st.markdown(response)
# Add assistant message to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?