-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from RobotSail/feedback-updates
add initial feedback code
- Loading branch information
Showing
7 changed files
with
254 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import logging | ||
import sqlite3 | ||
import os | ||
import time | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_db_file() -> str: | ||
""" | ||
Returns either the database location that was provided | ||
via the FEEDBACK_DB environment variable, or returns | ||
a default location. | ||
""" | ||
db_file = os.getenv("FEEDBACK_DB") | ||
if not db_file: | ||
db_file = "/tmp/feedback.db" | ||
logger.warning( | ||
"No feedback database file specified, storing feedback in default location: '%s'", | ||
db_file, | ||
) | ||
return db_file | ||
|
||
|
||
def create_connection() -> sqlite3.Connection | None: | ||
""" | ||
Creates connection with the Sqlite database. | ||
""" | ||
|
||
db_file = get_db_file() | ||
conn = None | ||
try: | ||
conn = sqlite3.connect(db_file) | ||
except sqlite3.Error as e: | ||
logger.error(e) | ||
return conn | ||
|
||
|
||
def create_table(conn: sqlite3.Connection): | ||
""" | ||
Creates the feedback table if it does not exist. | ||
""" | ||
try: | ||
sql_create_feedback_table = """ CREATE TABLE IF NOT EXISTS feedback ( | ||
id integer PRIMARY KEY AUTOINCREMENT, | ||
type text NOT NULL, | ||
score text NOT NULL, | ||
text text, | ||
timestamp text NOT NULL | ||
); """ | ||
c = conn.cursor() | ||
c.execute(sql_create_feedback_table) | ||
except sqlite3.Error as e: | ||
logger.error(e) | ||
|
||
|
||
def store_feedback(feedback: dict) -> None: | ||
""" | ||
Stores feedback from the provided feedback dict, which | ||
comes from the streamlit-feedback component. | ||
If the database does not exist, it will be created. | ||
""" | ||
logger.info("Got feedback: %s", str(feedback)) | ||
conn = create_connection() | ||
if conn is not None: | ||
create_table(conn) | ||
sql = """ INSERT INTO feedback(type,score,text,timestamp) | ||
VALUES(?,?,?,?) """ | ||
cur = conn.cursor() | ||
logger.debug("Storing feedback in database.") | ||
cur.execute( | ||
sql, | ||
( | ||
feedback["type"], | ||
feedback["score"], | ||
feedback.get("text", ""), | ||
time.ctime(), | ||
), | ||
) | ||
conn.commit() | ||
logger.debug("Feedback stored in database.") | ||
else: | ||
logger.error("Error! cannot create the database connection.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ py-readability-metrics | |
openai | ||
textstat | ||
scikit-learn | ||
streamlit-feedback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: user-feedback-db | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 1Gi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Analyzing Feedback\n", | ||
"\n", | ||
"This Jupyter notebook demonstrates how to extract\n", | ||
"user feedback from the database we created earlier.\n", | ||
"\n", | ||
"The data is stored in a table which was created with the\n", | ||
"following schema:\n", | ||
"\n", | ||
"```sql\n", | ||
"CREATE TABLE IF NOT EXISTS feedback (\n", | ||
"\tid integer PRIMARY KEY AUTOINCREMENT,\n", | ||
"\ttype text NOT NULL,\n", | ||
"\tscore text NOT NULL,\n", | ||
"\ttext text,\n", | ||
"\ttimestamp text NOT NULL\n", | ||
");\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import sqlite3\n", | ||
"import os" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_db_file() -> str:\n", | ||
" db_file = os.getenv(\"FEEDBACK_DB\")\n", | ||
" if not db_file:\n", | ||
" db_file = \"/tmp/feedback.db\"\n", | ||
" return db_file\n", | ||
"\n", | ||
"\n", | ||
"def create_connection() -> sqlite3.Connection | None:\n", | ||
" \"\"\"\n", | ||
" Creates connection with the Sqlite database.\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" db_file = get_db_file()\n", | ||
" conn = None\n", | ||
" try:\n", | ||
" conn = sqlite3.connect(db_file)\n", | ||
" except sqlite3.Error as e:\n", | ||
" print(e)\n", | ||
" return conn" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"conn = create_connection()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(4, 'thumbs', '👍', 'The API response is already perfect 👍', 'Tue Jan 16 17:22:09 2024')\n", | ||
"(5, 'thumbs', '👎', 'The API was way too long', 'Tue Jan 16 17:22:50 2024')\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"conn = create_connection()\n", | ||
"\n", | ||
"# Execute the SELECT * query\n", | ||
"cursor = conn.cursor()\n", | ||
"cursor.execute(\"SELECT * FROM feedback\")\n", | ||
"\n", | ||
"# Fetch all rows from the result set\n", | ||
"rows = cursor.fetchall()\n", | ||
"\n", | ||
"# Print the rows\n", | ||
"for row in rows:\n", | ||
" print(row)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Close the cursor and connection\n", | ||
"cursor.close()\n", | ||
"conn.close()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "venv", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |