Spaces:
Sleeping
Sleeping
Pujan Neupane commited on
Commit Β·
869a4ba
1
Parent(s): b5008e5
Added Readme for Accessing the fastapi from the nest.js
Browse files1 unmodified line23456789146 unmodified lines1561571581 unmodified lineThis FastAPI app loads a GPT-2 model, tokenizes input text, classifies it, and returns whether the text is AI-generated or human-written.---### **install Dependencies**```bash146 unmodified linesThis provides a basic but effective layer of security to prevent unauthorized access to the API.### **Implement it with NEST.js**1 unmodified line234567146 unmodified lines1541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882891 unmodified lineThis FastAPI app loads a GPT-2 model, tokenizes input text, classifies it, and returns whether the text is AI-generated or human-written.### **install Dependencies**```bash146 unmodified linesThis provides a basic but effective layer of security to prevent unauthorized access to the API.### **Implement it with NEST.js**NOTE: Make an micro service in NEST.JS and implement it there and call it from app.controller.tsin fastapi.service.ts file what we have done is### Project Structure```filesnestjs-fastapi-bridge/βββ src/β βββ app.controller.tsβ βββ app.module.tsβ βββ fastapi.service.tsβββ .env```---### Step-by-Step Setup#### 1. `.env`Create a `.env` file at the root with the following:```environmentFASTAPI_BASE_URL=http://localhost:8000SECRET_TOKEN="HelloThere"```#### 2. `fastapi.service.ts````javascript// src/fastapi.service.tsimport { Injectable } from "@nestjs/common";import { HttpService } from "@nestjs/axios";import { ConfigService } from "@nestjs/config";import { firstValueFrom } from "rxjs";@Injectable()export class FastAPIService {constructor(private http: HttpService,private config: ConfigService,) {}async analyzeText(text: string) {const url = `${this.config.get("FASTAPI_BASE_URL")}/analyze`;const token = this.config.get("SECRET_TOKEN");const response = await firstValueFrom(this.http.post(url,{ text },{headers: {Authorization: `Bearer ${token}`,},},),);return response.data;}}```#### 3. `app.module.ts````javascript// src/app.module.tsimport { Module } from "@nestjs/common";import { ConfigModule } from "@nestjs/config";import { HttpModule } from "@nestjs/axios";import { AppController } from "./app.controller";import { FastAPIService } from "./fastapi.service";@Module({imports: [ConfigModule.forRoot(), HttpModule],controllers: [AppController],providers: [FastAPIService],})export class AppModule {}```---#### 4. `app.controller.ts````javascript// src/app.controller.tsimport { Body, Controller, Post, Get, Query } from '@nestjs/common';import { FastAPIService } from './fastapi.service';@Controller()export class AppController {constructor(private readonly fastapiService: FastAPIService) {}@Post('analyze-text')async callFastAPI(@Body('text') text: string) {return this.fastapiService.analyzeText(text);}@Get()getHello(): string {return 'NestJS is connected to FastAPI ';}}```### π How to RunRun the server of flask and nest.js:- for nest.js```bashnpm run start```- for Fastapi```bashuvicorn app:app --reload```Make sure your FastAPI service is running at `http://localhost:8000`.### Test with CURL```bashcurl -X POST http://localhost:3000/analyze-text \-H 'Content-Type: application/json' \-d '{"text": "This is a test input"}'```
README.md+133-2