Pujan Neupane commited on
Commit ·
d762de0
1
Parent(s): 7098c2e
Updated: UPDATED README.md as well as some changes in code and url
Browse files
README.md
CHANGED
|
@@ -39,14 +39,6 @@ This command installs all the dependencies listed in the `requirements.txt` file
|
|
| 39 |
|
| 40 |
### **Code Overview**
|
| 41 |
|
| 42 |
-
```python
|
| 43 |
-
executor = ThreadPoolExecutor(max_workers=2)
|
| 44 |
-
```
|
| 45 |
-
|
| 46 |
-
- **`ThreadPoolExecutor(max_workers=2)`** limits the number of concurrent threads (tasks) per worker process to 2 for text classification. This helps control resource usage and prevent overloading the server.
|
| 47 |
-
|
| 48 |
-
---
|
| 49 |
-
|
| 50 |
### **Running and Load Balancing:**
|
| 51 |
|
| 52 |
To run the app in production with load balancing:
|
|
@@ -55,25 +47,8 @@ To run the app in production with load balancing:
|
|
| 55 |
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4
|
| 56 |
```
|
| 57 |
|
| 58 |
-
This command launches the FastAPI app
|
| 59 |
-
|
| 60 |
-
### **Concurrency Explained:**
|
| 61 |
|
| 62 |
-
1. **`ThreadPoolExecutor(max_workers=20)`**
|
| 63 |
-
|
| 64 |
-
- Controls the **number of threads** within a **single worker** process.
|
| 65 |
-
- Allows up to 20 tasks (text classification requests) to be handled simultaneously per worker, improving responsiveness for I/O-bound tasks.
|
| 66 |
-
|
| 67 |
-
2. **`--workers 4` in Uvicorn**
|
| 68 |
-
- Spawns **4 independent worker processes** to handle incoming HTTP requests.
|
| 69 |
-
- Each worker can independently handle multiple tasks, increasing the app's ability to process concurrent requests in parallel.
|
| 70 |
-
|
| 71 |
-
### **How They Relate:**
|
| 72 |
-
|
| 73 |
-
- **Uvicorn’s `--workers`** defines how many worker processes the server will run.
|
| 74 |
-
- **`ThreadPoolExecutor`** limits how many tasks (threads) each worker can process concurrently.
|
| 75 |
-
|
| 76 |
-
For example, with **4 workers** and **20 threads per worker**, the server can handle **80 tasks concurrently**. This provides scalable and efficient processing, balancing the load across multiple workers and threads.
|
| 77 |
|
| 78 |
### **Endpoints**
|
| 79 |
|
|
@@ -116,13 +91,13 @@ uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4
|
|
| 116 |
You can test the FastAPI endpoint using `curl` like this:
|
| 117 |
|
| 118 |
```bash
|
| 119 |
-
curl -X POST
|
| 120 |
-
-H "Authorization: Bearer
|
| 121 |
-H "Content-Type: application/json" \
|
| 122 |
-d '{"text": "This is a sample sentence for analysis."}'
|
| 123 |
```
|
| 124 |
|
| 125 |
-
- The `-H "Authorization: Bearer
|
| 126 |
- FastAPI checks this token against the one loaded from the `.env` file.
|
| 127 |
- If the token matches, the request is accepted and processed.
|
| 128 |
- Otherwise, it responds with a `403 Unauthorized` error.
|
|
@@ -131,8 +106,8 @@ curl -X POST http://127.0.0.1:8000/analyze \
|
|
| 131 |
|
| 132 |
### **API Documentation**
|
| 133 |
|
| 134 |
-
- **Swagger UI:** `
|
| 135 |
-
- **ReDoc:** `
|
| 136 |
|
| 137 |
### **🔐 Handshake Mechanism**
|
| 138 |
|
|
@@ -180,8 +155,8 @@ nestjs-fastapi-bridge/
|
|
| 180 |
Create a `.env` file at the root with the following:
|
| 181 |
|
| 182 |
```environment
|
| 183 |
-
FASTAPI_BASE_URL=
|
| 184 |
-
SECRET_TOKEN="
|
| 185 |
```
|
| 186 |
|
| 187 |
#### 2. `fastapi.service.ts`
|
|
@@ -281,7 +256,7 @@ Run the server of flask and nest.js:
|
|
| 281 |
Make sure your FastAPI service is running at `http://localhost:8000`.
|
| 282 |
|
| 283 |
### Test with CURL
|
| 284 |
-
|
| 285 |
```bash
|
| 286 |
curl -X POST http://localhost:3000/analyze-text \
|
| 287 |
-H 'Content-Type: application/json' \
|
|
|
|
| 39 |
|
| 40 |
### **Code Overview**
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
### **Running and Load Balancing:**
|
| 43 |
|
| 44 |
To run the app in production with load balancing:
|
|
|
|
| 47 |
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4
|
| 48 |
```
|
| 49 |
|
| 50 |
+
This command launches the FastAPI app.
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
### **Endpoints**
|
| 54 |
|
|
|
|
| 91 |
You can test the FastAPI endpoint using `curl` like this:
|
| 92 |
|
| 93 |
```bash
|
| 94 |
+
curl -X POST https://can-org-canspace.hf.space/analyze \
|
| 95 |
+
-H "Authorization: Bearer SECRET_CODE" \
|
| 96 |
-H "Content-Type: application/json" \
|
| 97 |
-d '{"text": "This is a sample sentence for analysis."}'
|
| 98 |
```
|
| 99 |
|
| 100 |
+
- The `-H "Authorization: Bearer SECRET_CODE"` part is used to simulate the **handshake**.
|
| 101 |
- FastAPI checks this token against the one loaded from the `.env` file.
|
| 102 |
- If the token matches, the request is accepted and processed.
|
| 103 |
- Otherwise, it responds with a `403 Unauthorized` error.
|
|
|
|
| 106 |
|
| 107 |
### **API Documentation**
|
| 108 |
|
| 109 |
+
- **Swagger UI:** `https://can-org-canspace.hf.space/docs` -> `/docs`
|
| 110 |
+
- **ReDoc:** `https://can-org-canspace.hf.space/redoc` -> `/redoc`
|
| 111 |
|
| 112 |
### **🔐 Handshake Mechanism**
|
| 113 |
|
|
|
|
| 155 |
Create a `.env` file at the root with the following:
|
| 156 |
|
| 157 |
```environment
|
| 158 |
+
FASTAPI_BASE_URL=https://can-org-canspace.hf.space/
|
| 159 |
+
SECRET_TOKEN="SECRET_CODE_TOKEN"
|
| 160 |
```
|
| 161 |
|
| 162 |
#### 2. `fastapi.service.ts`
|
|
|
|
| 256 |
Make sure your FastAPI service is running at `http://localhost:8000`.
|
| 257 |
|
| 258 |
### Test with CURL
|
| 259 |
+
http://localhost:3000/-> Server of nest.js
|
| 260 |
```bash
|
| 261 |
curl -X POST http://localhost:3000/analyze-text \
|
| 262 |
-H 'Content-Type: application/json' \
|