Spaces:
Sleeping
Sleeping
Pujan Neupane commited on
Commit ·
5fb4c10
0
Parent(s):
Pushed the fastapi code
Browse filesFiles changed (5)
- .gitattributesAdded+2
- .gitignoreAdded+2
- README.mdAdded+132
- app.pyAdded+97
- requirements.txtAdded+49
12*.pth filter=lfs diff=lfs merge=lfs -textAi-Text-Detector/model_weights.pth filter=lfs diff=lfs merge=lfs -text12./venv/./__pycache__/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132### **FastAPI AI**
This 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**
```bashpip install -r requirements.txt
```
This command installs all the dependencies listed in the `requirements.txt` file. It ensures that your environment has the required packages to run the project smoothly.
**NOTE: IF YOU HAVE DONE ANY CHANGES DON'NT FORGOT TO PUT IT IN THE REQUIREMENTS.TXT USING `bash pip freeze > requirements.txt `**
---
### **Functions**
1. **`load_model()`** Loads the GPT-2 model and tokenizer from specified paths.
2. **`lifespan()`** Manages the app's lifecycle: loads the model at startup and handles cleanup on shutdown.
3. **`classify_text_sync()`** Synchronously tokenizes input text and classifies it using the GPT-2 model. Returns the classification and perplexity.
4. **`classify_text()`** Asynchronously executes `classify_text_sync()` in a thread pool to ensure non-blocking processing.
5. **`analyze_text()`** **POST** endpoint: accepts text input, classifies it using `classify_text()`, and returns the result with perplexity.
6. **`health_check()`** **GET** endpoint: simple health check to confirm the API is running.
---
### **Code Overview**
```pythonexecutor = ThreadPoolExecutor(max_workers=2)```
- **`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.
---
### **Running and Load Balancing:**
To run the app in production with load balancing:
```bashuvicorn app:app --host 0.0.0.0 --port 8000 --workers 4```
This command launches the FastAPI app with **4 worker processes**, allowing it to handle multiple requests concurrently.
### **Concurrency Explained:**
1. **`ThreadPoolExecutor(max_workers=20)`**
- Controls the **number of threads** within a **single worker** process. - Allows up to 20 tasks (text classification requests) to be handled simultaneously per worker, improving responsiveness for I/O-bound tasks.
2. **`--workers 4` in Uvicorn** - Spawns **4 independent worker processes** to handle incoming HTTP requests. - Each worker can independently handle multiple tasks, increasing the app's ability to process concurrent requests in parallel.
### **How They Relate:**
- **Uvicorn’s `--workers`** defines how many worker processes the server will run.- **`ThreadPoolExecutor`** limits how many tasks (threads) each worker can process concurrently.
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.
### **Endpoints**
#### 1. **`/analyze`**
- **Method:** `POST`- **Description:** Classifies whether the text is AI-generated or human-written.- **Request:** ```json { "text": "sample text" } ```- **Response:** ```json { "result": "AI-generated", "perplexity": 55.67 } ```
#### 2. **`/health`**
- **Method:** `GET`- **Description:** Returns the status of the API.- **Response:** ```json { "status": "ok" } ```
---
### **Running the API**
Start the server with:
```bashuvicorn app:app --host 0.0.0.0 --port 8000 --workers 4```
---
### **Testing the API**
- Use `curl` for testing: ```bash curl -X 'POST' 'http://127.0.0.1:8000/analyze' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"text": "sample text"}' ```
---
### **API Documentation**
- **Swagger UI:** `http://127.0.0.1:8000/docs` -> `/docs`- **ReDoc:** `http://127.0.0.1:8000/redoc` -> `/redoc`
### **Implement it with NEST.js**
will be updated12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import torchfrom transformers import GPT2LMHeadModel, GPT2TokenizerFastfrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelimport asynciofrom concurrent.futures import ThreadPoolExecutorfrom contextlib import asynccontextmanager
# FastAPI instanceapp = FastAPI()model, tokenizer = None, Noneexecutor = ThreadPoolExecutor(max_workers=20)
# Function to load model and tokenizer
def load_model(): model_path = "./Ai-Text-Detector/model" weights_path = "./Ai-Text-Detector/model_weights.pth" tokenizer = GPT2TokenizerFast.from_pretrained(model_path) model = GPT2LMHeadModel.from_pretrained("gpt2") model.load_state_dict(torch.load(weights_path, map_location=torch.device("cpu"))) model.eval() # Set the model to evaluation mode return model, tokenizer
@asynccontextmanagerasync def lifespan(app: FastAPI): global model, tokenizer model, tokenizer = load_model() yield
app = FastAPI(lifespan=lifespan)
class TextInput(BaseModel): text: str
# Function to classify the text
def classify_text_sync(sentence: str): inputs = tokenizer(sentence, return_tensors="pt", truncation=True, padding=True) input_ids = inputs["input_ids"] attention_mask = inputs["attention_mask"]
with torch.no_grad(): outputs = model(input_ids, attention_mask=attention_mask, labels=input_ids) loss = outputs.loss perplexity = torch.exp(loss).item()
if perplexity < 60: result = "AI-generated*" elif perplexity < 80: result = "Probably AI-generated*" else: result = "Human-written*"
return result, perplexity
async def classify_text(sentence: str): loop = asyncio.get_event_loop() return await loop.run_in_executor(executor, classify_text_sync, sentence)
@app.post("/analyze")async def analyze_text(data: TextInput): user_input = data.text.strip()
if not user_input: raise HTTPException(status_code=400, detail="Text cannot be empty")
result, perplexity = await classify_text(user_input)
return { "result": result, "perplexity": round(perplexity, 2), }
@app.get("/health")async def health_check(): return {"status": "ok"}
@app.get("/")def index(): return {"Its an API"}
if __name__ == "__main__": import uvicorn
uvicorn.run("app:app", host="0.0.0.0", port=8000, workers=4) # Specify 4 workers12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849annotated-types==0.7.0anyio==4.9.0certifi==2025.1.31charset-normalizer==3.4.1click==8.1.8fastapi==0.115.12filelock==3.18.0fsspec==2025.3.2h11==0.14.0huggingface-hub==0.30.2idna==3.10Jinja2==3.1.6MarkupSafe==3.0.2mpmath==1.3.0networkx==3.4.2numpy==2.2.5nvidia-cublas-cu12==12.4.5.8nvidia-cuda-cupti-cu12==12.4.127nvidia-cuda-nvrtc-cu12==12.4.127nvidia-cuda-runtime-cu12==12.4.127nvidia-cudnn-cu12==9.1.0.70nvidia-cufft-cu12==11.2.1.3nvidia-curand-cu12==10.3.5.147nvidia-cusolver-cu12==11.6.1.9nvidia-cusparse-cu12==12.3.1.170nvidia-cusparselt-cu12==0.6.2nvidia-nccl-cu12==2.21.5nvidia-nvjitlink-cu12==12.4.127nvidia-nvtx-cu12==12.4.127packaging==25.0pydantic==2.11.3pydantic_core==2.33.1PyYAML==6.0.2regex==2024.11.6requests==2.32.3safetensors==0.5.3setuptools==79.0.0sniffio==1.3.1starlette==0.46.2sympy==1.13.1tokenizers==0.21.1torch==2.6.0tqdm==4.67.1transformers==4.51.3triton==3.2.0typing-inspection==0.4.0typing_extensions==4.13.2urllib3==2.4.0uvicorn==0.34.2.gitattributesAdded+2
.gitignoreAdded+2
README.mdAdded+132
app.pyAdded+97
requirements.txtAdded+49