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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!pip install sqlite3 # Install the sqlite3 library if not already installed
|
| 2 |
+
import sqlite3
|
| 3 |
+
|
| 4 |
+
# Define the database file path
|
| 5 |
+
DB_FILE = "./reviews.db"
|
| 6 |
+
# Connect to the SQLite database
|
| 7 |
+
db = sqlite3.connect(DB_FILE)
|
| 8 |
+
|
| 9 |
+
# Attempt to create the 'reviews' table if it doesn't exist
|
| 10 |
+
try:
|
| 11 |
+
# Try to select all rows from the 'reviews' table
|
| 12 |
+
db.execute("SELECT * FROM reviews").fetchall()
|
| 13 |
+
# Close the database connection if the table exists
|
| 14 |
+
db.close()
|
| 15 |
+
except sqlite3.OperationalError:
|
| 16 |
+
# If the table doesn't exist, create it
|
| 17 |
+
db.execute(
|
| 18 |
+
'''
|
| 19 |
+
CREATE TABLE reviews (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
| 20 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
| 21 |
+
name TEXT, review INTEGER, comments TEXT)
|
| 22 |
+
'''
|
| 23 |
+
)
|
| 24 |
+
# Commit the changes to the database
|
| 25 |
+
db.commit()
|
| 26 |
+
# Close the database connection
|
| 27 |
+
db.close()
|
| 28 |
+
|
| 29 |
+
# Function to retrieve the latest reviews from the database
|
| 30 |
+
def get_latest_reviews(db: sqlite3.Connection):
|
| 31 |
+
# Execute a query to get the 10 most recent reviews, ordered by id descending
|
| 32 |
+
reviews = db.execute("SELECT * FROM reviews ORDER BY id DESC limit 10").fetchall()
|
| 33 |
+
# Get the total number of reviews in the database
|
| 34 |
+
total_reviews = db.execute("Select COUNT(id) from reviews").fetchone()[0]
|
| 35 |
+
# Convert the reviews to a pandas DataFrame for easier handling
|
| 36 |
+
reviews = pd.DataFrame(
|
| 37 |
+
reviews,
|
| 38 |
+
columns=["id", "date_created", "name", "review", "comments"]
|
| 39 |
+
)
|
| 40 |
+
# Return the reviews DataFrame and the total number of reviews
|
| 41 |
+
return reviews, total_reviews
|
| 42 |
+
|
| 43 |
+
# Function to add a new review to the database
|
| 44 |
+
def add_review(name: str, review: int, comments: str):
|
| 45 |
+
# Connect to the database
|
| 46 |
+
db = sqlite3.connect(DB_FILE)
|
| 47 |
+
# Create a cursor object to execute SQL commands
|
| 48 |
+
cursor = db.cursor()
|
| 49 |
+
# Insert the new review into the database
|
| 50 |
+
cursor.execute("INSERT INTO reviews(name, review, comments) VALUES(?,?,?)",
|
| 51 |
+
[name, review, comments])
|
| 52 |
+
# Commit the changes to the database
|
| 53 |
+
db.commit()
|
| 54 |
+
# Retrieve the updated list of reviews and total count
|
| 55 |
+
reviews, total_reviews = get_latest_reviews(db)
|
| 56 |
+
# Close the database connection
|
| 57 |
+
db.close()
|
| 58 |
+
# Return the updated reviews and total count
|
| 59 |
+
return reviews, total_reviews
|
| 60 |
+
|
| 61 |
+
def load_data():
|
| 62 |
+
db = sqlite3.connect(DB_FILE)
|
| 63 |
+
reviews, total_reviews = get_latest_reviews(db)
|
| 64 |
+
db.close()
|
| 65 |
+
return reviews, total_reviews
|
| 66 |
+
|
| 67 |
+
import gradio as gr
|
| 68 |
+
|
| 69 |
+
# Create a Gradio Blocks interface
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
# Create a row to organize elements horizontally
|
| 72 |
+
with gr.Row():
|
| 73 |
+
# Create a column for input elements
|
| 74 |
+
with gr.Column():
|
| 75 |
+
# Create a text input for the user's name
|
| 76 |
+
name = gr.Textbox(label="Name", placeholder="What is your name?")
|
| 77 |
+
# Create a radio button group for rating satisfaction
|
| 78 |
+
review = gr.Radio(label="How satisfied are you with using gradio?",
|
| 79 |
+
choices=[1, 2, 3, 4, 5])
|
| 80 |
+
# Create a multi-line text input for comments
|
| 81 |
+
comments = gr.Textbox(
|
| 82 |
+
label="Comments",
|
| 83 |
+
lines=10,
|
| 84 |
+
placeholder="Do you have any feedback on gradio?"
|
| 85 |
+
)
|
| 86 |
+
# Create a submit button
|
| 87 |
+
submit = gr.Button(value="Submit Feedback")
|
| 88 |
+
# Create a column for output elements
|
| 89 |
+
with gr.Column():
|
| 90 |
+
# Create a dataframe to display the most recent 10 reviews
|
| 91 |
+
data = gr.Dataframe(label="Most recently created 10 rows")
|
| 92 |
+
# Create a number display for the total review count
|
| 93 |
+
count = gr.Number(label="Total number of reviews")
|
| 94 |
+
# Define the action when the submit button is clicked
|
| 95 |
+
submit.click(add_review,
|
| 96 |
+
[name, review, comments],
|
| 97 |
+
[data, count])
|
| 98 |
+
# Define the action when the demo is loaded
|
| 99 |
+
demo.load(load_data, None, [data, count])
|
| 100 |
+
|
| 101 |
+
from google.colab import userdata
|
| 102 |
+
TOKEN = userdata.get('token')
|
| 103 |
+
|
| 104 |
+
# Retrieve the Hugging Face Hub token from environment variables
|
| 105 |
+
TOKEN = TOKEN
|
| 106 |
+
|
| 107 |
+
# Create a Repository object for interacting with a Hugging Face dataset
|
| 108 |
+
repo = huggingface_hub.Repository(
|
| 109 |
+
# Specify the local directory where the repository will be cloned
|
| 110 |
+
local_dir="data",
|
| 111 |
+
# Set the repository type to "dataset"
|
| 112 |
+
repo_type="dataset",
|
| 113 |
+
# Specify the name of the dataset to clone from Hugging Face Hub
|
| 114 |
+
clone_from="Kilos1/my-reviews",
|
| 115 |
+
# Use the authentication token for accessing the repository
|
| 116 |
+
use_auth_token=TOKEN
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Pull the latest changes from the remote repository
|
| 120 |
+
repo.git_pull()
|
| 121 |
+
|
| 122 |
+
# Check if the file exists in the expected location
|
| 123 |
+
import os
|
| 124 |
+
if os.path.exists("./data/reviews.db"):
|
| 125 |
+
# Copy the reviews database file from the cloned repository to the local DB_FILE location
|
| 126 |
+
shutil.copyfile("./data/reviews.db", DB_FILE)
|
| 127 |
+
else:
|
| 128 |
+
print("File 'reviews.db' not found in the repository. Please check the file path.")
|
| 129 |
+
# If the file is not in the expected location, you may need to adjust the path
|
| 130 |
+
# based on its actual location in the repository.
|
| 131 |
+
# For example, if the file is in a subdirectory called 'database', you would use:
|
| 132 |
+
# shutil.copyfile("./data/database/reviews.db", DB_FILE)
|
| 133 |
+
|
| 134 |
+
# Import the BackgroundScheduler from APScheduler library
|
| 135 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 136 |
+
import pandas as pd
|
| 137 |
+
import datetime
|
| 138 |
+
|
| 139 |
+
# Define a function to backup the database
|
| 140 |
+
def backup_db():
|
| 141 |
+
# Copy the current database file to the data directory
|
| 142 |
+
shutil.copyfile(DB_FILE, "./data/reviews.db")
|
| 143 |
+
|
| 144 |
+
# Connect to the database
|
| 145 |
+
db = sqlite3.connect(DB_FILE)
|
| 146 |
+
|
| 147 |
+
# Fetch all reviews from the database
|
| 148 |
+
reviews = db.execute("SELECT * FROM reviews").fetchall()
|
| 149 |
+
|
| 150 |
+
# Convert the reviews to a pandas DataFrame and save as CSV
|
| 151 |
+
pd.DataFrame(reviews).to_csv("./data/reviews.csv", index=False)
|
| 152 |
+
|
| 153 |
+
# Print a message indicating the update is in progress
|
| 154 |
+
print("updating db")
|
| 155 |
+
|
| 156 |
+
# Push the updated data to the Hugging Face Hub
|
| 157 |
+
repo.push_to_hub(blocking=False,
|
| 158 |
+
commit_message=f"Updating data at {datetime.datetime.now()}")
|
| 159 |
+
|
| 160 |
+
# Create a BackgroundScheduler instance
|
| 161 |
+
scheduler = BackgroundScheduler()
|
| 162 |
+
|
| 163 |
+
# Add a job to run the backup_db function every 60 seconds
|
| 164 |
+
scheduler.add_job(func=backup_db,
|
| 165 |
+
trigger="interval",
|
| 166 |
+
seconds=60)
|
| 167 |
+
# Start the scheduler
|
| 168 |
+
scheduler.start()
|
| 169 |
+
|
| 170 |
+
demo.launch()
|