an ai
To upload files, please first save the app
import streamlit as st
import random
import time
st.title("🤖 AI Chat Assistant")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Function to simulate AI response
def ai_response_generator(prompt):
responses = [
f"I understand you're asking about '{prompt}'. Let me help you with that.",
f"Thanks for your question about '{prompt}'. Here's what I think...",
f"Interesting question about '{prompt}'! Let me share my thoughts.",
f"Regarding '{prompt}', I can provide some insights."
]
response = random.choice(responses) + " " + generate_detailed_response(prompt)
for word in response.split():
yield word + " "
time.sleep(0.05)
def generate_detailed_response(prompt):
responses = [
"Based on my analysis, this is a complex topic that requires careful consideration. Let me break it down for you: First, we should consider the main aspects. Second, we need to look at the implications. Finally, I'd recommend exploring this further.",
"From my perspective, there are several key points to consider. The main one is understanding the context. Additionally, we should think about the practical applications. Let me elaborate on these points.",
"This is an interesting area to explore. There are multiple factors at play here. Let's examine the most important aspects and how they interconnect.",
"I've processed your query and can offer some insights. The primary consideration is understanding the fundamental concepts. Then we can look at how these apply to your specific situation."
]
return random.choice(responses)
# Add a sidebar with information
with st.sidebar:
st.markdown("### About this Chat Assistant")
st.write("This is a simple AI chat assistant that can engage in conversation with you. While it uses simulated responses, it demonstrates the basic structure of an AI chat interface.")
st.markdown("### How to use")
st.write("Simply type your message in the chat input box below and press Enter. The AI will respond to your message.")
# 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 would you like to know?"):
# 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)
# Display assistant response in chat message container
with st.chat_message("assistant"):
response = st.write_stream(ai_response_generator(prompt))
# Add assistant response 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?