Publication / app.py
Ashar086's picture
Create app.py
59fa620 verified
raw
history blame
4.47 kB
import streamlit as st
import random
import pandas as pd
from datetime import datetime
# Load or initialize CSVs for logging interactions and survey responses
try:
interactions_df = pd.read_csv("interactions.csv")
except FileNotFoundError:
interactions_df = pd.DataFrame(columns=["timestamp", "participant_id", "chatbot_type", "interaction", "response"])
try:
survey_df = pd.read_csv("survey_responses.csv")
except FileNotFoundError:
survey_df = pd.DataFrame(columns=["timestamp", "participant_id", "chatbot_type", "emotional_response", "feedback"])
# Initialize session state for participant assignment and interactions
if "participant_id" not in st.session_state:
st.session_state.participant_id = str(random.randint(1000, 9999))
if "chatbot_type" not in st.session_state:
st.session_state.chatbot_type = random.choice(["Positive", "Critical"])
if "idea_count" not in st.session_state:
st.session_state.idea_count = 0
# Display Welcome Message
st.title("Welcome to the Chatbot Study")
st.write("""
Thank you for participating in our study. You will interact with a chatbot
and brainstorm ideas for a marketing campaign. Please press "Start" below to begin.
""")
if st.button("Start Study") or "start" in st.session_state:
st.session_state.start = True
# Chatbot Interaction Section
if "start" in st.session_state:
st.subheader("Chatbot Interaction")
# Display participant information and assigned chatbot type
st.write(f"Participant ID: {st.session_state.participant_id}")
st.write(f"Assigned Chatbot Type: {st.session_state.chatbot_type}")
# Pre-programmed responses for each chatbot type
if st.session_state.chatbot_type == "Positive":
responses = [
"Great idea! I love where you’re going with this.",
"Awesome suggestion! Here’s another thought: what if we add a new twist?",
"You're on fire! That’s a really promising direction.",
"Keep up the creativity! This is really going somewhere.",
]
else:
responses = [
"I don’t think that will work very well. Maybe try something different?",
"Are you sure about that idea? It seems a bit weak.",
"This doesn’t sound very innovative. Try another angle.",
"Not quite there yet. What else do you have in mind?",
]
# User input for submitting an idea
user_input = st.text_input("Enter your idea here:")
if st.button("Submit Idea"):
if st.session_state.idea_count < 10:
response = random.choice(responses)
st.write(f"Chatbot: {response}")
# Record the interaction
interaction_data = {
"timestamp": datetime.now(),
"participant_id": st.session_state.participant_id,
"chatbot_type": st.session_state.chatbot_type,
"interaction": user_input,
"response": response
}
interactions_df = interactions_df.append(interaction_data, ignore_index=True)
interactions_df.to_csv("interactions.csv", index=False)
# Increment idea count
st.session_state.idea_count += 1
st.write(f"Ideas Submitted: {st.session_state.idea_count}/10")
else:
st.warning("You have reached the maximum of 10 ideas.")
# Option to end interaction and proceed to the survey
if st.button("Proceed to Survey"):
st.session_state.end_interaction = True
# Survey Page
if "end_interaction" in st.session_state:
st.subheader("Post-Interaction Survey")
emotional_response = st.radio(
"How did interacting with the chatbot make you feel?",
("Very Positive", "Positive", "Neutral", "Negative", "Very Negative")
)
feedback = st.text_area("Any feedback on the chatbot interaction?")
if st.button("Submit Survey"):
# Record survey data
survey_data = {
"timestamp": datetime.now(),
"participant_id": st.session_state.participant_id,
"chatbot_type": st.session_state.chatbot_type,
"emotional_response": emotional_response,
"feedback": feedback
}
survey_df = survey_df.append(survey_data, ignore_index=True)
survey_df.to_csv("survey_responses.csv", index=False)
st.write("Thank you for completing the study! Your responses have been recorded.")