Spaces:
Sleeping
Sleeping
Commit
·
b4578a2
1
Parent(s):
58a9053
password added
Browse files
README.md
CHANGED
|
@@ -19,7 +19,7 @@ The application requires the following environment variables to be set:
|
|
| 19 |
|
| 20 |
* `NOTION_COOKIE`: Your Notion `token_v2` cookie value. This is used for authentication with the Notion API. You can typically find this in your browser's developer tools while logged into Notion.
|
| 21 |
* `NOTION_SPACE_ID`: The ID of your Notion workspace. You can usually find this in the URL when browsing your Notion workspace (it's the part after your domain and before the first page ID, often a UUID).
|
| 22 |
-
* `
|
| 23 |
|
| 24 |
## Running Locally (without Docker)
|
| 25 |
|
|
@@ -32,20 +32,20 @@ The application requires the following environment variables to be set:
|
|
| 32 |
```dotenv
|
| 33 |
NOTION_COOKIE="your_cookie_value_here"
|
| 34 |
NOTION_SPACE_ID="your_space_id_here"
|
| 35 |
-
#
|
| 36 |
```
|
| 37 |
4. Run the application using Uvicorn:
|
| 38 |
```bash
|
| 39 |
uvicorn main:app --reload --port 7860
|
| 40 |
```
|
| 41 |
-
The server will be available at `http://localhost:7860`. You will need to provide the correct
|
| 42 |
|
| 43 |
## Running with Docker Compose (Recommended for Local Dev)
|
| 44 |
|
| 45 |
This method uses the `docker-compose.yml` file for a streamlined local development setup. It automatically builds the image if needed and loads environment variables directly from your `.env` file.
|
| 46 |
|
| 47 |
1. Ensure you have Docker and Docker Compose installed.
|
| 48 |
-
2. Make sure your `.env` file exists in the project root with your `NOTION_COOKIE`, `NOTION_SPACE_ID`, and optionally `
|
| 49 |
3. Run the following command in the project root:
|
| 50 |
```bash
|
| 51 |
docker-compose up --build -d
|
|
@@ -73,10 +73,10 @@ This method involves building and running the Docker container manually, passing
|
|
| 73 |
docker run -p 7860:7860 \
|
| 74 |
-e NOTION_COOKIE="your_cookie_value" \
|
| 75 |
-e NOTION_SPACE_ID="your_space_id" \
|
| 76 |
-
-e
|
| 77 |
notion-api-bridge
|
| 78 |
```
|
| 79 |
-
The server will be available at `http://localhost:7860` (or whichever host port you mapped to the container's 7860). You will need to use the
|
| 80 |
|
| 81 |
## Deploying to Hugging Face Spaces
|
| 82 |
|
|
@@ -87,16 +87,17 @@ This application is designed to be easily deployed as a Docker Space on Hugging
|
|
| 87 |
3. **Add Secrets:** In your Space settings, navigate to the "Secrets" section. Add two secrets:
|
| 88 |
* `NOTION_COOKIE`: Paste your Notion `token_v2` cookie value.
|
| 89 |
* `NOTION_SPACE_ID`: Paste your Notion Space ID.
|
| 90 |
-
* `
|
| 91 |
Hugging Face will securely inject these secrets as environment variables into your running container.
|
| 92 |
4. **Deployment:** Hugging Face Spaces will automatically build the Docker image from your `Dockerfile` and run the container. It detects applications running on port 7860 (as specified in the `Dockerfile` and metadata).
|
| 93 |
-
5. **Accessing the API:** Once the Space is running, you can access the API endpoint at the Space's public URL, providing the
|
| 94 |
|
| 95 |
-
**Example using `curl` (replace `
|
| 96 |
```bash
|
| 97 |
-
# Example for Hugging Face Space
|
|
|
|
| 98 |
curl -X POST https://your-username-your-space-name.hf.space/v1/chat/completions \
|
| 99 |
-
-
|
| 100 |
-H "Content-Type: application/json" \
|
| 101 |
-d '{
|
| 102 |
"model": "notion-model", # Specify a Notion model like "openai-gpt-4.1"
|
|
@@ -105,9 +106,10 @@ This application is designed to be easily deployed as a Docker Space on Hugging
|
|
| 105 |
"notion_model": "openai-gpt-4.1" # Required field for Notion
|
| 106 |
}'
|
| 107 |
|
| 108 |
-
# Example for Localhost (using default
|
|
|
|
| 109 |
curl -X POST http://localhost:7860/v1/chat/completions \
|
| 110 |
-
-
|
| 111 |
-H "Content-Type: application/json" \
|
| 112 |
-d '{
|
| 113 |
"model": "notion-model",
|
|
|
|
| 19 |
|
| 20 |
* `NOTION_COOKIE`: Your Notion `token_v2` cookie value. This is used for authentication with the Notion API. You can typically find this in your browser's developer tools while logged into Notion.
|
| 21 |
* `NOTION_SPACE_ID`: The ID of your Notion workspace. You can usually find this in the URL when browsing your Notion workspace (it's the part after your domain and before the first page ID, often a UUID).
|
| 22 |
+
* `PROXY_AUTH_TOKEN` (Optional): The Bearer token required for authentication to access the API endpoints. If not set, it defaults to `default_token`.
|
| 23 |
|
| 24 |
## Running Locally (without Docker)
|
| 25 |
|
|
|
|
| 32 |
```dotenv
|
| 33 |
NOTION_COOKIE="your_cookie_value_here"
|
| 34 |
NOTION_SPACE_ID="your_space_id_here"
|
| 35 |
+
# PROXY_AUTH_TOKEN="your_secure_token" # Optional, defaults to default_token
|
| 36 |
```
|
| 37 |
4. Run the application using Uvicorn:
|
| 38 |
```bash
|
| 39 |
uvicorn main:app --reload --port 7860
|
| 40 |
```
|
| 41 |
+
The server will be available at `http://localhost:7860`. You will need to provide the correct token (either the default `default_token` or the one set in `.env`) via an `Authorization: Bearer <token>` header.
|
| 42 |
|
| 43 |
## Running with Docker Compose (Recommended for Local Dev)
|
| 44 |
|
| 45 |
This method uses the `docker-compose.yml` file for a streamlined local development setup. It automatically builds the image if needed and loads environment variables directly from your `.env` file.
|
| 46 |
|
| 47 |
1. Ensure you have Docker and Docker Compose installed.
|
| 48 |
+
2. Make sure your `.env` file exists in the project root with your `NOTION_COOKIE`, `NOTION_SPACE_ID`, and optionally `PROXY_AUTH_TOKEN`. If `PROXY_AUTH_TOKEN` is not in the `.env` file, the default `default_token` will be used.
|
| 49 |
3. Run the following command in the project root:
|
| 50 |
```bash
|
| 51 |
docker-compose up --build -d
|
|
|
|
| 73 |
docker run -p 7860:7860 \
|
| 74 |
-e NOTION_COOKIE="your_cookie_value" \
|
| 75 |
-e NOTION_SPACE_ID="your_space_id" \
|
| 76 |
+
-e PROXY_AUTH_TOKEN="your_token" \ # Set your desired token here
|
| 77 |
notion-api-bridge
|
| 78 |
```
|
| 79 |
+
The server will be available at `http://localhost:7860` (or whichever host port you mapped to the container's 7860). You will need to use the token provided in the `-e PROXY_AUTH_TOKEN` flag via an `Authorization: Bearer <token>` header for authentication.
|
| 80 |
|
| 81 |
## Deploying to Hugging Face Spaces
|
| 82 |
|
|
|
|
| 87 |
3. **Add Secrets:** In your Space settings, navigate to the "Secrets" section. Add two secrets:
|
| 88 |
* `NOTION_COOKIE`: Paste your Notion `token_v2` cookie value.
|
| 89 |
* `NOTION_SPACE_ID`: Paste your Notion Space ID.
|
| 90 |
+
* `PROXY_AUTH_TOKEN`: Paste the desired Bearer token for API authentication (e.g., a strong, generated token). If you omit this, the default `default_token` will be used.
|
| 91 |
Hugging Face will securely inject these secrets as environment variables into your running container.
|
| 92 |
4. **Deployment:** Hugging Face Spaces will automatically build the Docker image from your `Dockerfile` and run the container. It detects applications running on port 7860 (as specified in the `Dockerfile` and metadata).
|
| 93 |
+
5. **Accessing the API:** Once the Space is running, you can access the API endpoint at the Space's public URL, providing the token via an `Authorization: Bearer <token>` header. The token must match the `PROXY_AUTH_TOKEN` secret you set (or the default `default_token`).
|
| 94 |
|
| 95 |
+
**Example using `curl` (replace `your_token` and URL):**
|
| 96 |
```bash
|
| 97 |
+
# Example for Hugging Face Space (using token from HF Secret)
|
| 98 |
+
# Replace YOUR_HF_TOKEN with the value you set in the PROXY_AUTH_TOKEN secret
|
| 99 |
curl -X POST https://your-username-your-space-name.hf.space/v1/chat/completions \
|
| 100 |
+
-H "Authorization: Bearer YOUR_HF_TOKEN" \
|
| 101 |
-H "Content-Type: application/json" \
|
| 102 |
-d '{
|
| 103 |
"model": "notion-model", # Specify a Notion model like "openai-gpt-4.1"
|
|
|
|
| 106 |
"notion_model": "openai-gpt-4.1" # Required field for Notion
|
| 107 |
}'
|
| 108 |
|
| 109 |
+
# Example for Localhost (using default token 'default_token')
|
| 110 |
+
# If you set a different token in .env or via -e, use that instead.
|
| 111 |
curl -X POST http://localhost:7860/v1/chat/completions \
|
| 112 |
+
-H "Authorization: Bearer default_token" \
|
| 113 |
-H "Content-Type: application/json" \
|
| 114 |
-d '{
|
| 115 |
"model": "notion-model",
|
main.py
CHANGED
|
@@ -4,7 +4,7 @@ import json
|
|
| 4 |
import time
|
| 5 |
import httpx
|
| 6 |
from fastapi import FastAPI, Request, HTTPException, Depends, status
|
| 7 |
-
from fastapi.security import
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
import secrets # Added for secure comparison
|
|
@@ -33,19 +33,18 @@ if not NOTION_SPACE_ID:
|
|
| 33 |
NOTION_SPACE_ID = str(uuid.uuid4()) # Default or raise error
|
| 34 |
|
| 35 |
# --- Authentication ---
|
| 36 |
-
|
| 37 |
-
security =
|
| 38 |
|
| 39 |
-
def authenticate(credentials:
|
| 40 |
-
"""Compares provided
|
| 41 |
-
|
| 42 |
-
if not
|
| 43 |
raise HTTPException(
|
| 44 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 45 |
-
detail="
|
| 46 |
-
|
| 47 |
)
|
| 48 |
-
# Username is ignored as per requirement
|
| 49 |
return True # Indicate successful authentication
|
| 50 |
|
| 51 |
# --- FastAPI App ---
|
|
|
|
| 4 |
import time
|
| 5 |
import httpx
|
| 6 |
from fastapi import FastAPI, Request, HTTPException, Depends, status
|
| 7 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
import secrets # Added for secure comparison
|
|
|
|
| 33 |
NOTION_SPACE_ID = str(uuid.uuid4()) # Default or raise error
|
| 34 |
|
| 35 |
# --- Authentication ---
|
| 36 |
+
EXPECTED_TOKEN = os.getenv("PROXY_AUTH_TOKEN", "default_token") # Default token
|
| 37 |
+
security = HTTPBearer()
|
| 38 |
|
| 39 |
+
def authenticate(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 40 |
+
"""Compares provided token with the expected token."""
|
| 41 |
+
correct_token = secrets.compare_digest(credentials.credentials, EXPECTED_TOKEN)
|
| 42 |
+
if not correct_token:
|
| 43 |
raise HTTPException(
|
| 44 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 45 |
+
detail="Invalid authentication credentials",
|
| 46 |
+
# WWW-Authenticate header removed for Bearer
|
| 47 |
)
|
|
|
|
| 48 |
return True # Indicate successful authentication
|
| 49 |
|
| 50 |
# --- FastAPI App ---
|