Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fasthtml demo-app #1446

Merged
merged 13 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/link-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
ignore-urls: |-
src/main/application/schemas/wiki.sd#L80
/localhost:8080/
/localhost:5001/
src/main/application/search/query-profiles
/hub.docker.com/r/vespaengine/vespa/tags/
swap-urls: |-
Expand Down
8 changes: 8 additions & 0 deletions examples/fasthtml-demo/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The URL of your Vespa Cloud application (you get it when running `deploy_app.ipynb`)
VESPA_APP_URL=https://my-vespa-endpoint.z.vespa-app.cloud/
# The secret token for your Vespa Cloud application (Created in Vespa Cloud Console)
VESPA_CLOUD_SECRET_TOKEN=vespa_cloud_mysecret_tokenabcdeabcdeabcdedcba
# Username of admin user (has access to query logs in SQLite DB)
ADMIN_NAME=admin
# Password of admin user
ADMIN_PWD=admin
15 changes: 15 additions & 0 deletions examples/fasthtml-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ft/
db/
session/
todo/
fasthtml/
.venv/
__pycache__/
.python-version
# Python stuff
*.pyc
*.pyo


.env
.sesskey
10 changes: 10 additions & 0 deletions examples/fasthtml-demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.10
WORKDIR /code
COPY --link --chown=1000 . .
RUN mkdir -p /tmp/cache/ session/ db/
RUN chmod a+rwx -R /tmp/cache/ session/ db/
ENV HF_HUB_CACHE=HF_HOME
RUN pip install --no-cache-dir -r requirements.txt

ENV PYTHONUNBUFFERED=1 PORT=7860
CMD ["python", "main.py"]
16 changes: 16 additions & 0 deletions examples/fasthtml-demo/Dockerfile.nonhf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.10
# Set working directory
WORKDIR /code
# Copy only the requirements file to leverage Docker cache
COPY --chown=1000 requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY --chown=1000 . .
# Create necessary directories with appropriate permissions
RUN mkdir -p /tmp/cache/ session/ \
&& chmod a+rwx -R /tmp/cache/ session/
# Set environment variable
ENV PYTHONUNBUFFERED=1
# Define the command to run the application
CMD ["python", "main.py"]
141 changes: 141 additions & 0 deletions examples/fasthtml-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: vespa-engine/fasthtml-vespa
emoji: 🚀
colorFrom: purple
colorTo: red
sdk: docker
app_file: main.py
pinned: false
termination_grace_period: 2m
---
<!-- Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->

<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://assets.vespa.ai/logos/Vespa-logo-green-RGB.svg">
<source media="(prefers-color-scheme: light)" srcset="https://assets.vespa.ai/logos/Vespa-logo-dark-RGB.svg">
<img alt="#Vespa" width="200" src="https://assets.vespa.ai/logos/Vespa-logo-dark-RGB.svg" style="margin-bottom: 25px;">
</picture>


# FastHTML Vespa frontend

