Spaces:
Sleeping
Sleeping
Ashmi Banerjee
commited on
Commit
·
9a6b7dc
1
Parent(s):
dd0c0c8
first draft
Browse files- .config/.gitkeep +0 -0
- .gitignore +165 -0
- README.md +6 -0
- app.py +133 -70
- db/crud.py +101 -0
- db/schema.py +22 -0
- db/setup.py +41 -0
- requirements.txt +5 -1
.config/.gitkeep
ADDED
|
File without changes
|
.gitignore
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# pdm
|
| 105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 106 |
+
#pdm.lock
|
| 107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 108 |
+
# in version control.
|
| 109 |
+
# https://pdm.fming.dev/#use-with-ide
|
| 110 |
+
.pdm.toml
|
| 111 |
+
|
| 112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 113 |
+
__pypackages__/
|
| 114 |
+
|
| 115 |
+
# Celery stuff
|
| 116 |
+
celerybeat-schedule
|
| 117 |
+
celerybeat.pid
|
| 118 |
+
|
| 119 |
+
# SageMath parsed files
|
| 120 |
+
*.sage.py
|
| 121 |
+
|
| 122 |
+
# Environments
|
| 123 |
+
.env
|
| 124 |
+
.venv
|
| 125 |
+
env/
|
| 126 |
+
venv/
|
| 127 |
+
ENV/
|
| 128 |
+
env.bak/
|
| 129 |
+
venv.bak/
|
| 130 |
+
|
| 131 |
+
# Spyder project settings
|
| 132 |
+
.spyderproject
|
| 133 |
+
.spyproject
|
| 134 |
+
|
| 135 |
+
# Rope project settings
|
| 136 |
+
.ropeproject
|
| 137 |
+
|
| 138 |
+
# mkdocs documentation
|
| 139 |
+
/site
|
| 140 |
+
|
| 141 |
+
# mypy
|
| 142 |
+
.mypy_cache/
|
| 143 |
+
.dmypy.json
|
| 144 |
+
dmypy.json
|
| 145 |
+
|
| 146 |
+
# Pyre type checker
|
| 147 |
+
.pyre/
|
| 148 |
+
|
| 149 |
+
# pytype static type analyzer
|
| 150 |
+
.pytype/
|
| 151 |
+
|
| 152 |
+
# Cython debug symbols
|
| 153 |
+
cython_debug/
|
| 154 |
+
|
| 155 |
+
# PyCharm
|
| 156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
+
.idea/
|
| 161 |
+
.ipynb_checkpoints
|
| 162 |
+
config
|
| 163 |
+
notebooks/.ipynb_checkpoints
|
| 164 |
+
.DS_Store
|
| 165 |
+
|
README.md
CHANGED
|
@@ -11,3 +11,9 @@ license: mit
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
+
|
| 15 |
+
### TODO List
|
| 16 |
+
[ ] Consent form
|
| 17 |
+
[ ] Instructions
|
| 18 |
+
[ ] Add proceed button after prolific id
|
| 19 |
+
[ ] Make sure each question has at least 10 responses
|
app.py
CHANGED
|
@@ -1,96 +1,159 @@
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
# Database setup
|
| 6 |
-
DB_FILE = "feedback.db"
|
| 7 |
-
|
| 8 |
-
def init_db():
|
| 9 |
-
# Connect to SQLite database
|
| 10 |
-
conn = sqlite3.connect(DB_FILE)
|
| 11 |
-
cursor = conn.cursor()
|
| 12 |
-
|
| 13 |
-
# Create a table for storing feedback if not exists
|
| 14 |
-
cursor.execute("""
|
| 15 |
-
CREATE TABLE IF NOT EXISTS feedback (
|
| 16 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 17 |
-
question TEXT,
|
| 18 |
-
selected_answer TEXT,
|
| 19 |
-
rating INTEGER,
|
| 20 |
-
feedback_text TEXT
|
| 21 |
-
)
|
| 22 |
-
""")
|
| 23 |
-
conn.commit()
|
| 24 |
-
conn.close()
|
| 25 |
-
|
| 26 |
-
def store_feedback(question, rating, feedback_text):
|
| 27 |
-
conn = sqlite3.connect(DB_FILE)
|
| 28 |
-
cursor = conn.cursor()
|
| 29 |
-
cursor.execute("""
|
| 30 |
-
INSERT INTO feedback (question, rating, feedback_text)
|
| 31 |
-
VALUES (?, ?, ?)
|
| 32 |
-
""", (question, rating, feedback_text))
|
| 33 |
-
conn.commit()
|
| 34 |
-
conn.close()
|
| 35 |
-
|
| 36 |
-
# Initialize database
|
| 37 |
-
init_db()
|
| 38 |
|
| 39 |
# Load Q&A data
|
| 40 |
@st.cache_data
|
| 41 |
def load_data():
|
| 42 |
-
return pd.read_csv("dummy_qa_data.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
st.session_state.current_index = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Current question index
|
| 51 |
-
current_index = st.session_state.current_index
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
question = data.loc[current_index, "Question"]
|
| 56 |
-
|
| 57 |
|
| 58 |
-
st.subheader(f"Question {current_index + 1}
|
|
|
|
| 59 |
st.subheader("Generated Answer:")
|
| 60 |
-
st.write(
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Free-text feedback
|
| 67 |
-
feedback_text = st.text_area("Any additional feedback?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Navigation buttons
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
col1, col2, col3 = st.columns([1, 1, 2])
|
| 71 |
|
| 72 |
-
with col1:
|
| 73 |
if st.button("Back") and current_index > 0:
|
| 74 |
st.session_state.current_index -= 1
|
| 75 |
st.experimental_rerun()
|
| 76 |
|
| 77 |
-
with col2:
|
| 78 |
if st.button("Next"):
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if current_index < len(data) - 1:
|
| 83 |
-
st.session_state.current_index += 1
|
| 84 |
-
# st.experimental_rerun()
|
| 85 |
else:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from db.schema import Feedback, Response
|
| 2 |
+
from db.crud import ingest
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Load Q&A data
|
| 9 |
@st.cache_data
|
| 10 |
def load_data():
|
| 11 |
+
return pd.read_csv("dummy_qa_data.csv")[:3]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Function to validate Prolific ID (customize this based on your format requirements)
|
| 15 |
+
def validate_prolific_id(prolific_id: str) -> bool:
|
| 16 |
+
return bool(prolific_id.strip()) # Example: Check non-empty
|
| 17 |
|
| 18 |
+
# Function to reset the survey
|
| 19 |
|
| 20 |
+
|
| 21 |
+
def reset_survey():
|
| 22 |
+
st.session_state.prolific_id = None
|
| 23 |
st.session_state.current_index = 0
|
| 24 |
+
st.session_state.responses = []
|
| 25 |
+
st.session_state.completed = False
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def initialization():
|
| 29 |
+
"""Initialize session state variables."""
|
| 30 |
+
if "current_index" not in st.session_state:
|
| 31 |
+
st.session_state.current_index = 0
|
| 32 |
+
if "prolific_id" not in st.session_state:
|
| 33 |
+
st.session_state.prolific_id = None
|
| 34 |
+
if "responses" not in st.session_state:
|
| 35 |
+
st.session_state.responses = []
|
| 36 |
+
if "completed" not in st.session_state:
|
| 37 |
+
st.session_state.completed = False
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def prolific_id_screen():
|
| 41 |
+
"""Display the Prolific ID entry screen."""
|
| 42 |
+
st.title("Welcome to the Feedback Survey")
|
| 43 |
+
prolific_id_input = st.text_input("Enter your Prolific ID and press ENTER:")
|
| 44 |
+
|
| 45 |
+
if prolific_id_input:
|
| 46 |
+
if validate_prolific_id(prolific_id_input):
|
| 47 |
+
st.session_state.prolific_id = prolific_id_input
|
| 48 |
+
st.success("Prolific ID recorded! Now, proceed with the questions.")
|
| 49 |
+
st.experimental_rerun()
|
| 50 |
+
else:
|
| 51 |
+
st.warning("Invalid Prolific ID. Please check and try again.")
|
| 52 |
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def questions_screen(data):
|
| 55 |
+
"""Display the questions screen."""
|
| 56 |
+
current_index = st.session_state.current_index
|
| 57 |
+
|
| 58 |
+
# Display question and answer
|
| 59 |
question = data.loc[current_index, "Question"]
|
| 60 |
+
generated_answer = data.loc[current_index, "Generated Answer"]
|
| 61 |
|
| 62 |
+
st.subheader(f"Question {current_index + 1}")
|
| 63 |
+
st.markdown(f"***{question}***")
|
| 64 |
st.subheader("Generated Answer:")
|
| 65 |
+
st.write(generated_answer)
|
| 66 |
+
|
| 67 |
+
# Prefill responses if they exist
|
| 68 |
+
previous_rating = (
|
| 69 |
+
st.session_state.responses[current_index].ans
|
| 70 |
+
if len(st.session_state.responses) > current_index
|
| 71 |
+
else 0
|
| 72 |
+
)
|
| 73 |
+
previous_feedback = (
|
| 74 |
+
st.session_state.responses[current_index].feedback_text
|
| 75 |
+
if len(st.session_state.responses) > current_index
|
| 76 |
+
else ""
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Rating slider
|
| 80 |
+
rating = st.slider("Rate the answer (1 = Worst, 5 = Best)", 1, 5, value=previous_rating,
|
| 81 |
+
key=f"rating_{current_index}")
|
| 82 |
|
| 83 |
# Free-text feedback
|
| 84 |
+
feedback_text = st.text_area("Any additional feedback?", value=previous_feedback, key=f"feedback_{current_index}")
|
| 85 |
+
|
| 86 |
+
# Store or update the response
|
| 87 |
+
response = Response(q_id=f"q{current_index + 1}", ans=rating, feedback_text=feedback_text)
|
| 88 |
+
if len(st.session_state.responses) > current_index:
|
| 89 |
+
st.session_state.responses[current_index] = response
|
| 90 |
+
else:
|
| 91 |
+
st.session_state.responses.append(response)
|
| 92 |
|
| 93 |
# Navigation buttons
|
| 94 |
+
navigation_buttons(data, rating, feedback_text)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def navigation_buttons(data, rating, feedback_text):
|
| 98 |
+
"""Display navigation buttons."""
|
| 99 |
+
current_index = st.session_state.current_index
|
| 100 |
+
|
| 101 |
col1, col2, col3 = st.columns([1, 1, 2])
|
| 102 |
|
| 103 |
+
with col1: # Back button
|
| 104 |
if st.button("Back") and current_index > 0:
|
| 105 |
st.session_state.current_index -= 1
|
| 106 |
st.experimental_rerun()
|
| 107 |
|
| 108 |
+
with col2: # Next button
|
| 109 |
if st.button("Next"):
|
| 110 |
+
if not rating:
|
| 111 |
+
st.warning("Please provide a rating before proceeding.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
| 113 |
+
if current_index < len(data) - 1:
|
| 114 |
+
st.session_state.current_index += 1
|
| 115 |
+
st.experimental_rerun()
|
| 116 |
+
else:
|
| 117 |
+
feedback = Feedback(
|
| 118 |
+
id=current_index + 1,
|
| 119 |
+
user_id=st.session_state.prolific_id,
|
| 120 |
+
time_stamp=datetime.now(),
|
| 121 |
+
responses=st.session_state.responses,
|
| 122 |
+
)
|
| 123 |
+
try:
|
| 124 |
+
ingest(feedback)
|
| 125 |
+
st.session_state.completed = True
|
| 126 |
+
st.experimental_rerun()
|
| 127 |
+
except Exception as e:
|
| 128 |
+
st.error(f"An error occurred while submitting feedback: {e}")
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def reset_survey():
|
| 132 |
+
"""Reset the survey state to start over."""
|
| 133 |
+
st.session_state.prolific_id = None
|
| 134 |
+
st.session_state.current_index = 0
|
| 135 |
+
st.session_state.responses = []
|
| 136 |
+
st.session_state.completed = False
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def ui():
|
| 140 |
+
"""Main function to control the survey flow."""
|
| 141 |
+
data = load_data()
|
| 142 |
+
initialization()
|
| 143 |
+
|
| 144 |
+
if st.session_state.completed:
|
| 145 |
+
st.title("Survey Completed")
|
| 146 |
+
st.success("You have completed all questions and your answers have been recorded.")
|
| 147 |
+
if st.button("Retake Survey"):
|
| 148 |
+
reset_survey()
|
| 149 |
+
st.experimental_rerun()
|
| 150 |
+
return
|
| 151 |
+
|
| 152 |
+
if st.session_state.prolific_id is None:
|
| 153 |
+
prolific_id_screen()
|
| 154 |
+
else:
|
| 155 |
+
questions_screen(data)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
if __name__ == "__main__":
|
| 159 |
+
ui()
|
db/crud.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
from db.schema import Feedback
|
| 4 |
+
from db.setup import db_setup
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Create operation (Ingest data into Firebase with unique user_id check)
|
| 8 |
+
def ingest(data: Feedback):
|
| 9 |
+
ref = db_setup() # Ensure Firebase is initialized
|
| 10 |
+
|
| 11 |
+
# Check if the user_id already exists in the feedback data
|
| 12 |
+
existing_feedback = ref.child('feedback').order_by_child('user_id').equal_to(data.user_id).get()
|
| 13 |
+
|
| 14 |
+
# TODO: This should probably change. If the same user has multiple feedbacks, we should allow it. -> change to update
|
| 15 |
+
if existing_feedback:
|
| 16 |
+
print(f"Feedback from user '{data.user_id}' already exists. Ingestion aborted.")
|
| 17 |
+
else:
|
| 18 |
+
feedback_data = data.dict() # Convert Pydantic model to a dictionary
|
| 19 |
+
ref.child('feedback').push(feedback_data)
|
| 20 |
+
print(f"Feedback data from user '{data.user_id}' pushed to Firebase!")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Read operation (Fetch feedback data from Firebase)
|
| 24 |
+
def read(feedback_id: str):
|
| 25 |
+
ref = db_setup()
|
| 26 |
+
feedback_ref = ref.child('feedback').order_by_child('id').equal_to(feedback_id).get()
|
| 27 |
+
|
| 28 |
+
if feedback_ref:
|
| 29 |
+
return feedback_ref
|
| 30 |
+
else:
|
| 31 |
+
print("Feedback not found!")
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Update operation (Update feedback data in Firebase)
|
| 36 |
+
def update(feedback_id: int, updated_data: Feedback):
|
| 37 |
+
ref = db_setup()
|
| 38 |
+
feedback_ref = ref.child('feedback').order_by_child('id').equal_to(feedback_id).get()
|
| 39 |
+
|
| 40 |
+
if feedback_ref:
|
| 41 |
+
# Assuming we're updating the first entry found
|
| 42 |
+
for key in feedback_ref:
|
| 43 |
+
ref.child('feedback').child(key).update(updated_data.dict())
|
| 44 |
+
print("Feedback data updated in Firebase!")
|
| 45 |
+
else:
|
| 46 |
+
print("Feedback not found to update!")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Delete operation (Remove feedback data from Firebase)
|
| 50 |
+
def delete(feedback_id: int):
|
| 51 |
+
ref = db_setup()
|
| 52 |
+
feedback_ref = ref.child('feedback').order_by_child('id').equal_to(feedback_id).get()
|
| 53 |
+
|
| 54 |
+
if feedback_ref:
|
| 55 |
+
# Assuming we're deleting the first entry found
|
| 56 |
+
for key in feedback_ref:
|
| 57 |
+
ref.child('feedback').child(key).delete()
|
| 58 |
+
print("Feedback data deleted from Firebase!")
|
| 59 |
+
else:
|
| 60 |
+
print("Feedback not found to delete!")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test():
|
| 64 |
+
# Create a feedback object
|
| 65 |
+
feedback_example = Feedback(
|
| 66 |
+
id=1,
|
| 67 |
+
user_id="user1234",
|
| 68 |
+
time_stamp=datetime.now(),
|
| 69 |
+
responses=[
|
| 70 |
+
{"q_id": "q1", "ans": 5},
|
| 71 |
+
{"q_id": "q2", "ans": 3}
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Create (Ingest)
|
| 76 |
+
ingest(feedback_example)
|
| 77 |
+
|
| 78 |
+
# Read (Fetch)
|
| 79 |
+
feedback_data = read(1)
|
| 80 |
+
if feedback_data:
|
| 81 |
+
print(feedback_data)
|
| 82 |
+
|
| 83 |
+
# Update (Modify)
|
| 84 |
+
updated_feedback = Feedback(
|
| 85 |
+
id=1,
|
| 86 |
+
user_id="user123",
|
| 87 |
+
time_stamp=datetime.now(),
|
| 88 |
+
responses=[
|
| 89 |
+
{"q_id": "q1", "ans": 4}, # Updated answer
|
| 90 |
+
{"q_id": "q2", "ans": 3}
|
| 91 |
+
]
|
| 92 |
+
)
|
| 93 |
+
update(1, updated_feedback)
|
| 94 |
+
|
| 95 |
+
# Delete (Remove)
|
| 96 |
+
delete(1)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# Example usage
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
test()
|
db/schema.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from typing import List, Dict
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class Response(BaseModel):
|
| 7 |
+
q_id: str
|
| 8 |
+
ans: int
|
| 9 |
+
feedback_text: str | None = None
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Feedback(BaseModel):
|
| 13 |
+
id: int
|
| 14 |
+
user_id: str
|
| 15 |
+
time_stamp: datetime
|
| 16 |
+
responses: List[Response]
|
| 17 |
+
|
| 18 |
+
# Override the method to serialize datetime
|
| 19 |
+
def dict(self, *args, **kwargs):
|
| 20 |
+
feedback_data = super().dict(*args, **kwargs)
|
| 21 |
+
feedback_data['time_stamp'] = self.time_stamp.isoformat() # Convert datetime to ISO string
|
| 22 |
+
return feedback_data
|
db/setup.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import firebase_admin
|
| 5 |
+
from firebase_admin import db
|
| 6 |
+
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
databaseURL = os.environ["FIREBASE_DATABASE_URL"]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def decode_service_key():
|
| 14 |
+
encoded_key = os.environ["FIREBASE_CREDENTIALS"]
|
| 15 |
+
original_service_key = json.loads(base64.b64decode(encoded_key).decode('utf-8'))
|
| 16 |
+
if original_service_key:
|
| 17 |
+
return original_service_key
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# Firebase initialization
|
| 22 |
+
def initialize_firebase():
|
| 23 |
+
try:
|
| 24 |
+
# Try to initialize Firebase app if not already initialized
|
| 25 |
+
firebase_admin.get_app()
|
| 26 |
+
except ValueError:
|
| 27 |
+
creds_file_name = '.config/user-evaluations-firebase-creds.json'
|
| 28 |
+
if not (os.path.exists(creds_file_name) and os.path.isdir(creds_file_name)):
|
| 29 |
+
credentials = decode_service_key()
|
| 30 |
+
with open(creds_file_name, 'w', encoding='utf-8') as file:
|
| 31 |
+
json.dump(credentials, file, ensure_ascii=False, indent=4)
|
| 32 |
+
# If not initialized, initialize the default app
|
| 33 |
+
cred_obj = firebase_admin.credentials.Certificate(creds_file_name)
|
| 34 |
+
databaseURL = os.environ["FIREBASE_DATABASE_URL"]
|
| 35 |
+
firebase_admin.initialize_app(cred_obj, {'databaseURL': databaseURL})
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def db_setup():
|
| 39 |
+
initialize_firebase()
|
| 40 |
+
ref = db.reference('/')
|
| 41 |
+
return ref
|
requirements.txt
CHANGED
|
@@ -1,4 +1,8 @@
|
|
| 1 |
streamlit
|
| 2 |
pandas
|
| 3 |
openpyxl
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
streamlit
|
| 2 |
pandas
|
| 3 |
openpyxl
|
| 4 |
+
pyrebase
|
| 5 |
+
requests==2.11.1
|
| 6 |
+
python-dotenv
|
| 7 |
+
firebase-admin
|
| 8 |
+
pydantic
|