File size: 2,634 Bytes
245820b
 
 
 
 
5d974bb
 
 
 
 
 
245820b
 
 
 
992f09e
751cbd9
992f09e
751cbd9
 
 
245820b
751cbd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122




---
title: Canspace
emoji: 🐨
colorFrom: red
colorTo: purple
sdk: docker
pinned: false




---
# AI Text Detector β€” FastAPI App Documentation

## About
This FastAPI application predicts whether a given text is AI-generated or human-written based on a GPT-2 model.  
It measures the perplexity of the input text to make the decision.

You can access the live version here:  
**[Public App Link](https://can-org-canspace.hf.space)**

---

## API Endpoints

### `GET /`
- **Description**: Welcome route showing basic API information.
- **Response**:
  ```json
  {
    "message": "FastAPI API is up.",
    "try": "/docs to test the API.",
    "status": "OK"
  }
  ```

### `GET /health`
- **Description**: Health check to verify if the server is running.
- **Response**:
  ```json
  {
    "status": "ok"
  }
  ```

### `POST /analyze`
- **Description**: Analyze input text to determine if it is AI-generated or human-written.
- **Authorization**: Requires Bearer Token.
- **Request Body**:
  ```json
  {
    "text": "Enter your text here"
  }
  ```
- **Response**:
  ```json
  {
    "result": "AI-generated" | "Probably AI-generated" | "Human-written",
    "perplexity": 47.35
  }
  ```
- **Important**:
  - Input text must contain at least two words.
  - Without a valid token, this endpoint will not work.

---

## Authorization Guide
- Open `/docs` (Swagger UI).
- Click the "Authorize" button at the top right.
- Paste your Bearer Token into the Authorization field.
- After authorization, you can use the `/analyze` endpoint.

Example Authorization header:
```
Authorization: Bearer YOUR_SECRET_TOKEN
```

---

## How the AI Detection Works
- The app loads a fine-tuned GPT-2 model at startup.
- It calculates the perplexity of the input text:
  - Perplexity less than 60 β†’ AI-generated
  - Perplexity between 60 and 80 β†’ Probably AI-generated
  - Perplexity greater than 80 β†’ Human-written

Higher perplexity indicates the text is harder for the model to predict, usually meaning it is human-written.

---

## How to Run Locally
1. Clone the repository:
   ```bash
   git clone https://huggingface.co/spaces/can-org/canspace
   cd canspace
   ```

2. Install dependencies:
   ```bash
   pip install -r requirements.txt
   ```

3. Start the server:
   ```bash
   uvicorn app:app --reload
   ```

4. Open the API docs at:  
   [http://localhost:8000/docs](http://localhost:8000/docs)



## Quick Reminders
- Always authorize first in `/docs` before using `/analyze`.
- Text input must contain more than two words.
- Public live app is available at: [https://can-org-canspace.hf.space](https://can-org-canspace.hf.space)