Create an app with: (1) a collapsible sidebar. The user will log in here with the username and password. (2) Tab #1. The app should load some sample data (.csv) using pandas, then show a bar chart on the data. (3) Tab #2. It should have a chat, where the user can ask questions to a GPT model to interact with the sample data loaded in Tab #1.
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import random
import time
# Sample data loading
@st.cache_data
def load_data():
return pd.DataFrame({
'Category': ['A', 'B', 'C', 'D'],
'Values': [23, 45, 12, 67]
})
df = load_data()
# Sidebar for login
st.sidebar.title('Login')
username = st.sidebar.text_input('Username')
password = st.sidebar.text_input('Password', type='password')
if st.sidebar.button('Login'):
if username == 'user' and password == 'pass':
st.sidebar.success('Logged in successfully!')
else:
st.sidebar.error('Invalid username or password')
# Create tabs
tabs = st.tabs(['Bar Chart', 'Chat'])
# Tab 1: Bar chart
with tabs[0]:
st.title('Sample Data Bar Chart')
st.bar_chart(df.set_index('Category'))
# Tab 2: Chat interface
with tabs[1]:
st.title('Data Chat')
# Streamed response emulator
def response_generator():
response = random.choice(
[
"I'm here to help! What do you want to know about the data?",
"Ask me any question regarding the sample data!",
"Feel free to inquire about the data columns.",
]
)
for word in response.split():
yield word + " "
time.sleep(0.1)
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message['role']):
st.markdown(message['content'])
if prompt := st.chat_input("What would you like to ask?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
response = st.write_stream(response_generator())
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?