Pujan Neupane commited on
Commit
869a4ba
Β·
1 Parent(s): b5008e5

Added Readme for Accessing the fastapi from the nest.js

Browse files
Files changed (1) hide show
  1. README.md +133 -2
README.md CHANGED
@@ -2,8 +2,6 @@
2
 
3
  This FastAPI app loads a GPT-2 model, tokenizes input text, classifies it, and returns whether the text is AI-generated or human-written.
4
 
5
- ---
6
-
7
  ### **install Dependencies**
8
 
9
  ```bash
@@ -156,3 +154,136 @@ def verify_token(auth: str):
156
  This provides a basic but effective layer of security to prevent unauthorized access to the API.
157
 
158
  ### **Implement it with NEST.js**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  This FastAPI app loads a GPT-2 model, tokenizes input text, classifies it, and returns whether the text is AI-generated or human-written.
4
 
 
 
5
  ### **install Dependencies**
6
 
7
  ```bash
 
154
  This provides a basic but effective layer of security to prevent unauthorized access to the API.
155
 
156
  ### **Implement it with NEST.js**
157
+
158
+ NOTE: Make an micro service in NEST.JS and implement it there and call it from app.controller.ts
159
+
160
+ in fastapi.service.ts file what we have done is
161
+
162
+ ### Project Structure
163
+
164
+ ```files
165
+ nestjs-fastapi-bridge/
166
+ β”œβ”€β”€ src/
167
+ β”‚ β”œβ”€β”€ app.controller.ts
168
+ β”‚ β”œβ”€β”€ app.module.ts
169
+ β”‚ └── fastapi.service.ts
170
+ β”œβ”€β”€ .env
171
+
172
+ ```
173
+
174
+ ---
175
+
176
+ ### Step-by-Step Setup
177
+
178
+ #### 1. `.env`
179
+
180
+ Create a `.env` file at the root with the following:
181
+
182
+ ```environment
183
+ FASTAPI_BASE_URL=http://localhost:8000
184
+ SECRET_TOKEN="HelloThere"
185
+ ```
186
+
187
+ #### 2. `fastapi.service.ts`
188
+
189
+ ```javascript
190
+ // src/fastapi.service.ts
191
+ import { Injectable } from "@nestjs/common";
192
+ import { HttpService } from "@nestjs/axios";
193
+ import { ConfigService } from "@nestjs/config";
194
+ import { firstValueFrom } from "rxjs";
195
+
196
+ @Injectable()
197
+ export class FastAPIService {
198
+ constructor(
199
+ private http: HttpService,
200
+ private config: ConfigService,
201
+ ) {}
202
+
203
+ async analyzeText(text: string) {
204
+ const url = `${this.config.get("FASTAPI_BASE_URL")}/analyze`;
205
+ const token = this.config.get("SECRET_TOKEN");
206
+
207
+ const response = await firstValueFrom(
208
+ this.http.post(
209
+ url,
210
+ { text },
211
+ {
212
+ headers: {
213
+ Authorization: `Bearer ${token}`,
214
+ },
215
+ },
216
+ ),
217
+ );
218
+
219
+ return response.data;
220
+ }
221
+ }
222
+ ```
223
+
224
+ #### 3. `app.module.ts`
225
+
226
+ ```javascript
227
+ // src/app.module.ts
228
+ import { Module } from "@nestjs/common";
229
+ import { ConfigModule } from "@nestjs/config";
230
+ import { HttpModule } from "@nestjs/axios";
231
+ import { AppController } from "./app.controller";
232
+ import { FastAPIService } from "./fastapi.service";
233
+
234
+ @Module({
235
+ imports: [ConfigModule.forRoot(), HttpModule],
236
+ controllers: [AppController],
237
+ providers: [FastAPIService],
238
+ })
239
+ export class AppModule {}
240
+ ```
241
+
242
+ ---
243
+
244
+ #### 4. `app.controller.ts`
245
+
246
+ ```javascript
247
+ // src/app.controller.ts
248
+ import { Body, Controller, Post, Get, Query } from '@nestjs/common';
249
+ import { FastAPIService } from './fastapi.service';
250
+
251
+ @Controller()
252
+ export class AppController {
253
+ constructor(private readonly fastapiService: FastAPIService) {}
254
+
255
+ @Post('analyze-text')
256
+ async callFastAPI(@Body('text') text: string) {
257
+ return this.fastapiService.analyzeText(text);
258
+ }
259
+
260
+ @Get()
261
+ getHello(): string {
262
+ return 'NestJS is connected to FastAPI ';
263
+ }
264
+ }
265
+ ```
266
+
267
+ ### πŸš€ How to Run
268
+
269
+ Run the server of flask and nest.js:
270
+
271
+ - for nest.js
272
+ ```bash
273
+ npm run start
274
+ ```
275
+ - for Fastapi
276
+
277
+ ```bash
278
+ uvicorn app:app --reload
279
+ ```
280
+
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' \
288
+ -d '{"text": "This is a test input"}'
289
+ ```