bibibi12345 commited on
Commit
58a9053
·
1 Parent(s): 497af08

password added

Browse files
Files changed (2) hide show
  1. README.md +27 -6
  2. main.py +21 -3
README.md CHANGED
@@ -19,6 +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
  ## Running Locally (without Docker)
24
 
@@ -31,19 +32,20 @@ The application requires the following environment variables to be set:
31
  ```dotenv
32
  NOTION_COOKIE="your_cookie_value_here"
33
  NOTION_SPACE_ID="your_space_id_here"
 
34
  ```
35
  4. Run the application using Uvicorn:
36
  ```bash
37
  uvicorn main:app --reload --port 7860
38
  ```
39
- The server will be available at `http://localhost:7860`.
40
 
41
  ## Running with Docker Compose (Recommended for Local Dev)
42
 
43
  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.
44
 
45
  1. Ensure you have Docker and Docker Compose installed.
46
- 2. Make sure your `.env` file exists in the project root with your `NOTION_COOKIE` and `NOTION_SPACE_ID`.
47
  3. Run the following command in the project root:
48
  ```bash
49
  docker-compose up --build -d
@@ -71,9 +73,10 @@ This method involves building and running the Docker container manually, passing
71
  docker run -p 7860:7860 \
72
  -e NOTION_COOKIE="your_cookie_value" \
73
  -e NOTION_SPACE_ID="your_space_id" \
 
74
  notion-api-bridge
75
  ```
76
- The server will be available at `http://localhost:7860` (or whichever host port you mapped to the container's 7860).
77
 
78
  ## Deploying to Hugging Face Spaces
79
 
@@ -84,13 +87,31 @@ This application is designed to be easily deployed as a Docker Space on Hugging
84
  3. **Add Secrets:** In your Space settings, navigate to the "Secrets" section. Add two secrets:
85
  * `NOTION_COOKIE`: Paste your Notion `token_v2` cookie value.
86
  * `NOTION_SPACE_ID`: Paste your Notion Space ID.
 
87
  Hugging Face will securely inject these secrets as environment variables into your running container.
88
  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).
