user-feedback / app.py
ashmib's picture
Update app.py
00eef23 verified
raw
history blame
2.75 kB
import pandas as pd
import streamlit as st
import sqlite3
# Database setup
DB_FILE = "feedback.db"
def init_db():
# Connect to SQLite database
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Create a table for storing feedback if not exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS feedback (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question TEXT,
selected_answer TEXT,
rating INTEGER,
feedback_text TEXT
)
""")
conn.commit()
conn.close()
def store_feedback(question, selected_answer, rating, feedback_text):
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO feedback (question, selected_answer, rating, feedback_text)
VALUES (?, ?, ?, ?)
""", (question, selected_answer, rating, feedback_text))
conn.commit()
conn.close()
# Initialize database
init_db()
# Load Q&A data
@st.cache_data
def load_data():
return pd.read_csv("dummy_qa_data.csv")
data = load_data()
# Session state for question navigation
if "current_index" not in st.session_state:
st.session_state.current_index = 0
# Current question index
current_index = st.session_state.current_index
# Display question and options
if 0 <= current_index < len(data):
question = data.loc[current_index, "Question"]
answers = eval(data.loc[current_index, "Generated Answer"]) # Convert string to list of tuples
st.subheader(f"Question {current_index + 1}: {question}")
selected_answer = st.radio("Choose the best answer:", options=[ans[0] for ans in answers])
# Rating
rating = st.slider("Rate the answer (1 = Worst, 5 = Best)", 1, 5, value=3)
# Free-text feedback
feedback_text = st.text_area("Any additional feedback?")
# Navigation buttons
col1, col2, col3 = st.columns([1, 1, 2])
with col1:
if st.button("Back") and current_index > 0:
st.session_state.current_index -= 1
st.experimental_rerun()
with col2:
if st.button("Next"):
# Store feedback for the current question
store_feedback(question, selected_answer, rating, feedback_text)
if current_index < len(data) - 1:
st.session_state.current_index += 1
st.experimental_rerun()
else:
st.success("You have completed all questions!")
st.stop()
else:
st.write("No more questions available!")
# View results for debugging (optional)
if st.checkbox("Show Feedback Database (Admin Use)"):
conn = sqlite3.connect(DB_FILE)
df = pd.read_sql_query("SELECT * FROM feedback", conn)
st.dataframe(df)
conn.close()