Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNjvvrVU1glxVTod7TnMpG+"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["#**Step 1 - Write your database logic πΎ**#\n","##Our application will store the name of the reviewer, their rating of gradio on a scale of 1 to 5, as well as any comments they want to share about the library. Let's write some code that creates a database table to store this data. We'll also write some functions to insert a review into that table and fetch the latest 10 reviews.\n","\n","##We're going to use the sqlite3 library to connect to our sqlite database but gradio will work with any library.\n","\n","##The code will look like this:"],"metadata":{"id":"9bLteRezPZZA"}},{"cell_type":"code","source":["#!pip install sqlite3 # Install the sqlite3 library if not already installed\n","import sqlite3"],"metadata":{"id":"WZKJ1OSLUQ1D"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"dt02qGJnO27O"},"outputs":[],"source":["# Define the database file path\n","DB_FILE = \"./reviews.db\"\n","# Connect to the SQLite database\n","db = sqlite3.connect(DB_FILE)\n","\n","# Attempt to create the 'reviews' table if it doesn't exist\n","try:\n"," # Try to select all rows from the 'reviews' table\n"," db.execute(\"SELECT * FROM reviews\").fetchall()\n"," # Close the database connection if the table exists\n"," db.close()\n","except sqlite3.OperationalError:\n"," # If the table doesn't exist, create it\n"," db.execute(\n"," '''\n"," CREATE TABLE reviews (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"," created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,\n"," name TEXT, review INTEGER, comments TEXT)\n"," '''\n"," )\n"," # Commit the changes to the database\n"," db.commit()\n"," # Close the database connection\n"," db.close()\n","\n","# Function to retrieve the latest reviews from the database\n","def get_latest_reviews(db: sqlite3.Connection):\n"," # Execute a query to get the 10 most recent reviews, ordered by id descending\n"," reviews = db.execute(\"SELECT * FROM reviews ORDER BY id DESC limit 10\").fetchall()\n"," # Get the total number of reviews in the database\n"," total_reviews = db.execute(\"Select COUNT(id) from reviews\").fetchone()[0]\n"," # Convert the reviews to a pandas DataFrame for easier handling\n"," reviews = pd.DataFrame(\n"," reviews,\n"," columns=[\"id\", \"date_created\", \"name\", \"review\", \"comments\"]\n"," )\n"," # Return the reviews DataFrame and the total number of reviews\n"," return reviews, total_reviews\n","\n","# Function to add a new review to the database\n","def add_review(name: str, review: int, comments: str):\n"," # Connect to the database\n"," db = sqlite3.connect(DB_FILE)\n"," # Create a cursor object to execute SQL commands\n"," cursor = db.cursor()\n"," # Insert the new review into the database\n"," cursor.execute(\"INSERT INTO reviews(name, review, comments) VALUES(?,?,?)\",\n"," [name, review, comments])\n"," # Commit the changes to the database\n"," db.commit()\n"," # Retrieve the updated list of reviews and total count\n"," reviews, total_reviews = get_latest_reviews(db)\n"," # Close the database connection\n"," db.close()\n"," # Return the updated reviews and total count\n"," return reviews, total_reviews"]},{"cell_type":"markdown","source":["##Let's also write a function to load the latest reviews when the gradio application loads:"],"metadata":{"id":"Ot6tq1CVVfr6"}},{"cell_type":"code","source":["def load_data():\n"," db = sqlite3.connect(DB_FILE)\n"," reviews, total_reviews = get_latest_reviews(db)\n"," db.close()\n"," return reviews, total_reviews"],"metadata":{"id":"C6azHtZyVpVQ"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["#**Step 2 - Create a gradio app β‘**#\n","##Now that we have our database logic defined, we can use gradio create a dynamic web page to ask our users for feedback!"],"metadata":{"id":"BFgYfvSLWG0B"}},{"cell_type":"code","source":["!pip install gradio"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"collapsed":true,"id":"3WatoAWIW1YO","executionInfo":{"status":"ok","timestamp":1719821664202,"user_tz":-120,"elapsed":26248,"user":{"displayName":"KGAOGELO Moloko","userId":"02299502165535871727"}},"outputId":"091e25d4-d104-456e-9e7a-d3c37e215a41"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting gradio\n"," Downloading gradio-4.37.2-py3-none-any.whl (12.3 MB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m12.3/12.3 MB\u001b[0m \u001b[31m41.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting aiofiles<24.0,>=22.0 (from gradio)\n"," Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n","Requirement already satisfied: altair<6.0,>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.2.2)\n","Collecting fastapi (from gradio)\n"," Downloading fastapi-0.111.0-py3-none-any.whl (91 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m92.0/92.0 kB\u001b[0m \u001b[31m10.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting ffmpy (from gradio)\n"," Downloading ffmpy-0.3.2.tar.gz (5.5 kB)\n"," Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n","Collecting gradio-client==1.0.2 (from gradio)\n"," Downloading gradio_client-1.0.2-py3-none-any.whl (318 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m318.2/318.2 kB\u001b[0m \u001b[31m29.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting httpx>=0.24.1 (from gradio)\n"," Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: huggingface-hub>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.4)\n","Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.4.0)\n","Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.4)\n","Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.5)\n","Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n","Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.25.2)\n","Collecting orjson~=3.0 (from gradio)\n"," Downloading orjson-3.10.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (144 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m145.0/145.0 kB\u001b[0m \u001b[31m15.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (24.1)\n","Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.3)\n","Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n","Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.7.4)\n","Collecting pydub (from gradio)\n"," Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n","Collecting python-multipart>=0.0.9 (from gradio)\n"," Downloading python_multipart-0.0.9-py3-none-any.whl (22 kB)\n","Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n","Collecting ruff>=0.2.2 (from gradio)\n"," Downloading ruff-0.5.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.1 MB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m10.1/10.1 MB\u001b[0m \u001b[31m54.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting semantic-version~=2.0 (from gradio)\n"," Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n","Collecting tomlkit==0.12.0 (from gradio)\n"," Downloading tomlkit-0.12.0-py3-none-any.whl (37 kB)\n","Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.3)\n","Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.12.2)\n","Requirement already satisfied: urllib3~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.7)\n","Collecting uvicorn>=0.14.0 (from gradio)\n"," Downloading uvicorn-0.30.1-py3-none-any.whl (62 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m62.4/62.4 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.0.2->gradio) (2023.6.0)\n","Collecting websockets<12.0,>=10.0 (from gradio-client==1.0.2->gradio)\n"," Downloading websockets-11.0.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (129 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m129.9/129.9 kB\u001b[0m \u001b[31m14.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.4)\n","Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (4.19.2)\n","Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.12.1)\n","Requirement already satisfied: anyio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (3.7.1)\n","Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (2024.6.2)\n","Collecting httpcore==1.* (from httpx>=0.24.1->gradio)\n"," Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (3.7)\n","Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (1.3.1)\n","Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx>=0.24.1->gradio)\n"," Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (3.15.3)\n","Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n","Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (4.66.4)\n","Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.2.1)\n","Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1)\n","Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.53.0)\n","Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n","Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.2)\n","Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.4)\n","Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n","Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n","Requirement already satisfied: pydantic-core==2.18.4 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (2.18.4)\n","Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n","Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n","Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n","Collecting starlette<0.38.0,>=0.37.2 (from fastapi->gradio)\n"," Downloading starlette-0.37.2-py3-none-any.whl (71 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting fastapi-cli>=0.0.2 (from fastapi->gradio)\n"," Downloading fastapi_cli-0.0.4-py3-none-any.whl (9.5 kB)\n","Collecting ujson!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0,>=4.0.1 (from fastapi->gradio)\n"," Downloading ujson-5.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (53 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m53.6/53.6 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting email_validator>=2.0.0 (from fastapi->gradio)\n"," Downloading email_validator-2.2.0-py3-none-any.whl (33 kB)\n","Collecting dnspython>=2.0.0 (from email_validator>=2.0.0->fastapi->gradio)\n"," Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m307.7/307.7 kB\u001b[0m \u001b[31m28.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.2.0)\n","Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.12.1)\n","Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.35.1)\n","Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.18.1)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n","Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n","Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.16.1)\n","Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio->httpx>=0.24.1->gradio) (1.2.1)\n","Collecting httptools>=0.5.0 (from uvicorn>=0.14.0->gradio)\n"," Downloading httptools-0.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341 kB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m32.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting python-dotenv>=0.13 (from uvicorn>=0.14.0->gradio)\n"," Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n","Collecting uvloop!=0.15.0,!=0.15.1,>=0.14.0 (from uvicorn>=0.14.0->gradio)\n"," Downloading uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m88.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting watchfiles>=0.13 (from uvicorn>=0.14.0->gradio)\n"," Downloading watchfiles-0.22.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)\n","\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m63.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.2)\n","Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n","Building wheels for collected packages: ffmpy\n"," Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n"," Created wheel for ffmpy: filename=ffmpy-0.3.2-py3-none-any.whl size=5584 sha256=34d8e63296df62162bd2c44824f4b2b0081666df75d0458e744acb9cbdcb0258\n"," Stored in directory: /root/.cache/pip/wheels/bd/65/9a/671fc6dcde07d4418df0c592f8df512b26d7a0029c2a23dd81\n","Successfully built ffmpy\n","Installing collected packages: pydub, ffmpy, websockets, uvloop, ujson, tomlkit, semantic-version, ruff, python-multipart, python-dotenv, orjson, httptools, h11, dnspython, aiofiles, watchfiles, uvicorn, starlette, httpcore, email_validator, httpx, gradio-client, fastapi-cli, fastapi, gradio\n","Successfully installed aiofiles-23.2.1 dnspython-2.6.1 email_validator-2.2.0 fastapi-0.111.0 fastapi-cli-0.0.4 ffmpy-0.3.2 gradio-4.37.2 gradio-client-1.0.2 h11-0.14.0 httpcore-1.0.5 httptools-0.6.1 httpx-0.27.0 orjson-3.10.5 pydub-0.25.1 python-dotenv-1.0.1 python-multipart-0.0.9 ruff-0.5.0 semantic-version-2.10.0 starlette-0.37.2 tomlkit-0.12.0 ujson-5.10.0 uvicorn-0.30.1 uvloop-0.19.0 watchfiles-0.22.0 websockets-11.0.3\n"]}]},{"cell_type":"code","source":["import gradio as gr\n","\n","# Create a Gradio Blocks interface\n","with gr.Blocks() as demo:\n"," # Create a row to organize elements horizontally\n"," with gr.Row():\n"," # Create a column for input elements\n"," with gr.Column():\n"," # Create a text input for the user's name\n"," name = gr.Textbox(label=\"Name\", placeholder=\"What is your name?\")\n"," # Create a radio button group for rating satisfaction\n"," review = gr.Radio(label=\"How satisfied are you with using gradio?\",\n"," choices=[1, 2, 3, 4, 5])\n"," # Create a multi-line text input for comments\n"," comments = gr.Textbox(\n"," label=\"Comments\",\n"," lines=10,\n"," placeholder=\"Do you have any feedback on gradio?\"\n"," )\n"," # Create a submit button\n"," submit = gr.Button(value=\"Submit Feedback\")\n"," # Create a column for output elements\n"," with gr.Column():\n"," # Create a dataframe to display the most recent 10 reviews\n"," data = gr.Dataframe(label=\"Most recently created 10 rows\")\n"," # Create a number display for the total review count\n"," count = gr.Number(label=\"Total number of reviews\")\n"," # Define the action when the submit button is clicked\n"," submit.click(add_review,\n"," [name, review, comments],\n"," [data, count])\n"," # Define the action when the demo is loaded\n"," demo.load(load_data, None, [data, count])\n","\n"],"metadata":{"id":"ULouvY3QXC4H"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["#**Step 3 - Synchronize with HuggingFace Datasets π€**#\n","##We could call demo.launch() after step 2 and have a fully functioning application. However, our data would be stored locally on our machine. If the sqlite file were accidentally deleted, we'd lose all of our reviews! Let's back up our data to a dataset on the HuggingFace hub.\n","\n","##Create a dataset here before proceeding.\n","\n","##Now at the top of our script, we'll use the huggingface hub client library to connect to our dataset and pull the latest backup."],"metadata":{"id":"sIxG4Ds7antR"}},{"cell_type":"code","source":["!pip install huggingface_hub\n","import huggingface_hub"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"collapsed":true,"id":"7vXNxL7jerrI","executionInfo":{"status":"ok","timestamp":1719823771432,"user_tz":-120,"elapsed":8853,"user":{"displayName":"KGAOGELO Moloko","userId":"02299502165535871727"}},"outputId":"90587a4b-a450-43b6-ae1f-ef75517193c2"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.10/dist-packages (0.23.4)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (3.15.3)\n","Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (2023.6.0)\n","Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (24.1)\n","Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (6.0.1)\n","Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (2.31.0)\n","Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (4.66.4)\n","Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface_hub) (4.12.2)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (3.3.2)\n","Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (3.7)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (2.0.7)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface_hub) (2024.6.2)\n"]}]},{"cell_type":"code","source":["import shutil"],"metadata":{"id":"2aF2bIaXlqlG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Retrieve the Hugging Face Hub token from environment variables\n","TOKEN = TOKEN\n","\n","# Create a Repository object for interacting with a Hugging Face dataset\n","repo = huggingface_hub.Repository(\n"," # Specify the local directory where the repository will be cloned\n"," local_dir=\"data\",\n"," # Set the repository type to \"dataset\"\n"," repo_type=\"dataset\",\n"," # Specify the name of the dataset to clone from Hugging Face Hub\n"," clone_from=\"Kilos1/my-reviews\",\n"," # Use the authentication token for accessing the repository\n"," use_auth_token=TOKEN\n",")\n","\n","# Pull the latest changes from the remote repository\n","repo.git_pull()\n","\n","# Copy the reviews database file from the cloned repository to the local DB_FILE location\n","shutil.copyfile(\"./data/reviews.db\", DB_FILE)\n"],"metadata":{"id":"qVrTMscpcpPS"},"execution_count":null,"outputs":[]},{"source":["# Retrieve the Hugging Face Hub token from environment variables\n","TOKEN = openai_api_key\n","\n","# Create a Repository object for interacting with a Hugging Face dataset\n","repo = huggingface_hub.Repository(\n"," # Specify the local directory where the repository will be cloned\n"," local_dir=\"data\",\n"," # Set the repository type to \"dataset\"\n"," repo_type=\"dataset\",\n"," # Specify the name of the dataset to clone from Hugging Face Hub\n"," clone_from=\"Kilos1/my-reviews\",\n"," # Use the authentication token for accessing the repository\n"," use_auth_token=TOKEN\n",")\n","\n","# Pull the latest changes from the remote repository\n","repo.git_pull()\n","\n","# Check if the file exists in the expected location\n","import os\n","if os.path.exists(\"./data/reviews.db\"):\n"," # Copy the reviews database file from the cloned repository to the local DB_FILE location\n"," shutil.copyfile(\"./data/reviews.db\", DB_FILE)\n","else:\n"," print(\"File 'reviews.db' not found in the repository. Please check the file path.\")\n"," # If the file is not in the expected location, you may need to adjust the path\n"," # based on its actual location in the repository.\n"," # For example, if the file is in a subdirectory called 'database', you would use:\n"," # shutil.copyfile(\"./data/database/reviews.db\", DB_FILE)"],"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"9K8JibthmH4k","executionInfo":{"status":"ok","timestamp":1719825636014,"user_tz":-120,"elapsed":2523,"user":{"displayName":"KGAOGELO Moloko","userId":"02299502165535871727"}},"outputId":"73ab68fe-dd5a-420c-a4fc-807ee9b54f71"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_deprecation.py:131: FutureWarning: 'Repository' (from 'huggingface_hub.repository') is deprecated and will be removed from version '1.0'. Please prefer the http-based alternatives instead. Given its large adoption in legacy code, the complete removal is only planned on next major release.\n","For more details, please read https://huggingface.co/docs/huggingface_hub/concepts/git_vs_http.\n"," warnings.warn(warning_message, FutureWarning)\n","/content/data is already a clone of https://huggingface.co/datasets/Kilos1/my-reviews. Make sure you pull the latest changes with `repo.git_pull()`.\n","WARNING:huggingface_hub.repository:/content/data is already a clone of https://huggingface.co/datasets/Kilos1/my-reviews. Make sure you pull the latest changes with `repo.git_pull()`.\n"]},{"output_type":"stream","name":"stdout","text":["File 'reviews.db' not found in the repository. Please check the file path.\n"]}]},{"cell_type":"markdown","source":["##Now we will create a background task to synch our local database to the dataset hub every 60 seconds. We will use the `AdvancedPythonScheduler` to handle the scheduling. However, this is not the only task scheduling library available. Feel free to use whatever you are comfortable with.\n","\n","##The function to back up our data will look like this:"],"metadata":{"id":"Y2hCpePRmk07"}},{"cell_type":"code","source":["from google.colab import userdata\n","TOKEN = userdata.get('token')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NioaesGn9Eoq","executionInfo":{"status":"ok","timestamp":1719831723097,"user_tz":-120,"elapsed":3277,"user":{"displayName":"KGAOGELO Moloko","userId":"02299502165535871727"}},"outputId":"70a428b0-17b5-4e77-8050-02229684ddcf"},"execution_count":33,"outputs":[{"output_type":"stream","name":"stdout","text":["updating db\n"]}]},{"cell_type":"code","source":["!pip install apscheduler"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"collapsed":true,"id":"Od22i3gGobZ0","executionInfo":{"status":"ok","timestamp":1719826257148,"user_tz":-120,"elapsed":7576,"user":{"displayName":"KGAOGELO Moloko","userId":"02299502165535871727"}},"outputId":"d98d4e29-3581-4694-c12b-50819a803f74"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting apscheduler\n"," Downloading APScheduler-3.10.4-py3-none-any.whl (59 kB)\n","\u001b[?25l \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m0.0/59.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91mββββββββββββββββββββ\u001b[0m\u001b[91mβΈ\u001b[0m\u001b[90mβββββββββββββββββββ\u001b[0m \u001b[32m30.7/59.3 kB\u001b[0m \u001b[31m642.8 kB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m59.3/59.3 kB\u001b[0m \u001b[31m788.5 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: six>=1.4.0 in /usr/local/lib/python3.10/dist-packages (from apscheduler) (1.16.0)\n","Requirement already satisfied: pytz in /usr/local/lib/python3.10/dist-packages (from apscheduler) (2023.4)\n","Requirement already satisfied: tzlocal!=3.*,>=2.0 in /usr/local/lib/python3.10/dist-packages (from apscheduler) (5.2)\n","Installing collected packages: apscheduler\n","Successfully installed apscheduler-3.10.4\n"]}]},{"cell_type":"code","source":["# Import the BackgroundScheduler from APScheduler library\n","from apscheduler.schedulers.background import BackgroundScheduler\n","import pandas as pd\n","import datetime\n","\n","# Define a function to backup the database\n","def backup_db():\n"," # Copy the current database file to the data directory\n"," shutil.copyfile(DB_FILE, \"./data/reviews.db\")\n","\n"," # Connect to the database\n"," db = sqlite3.connect(DB_FILE)\n","\n"," # Fetch all reviews from the database\n"," reviews = db.execute(\"SELECT * FROM reviews\").fetchall()\n","\n"," # Convert the reviews to a pandas DataFrame and save as CSV\n"," pd.DataFrame(reviews).to_csv(\"./data/reviews.csv\", index=False)\n","\n"," # Print a message indicating the update is in progress\n"," print(\"updating db\")\n","\n"," # Push the updated data to the Hugging Face Hub\n"," repo.push_to_hub(blocking=False,\n"," commit_message=f\"Updating data at {datetime.datetime.now()}\")\n","\n","# Create a BackgroundScheduler instance\n","scheduler = BackgroundScheduler()\n","\n","# Add a job to run the backup_db function every 60 seconds\n","scheduler.add_job(func=backup_db,\n"," trigger=\"interval\",\n"," seconds=60)\n","# Start the scheduler\n","scheduler.start()\n","\n","demo.launch()"],"metadata":{"id":"dgl_rSEeoWgy","executionInfo":{"status":"ok","timestamp":1719829393815,"user_tz":-120,"elapsed":2139,"user":{"displayName":"KGAOGELO Moloko","userId":"02299502165535871727"}},"outputId":"65c8dec1-1d8b-416a-9757-bf75afaea088","colab":{"base_uri":"https://localhost:8080/","height":646}},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n","\n","Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n","Running on public URL: https://527568b4302aef5b7b.gradio.live\n","\n","This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<div><iframe src=\"https://527568b4302aef5b7b.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"]},"metadata":{}},{"output_type":"execute_result","data":{"text/plain":[]},"metadata":{},"execution_count":31}]}]}
|
|
|
|
|
|