89
- 5. **Accessing the API:** Once the Space is running, you can access the API endpoint at the Space's public URL. For example, if your Space URL is `https://your-username-your-space-name.hf.space`, you can send requests like:
 
 
90
  ```bash
 
91
  curl -X POST https://your-username-your-space-name.hf.space/v1/chat/completions \
 
 
 
 
 
 
 
 
 
 
 
 
92
  -H "Content-Type: application/json" \
93
  -d '{
94
- "model": "gpt-3.5-turbo", # Model name is often ignored by bridge, but required by spec
95
- "messages": [{"role": "user", "content": "Add a new page titled '\''Meeting Notes'\'' with content '\''Discuss project roadmap'\''"}]
 
 
96
  }'
 
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_PASSWORD` (Optional): The password required for HTTP Basic Authentication to access the API endpoints. If not set, it defaults to `123456`.
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_PASSWORD="your_secure_password" # Optional, defaults to 123456
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 password (either the default `123456` or the one set in `.env`) via HTTP Basic Authentication.
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_PASSWORD`. If `PROXY_PASSWORD` is not in the `.env` file, the default `123456` 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_PASSWORD="your_secure_password" \ # Set your desired password 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 password provided in the `-e PROXY_PASSWORD` flag 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_PASSWORD`: Paste the desired password for API authentication (e.g., a strong, generated password). If you omit this, the default `123456` will be used, which is **not recommended** for public spaces.
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 password via HTTP Basic Authentication. The username can be anything (e.g., `user`), but the password must match the `PROXY_PASSWORD` secret you set (or the default `123456`).
94
+
95
+ **Example using `curl` (replace `your_password` and URL):**
96
  ```bash
97
+ # Example for Hugging Face Space
98
  curl -X POST https://your-username-your-space-name.hf.space/v1/chat/completions \
99
+ -u user:your_password \
100
+ -H "Content-Type: application/json" \
101
+ -d '{
102
+ "model": "notion-model", # Specify a Notion model like "openai-gpt-4.1"
103
+ "messages": [{"role": "user", "content": "Summarize this document."}],
104
+ "stream": false,
105
+ "notion_model": "openai-gpt-4.1" # Required field for Notion
106
+ }'
107
+
108
+ # Example for Localhost (using default password 123456)
109
+ curl -X POST http://localhost:7860/v1/chat/completions \
110
+ -u user:123456 \
111
  -H "Content-Type: application/json" \
112
  -d '{
113
+ "model": "notion-model",
114
+ "messages": [{"role": "user", "content": "What is the capital of France?"}],
115
+ "stream": true,
116
+ "notion_model": "anthropic-sonnet-4" # Required field for Notion
117
  }'
main.py CHANGED
@@ -3,9 +3,11 @@ import uuid
3
  import json
4
  import time
5
  import httpx
6
- from fastapi import FastAPI, Request, HTTPException
 
7
  from fastapi.responses import StreamingResponse
8
  from dotenv import load_dotenv
 
9
  from models import (
10
  ChatMessage, ChatCompletionRequest, NotionTranscriptConfigValue,
11
  NotionTranscriptItem, NotionDebugOverrides, NotionRequestBody,
@@ -30,6 +32,22 @@ if not NOTION_SPACE_ID:
30
  # Consider raising an error instead: raise ValueError("NOTION_SPACE_ID not set")
31
  NOTION_SPACE_ID = str(uuid.uuid4()) # Default or raise error
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # --- FastAPI App ---
34
  app = FastAPI()
35
 
@@ -156,7 +174,7 @@ async def stream_notion_response(notion_request_body: NotionRequestBody):
156
  # --- API Endpoint ---
157
 
158
  @app.get("/v1/models", response_model=ModelList)
159
- async def list_models():
160
  """
161
  Endpoint to list available Notion models, mimicking OpenAI's /v1/models.
162
  """
@@ -171,7 +189,7 @@ async def list_models():
171
  ]
172
  return ModelList(data=model_list)
173
  @app.post("/v1/chat/completions")
174
- async def chat_completions(request_data: ChatCompletionRequest, request: Request):
175
  """
176
  Endpoint to mimic OpenAI's chat completions, proxying to Notion.
177
  """
 
3
  import json
4
  import time
5
  import httpx
6
+ from fastapi import FastAPI, Request, HTTPException, Depends, status
7
+ from fastapi.security import HTTPBasic, HTTPBasicCredentials
8
  from fastapi.responses import StreamingResponse
9
  from dotenv import load_dotenv
10
+ import secrets # Added for secure comparison
11
  from models import (
12
  ChatMessage, ChatCompletionRequest, NotionTranscriptConfigValue,
13
  NotionTranscriptItem, NotionDebugOverrides, NotionRequestBody,
 
32
  # Consider raising an error instead: raise ValueError("NOTION_SPACE_ID not set")
33
  NOTION_SPACE_ID = str(uuid.uuid4()) # Default or raise error
34
 
35
+ # --- Authentication ---
36
+ PROXY_PASSWORD = os.getenv("PROXY_PASSWORD", "123456") # Default password
37
+ security = HTTPBasic()
38
+
39
+ def authenticate(credentials: HTTPBasicCredentials = Depends(security)):
40
+ """Compares provided password with the proxy password."""
41
+ correct_password = secrets.compare_digest(credentials.password, PROXY_PASSWORD)
42
+ if not correct_password:
43
+ raise HTTPException(
44
+ status_code=status.HTTP_401_UNAUTHORIZED,
45
+ detail="Incorrect password",
46
+ headers={"WWW-Authenticate": "Basic"},
47
+ )
48
+ # Username is ignored as per requirement
49
+ return True # Indicate successful authentication
50
+
51
  # --- FastAPI App ---
52
  app = FastAPI()
53
 
 
174
  # --- API Endpoint ---
175
 
176
  @app.get("/v1/models", response_model=ModelList)
177
+ async def list_models(authenticated: bool = Depends(authenticate)):
178
  """
179
  Endpoint to list available Notion models, mimicking OpenAI's /v1/models.
180
  """
 
189
  ]
190
  return ModelList(data=model_list)
191
  @app.post("/v1/chat/completions")
192
+ async def chat_completions(request_data: ChatCompletionRequest, request: Request, authenticated: bool = Depends(authenticate)):
193
  """
194
  Endpoint to mimic OpenAI's chat completions, proxying to Notion.
195
  """