This is a simple frontend for Vespa search engine. It is built using [FastHTML](https://www.fastht.ml/) and written in pure Python.

Search page | Login page | Query logs
:-------------------------:|:-------------------------:|:-------------------------:
![search](assets/search-page.png) | ![admin-login](assets/admin-login.png) | ![query-log](assets/query-log.png)

### Features

- Simple search interface, with links to search results.
- Accordion with full JSON-response from Vespa.
- SQLite DB for storing queries.
- Admin authentication for viewing and downloading queries.
- Deployment options - Docker + [Huggingface spaces](https://huggingface.co/spaces/).

### Why?

We have recognized the need, both for ourselves and others, to be able to set up a simple frontend for Vespa, without having to navigate the frontend framework jungle. Demo frontend apps often end up with a bunch of dependendcies and angry github renovate bots. :robot: :sad:

This sample-app can serve as an example of how you can build and deploy a simple frontend for Vespa, using FastHTML.

### How to use

#### 1. Clone this folder to your local machine 📂

The command below will clone the repository and only fetch the `fasthtml-demo` directory.

```bash
git clone --depth 1 --filter=blob:none --sparse https://github.com/vespa-engine/sample-apps.git temp-sample-apps && cd temp-sample-apps && git sparse-checkout set fasthtml-demo && mkdir -p ../fasthtml-demo && mv fasthtml-demo/* ../fasthtml-demp/ && cd .. && rm -rf temp-sample-apps
```

#### 2. Install dependencies 🔧

```bash
pip install -r requirements.txt
```

#### 3. Run the app locally 💻

```bash
python main.py
```

At this point, you should be able to access the app at [http://localhost:5001](http://localhost:5001).

But, you will _not_ be able to search for anything, as your environment variables are not set up.

#### 4. Deploy and feed your Vespa application ▶️

By running the `deploy_app.ipynb` notebook, you will deploy a Vespa application to the Vespa Cloud. The application is just a sample hybrid search application using the [BEIR/nfcorpus](https://huggingface.co/datasets/BeIR/nfcorpus) dataset.
Feel free to replace the dataset and application with your own.

Make sure to replace these variables at the top of the notebook with your own values:

```python
# Replace with your tenant name from the Vespa Cloud Console
tenant_name = "mytenant"
# Replace with your application name (does not need to exist yet)
application = "fasthtml"
# Token id (from Vespa Cloud Console)
token_id = "fasthtmltoken"
```

#### 5. Set up environment variables 🔐

Make sure to add the output of the `token_endpoint` from the `deploy_app.ipynb`- notebook to your `.env.example` file.
This value should be placed in the `VESPA_APP_URL` environment variable.

At the same time, you should rename the `.env.example` file to `.env`. This is added to the `.gitignore` file.

#### 6. Run the app locally 🚀

Now, you should be able to run the app locally and search for queries.

```bash
python main.py
```

Open your browser and navigate to [http://localhost:5001](http://localhost:5001).

### Deployment

If you want to deploy the app, you set the `DEV_MODE=False` in `main.py`.
This will disable loading of environment variables from the `.env` file, and instead use the environment variables set in the deployment environment.

#### Docker 🐳

You can build and run the app using Docker.

Note that there are two Dockerfiles in the repo:

- `Dockerfile` is for building the image for Huggingface Spaces.
- `Dockerfile.nonhf` is for building an image that can be run locally or on any other platform.

Build the image:

```bash
docker build -t fhtdemoimg . -f Dockerfile.nonhf
```

**Run the container:**

- Makes the environment variables in the `.env` file available to the container.
- Will mount the `db/` folder to the container, so that the SQLite database is persisted between runs.
- Sets the hostname to `dockerhost`, so that we can know use that to enable hot-reloading in the FastHTML app.
- Maps the default Starlette port `5001` to `8000` on the host.

```bash
docker run --name fhtdemo --rm --env-file .env -p 8000:5001 -h dockerhost -v $(pwd)/db:/code/db fhtdemoimg
```

#### Huggingface 🤗 Spaces

This deployment option is free. The deployment script is shamelessly copied from the [fasthtml-hf](https://github.com/AnswerDotAI/fasthtml-hf) repo. Check it out for details on cli-options, configuration and DB-backup options.

1. Get a huggingface token with `write` permissions. You can do this by going to your [Huggingface profile](https://huggingface.co/settings/tokens) and create a new token.
2. Set the `HF_TOKEN` environment variable to the token you just created.
3. Run `python deploy_hf.py <your-space-name> [--private true]` to deploy the app to Huggingface Spaces.
4. Remember to add `VESPA_APP_URL` and `VESPA_CLOUD_SECRET_TOKEN` to the environment variables in the [Huggingface Spaces settings.](https://huggingface.co/docs/hub/en/spaces-overview#managing-secrets)

### Go build some cool Vespa apps! 🚀
Binary file added examples/fasthtml-demo/assets/admin-login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/fasthtml-demo/assets/query-log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/fasthtml-demo/assets/search-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions examples/fasthtml-demo/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[DEFAULT]
dataset_id = space-backup
db_dir = data
private_backup = True
interval = 5
Loading
Loading