To upload files, please first save the app
import streamlit as st
import requests
import time
# ------------------------------
# Configuration and Initialization
# ------------------------------
# Define the API URL for creating support tickets
TICKET_API_URL = "https://your-api-endpoint.com/create_ticket" # Replace with your actual API endpoint
# Set the page configuration
st.set_page_config(
page_title="Support Chatbot",
layout="centered",
initial_sidebar_state="expanded",
)
# Initialize session state variables
if "messages" not in st.session_state:
st.session_state["messages"] = [
{"role": "assistant", "content": "Hello! I'm your support assistant. How can I help you today?"}
]
if "show_ticket_form" not in st.session_state:
st.session_state["show_ticket_form"] = False
if "ticket_created" not in st.session_state:
st.session_state["ticket_created"] = False
# ------------------------------
# Helper Functions
# ------------------------------
def add_message(role, content):
"""Add a message to the chat history."""
st.session_state.messages.append({"role": role, "content": content})
def generate_assistant_response(user_message):
"""
Generate a response from the assistant.
For demonstration, this function triggers the support ticket form when the user says 'support'.
"""
if "support" in user_message.lower():
add_message("assistant", "It seems like you're facing an issue. Would you like to create a support ticket?")
st.session_state["show_ticket_form"] = True
else:
# Default response
responses = [
"I'm here to help! Could you please provide more details?",
"Sure, let me assist you with that.",
"Can you please elaborate on your issue?",
]
add_message("assistant", st.session_state["messages"].count < len(responses) and responses[st.session_state["messages"].count] or "I'm here to help!")
def create_support_ticket(name, email, description):
"""Send a POST request to create a support ticket."""
payload = {
"name": name,
"email": email,
"description": description,
}
try:
response = requests.post(TICKET_API_URL, json=payload)
if response.status_code == 201:
return True, "Your support ticket has been created successfully!"
else:
return False, f"Failed to create ticket. Status Code: {response.status_code}"
except requests.exceptions.RequestException as e:
return False, f"An error occurred: {e}"
# ------------------------------
# User Interface Components
# ------------------------------
# Display chat messages
st.title("🤖 Support Chatbot")
for message in st.session_state.messages:
if message["role"] == "assistant":
with st.chat_message("assistant"):
st.markdown(message["content"])
else:
with st.chat_message("user"):
st.markdown(message["content"])
# Chat input
user_input = st.chat_input("Type your message here...")
if user_input:
# Add user message to chat history
add_message("user", user_input)
# Generate assistant response
generate_assistant_response(user_input)
# Display the support ticket form if triggered
if st.session_state["show_ticket_form"]:
with st.chat_message("assistant"):
st.markdown("Please fill out the form below to create a support ticket.")
with st.expander("Create Support Ticket"):
with st.form("support_ticket_form"):
name = st.text_input("Name", max_chars=50)
email = st.text_input("Email", max_chars=50)
description = st.text_area("Description of the Issue", height=150)
submitted = st.form_submit_button("Submit Ticket")
if submitted:
if not name or not email or not description:
st.error("All fields are required.")
else:
with st.spinner("Creating your support ticket..."):
success, message = create_support_ticket(name, email, description)
if success:
st.success(message)
st.session_state["ticket_created"] = True
st.session_state["show_ticket_form"] = False
add_message("assistant", "Thank you! Your support ticket has been created.")
else:
st.error(message)
# Reset Conversation Button
reset = st.sidebar.button("🔄 Reset Conversation")
if reset:
st.session_state["messages"] = [
{"role": "assistant", "content": "Hello! I'm your support assistant. How can I help you today?"}
]
st.session_state["show_ticket_form"] = False
st.session_state["ticket_created"] = False
st.experimental_rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?