A FastAPI-based backend service for humanizing AI-generated text and detecting AI content.
- Analyzes text to detect AI-generated content
- Classifies each sentence as:
- AI-generated
- AI-generated & AI-refined
- Human-written
- Human-written & AI-refined
- Returns detailed percentages and classification results
- Uses
roberta-base-openai-detectormodel
- Humanizes AI-generated text while preserving APA citations
- Expands contractions naturally
- Replaces words with contextual synonyms using spaCy + WordNet
- Adds academic transitions between sentences
- NEW: Adds hedging language to sound more natural and academic
- NEW: Intelligently combines short sentences with semantic analysis
- Configurable transformation probabilities for fine control
- Python 3.9+
- pip
- Clone the repository:
git clone <repository-url>
cd Text-Humanizer-Python-Fork- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt
python -m spacy download en_core_web_sm
python -m nltk.downloader punkt punkt_tab wordnet averaged_perceptron_tagger- Run the API:
uvicorn main:app --reloadThe API will be available at: http://localhost:8000
Humanize AI-generated text while preserving citations.
Request Body:
{
"text": "Your AI-generated text here...",
"synonym_probability": 0.2,
"transition_probability": 0.2,
"hedging_probability": 0.15,
"sentence_combine_probability": 0.3
}Response:
{
"original_text": "...",
"humanized_text": "...",
"original_word_count": 150,
"humanized_word_count": 165,
"original_sentence_count": 8,
"humanized_sentence_count": 8
}Detect AI-generated content in text.
Request Body:
{
"text": "Your text to analyze..."
}Response:
{
"text": "...",
"classification_results": {
"First sentence.": "AI-generated",
"Second sentence.": "Human-written"
},
"percentages": {
"AI-generated": 45.5,
"AI-generated & AI-refined": 10.2,
"Human-written": 30.1,
"Human-written & AI-refined": 14.2
},
"summary": { ... }
}Root endpoint with API information.
Health check endpoint.
Interactive API documentation (Swagger UI).
Humanize Text:
curl -X POST "http://localhost:8000/humanize" \
-H "Content-Type: application/json" \
-d '{
"text": "AI is transforming industries. It is creating new opportunities.",
"synonym_probability": 0.3,
"transition_probability": 0.2,
"hedging_probability": 0.15,
"sentence_combine_probability": 0.3
}'Detect AI:
curl -X POST "http://localhost:8000/detect" \
-H "Content-Type: application/json" \
-d '{
"text": "The research methodology employed a quantitative approach."
}'import requests
# Humanize text
response = requests.post(
"http://localhost:8000/humanize",
json={
"text": "Your AI text here...",
"synonym_probability": 0.2,
"transition_probability": 0.2,
"hedging_probability": 0.15,
"sentence_combine_probability": 0.3
}
)
result = response.json()
print(result['humanized_text'])
# Detect AI
response = requests.post(
"http://localhost:8000/detect",
json={"text": "Text to analyze..."}
)
result = response.json()
print(result['percentages'])// Humanize text
fetch("http://localhost:8000/humanize", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: "Your AI text here...",
synonym_probability: 0.2,
transition_probability: 0.2,
}),
})
.then((res) => res.json())
.then((data) => console.log(data));
// Detect AI
fetch("http://localhost:8000/detect", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: "Text to analyze...",
}),
})
.then((res) => res.json())
.then((data) => console.log(data));Run the test script to verify everything works:
python test_api.pyOr visit the interactive documentation at: http://localhost:8000/docs
Text-Humanizer-Python-Fork/
βββ main.py # FastAPI application
βββ utils/
β βββ __init__.py
β βββ ai_detection_utils.py # AI detection logic
β βββ model_loaders.py # Model caching
β βββ text_humanizer.py # Text humanization logic
βββ requirements.txt # Python dependencies
βββ test_api.py # Test script
βββ .gitignore # Git ignore rules
βββ README.md # This file
- FastAPI - Modern web framework for building APIs
- Transformers - HuggingFace library for AI models
- spaCy - Industrial-strength NLP
- NLTK - Natural Language Toolkit
- PyTorch - Deep learning framework
- Uvicorn - ASGI server
| Parameter | Type | Default | Range | Description |
|---|---|---|---|---|
text |
string | required | - | Text to humanize |
synonym_probability |
float | 0.2 | 0.0-1.0 | Probability of replacing words with synonyms |
transition_probability |
float | 0.2 | 0.0-1.0 | Probability of adding academic transitions |
hedging_probability |
float | 0.15 | 0.0-1.0 | Probability of adding hedging language (e.g., "generally", "tends to") |
sentence_combine_probability |
float | 0.3 | 0.0-1.0 | Probability of intelligently combining short sentences |
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
string | required | Text to analyze for AI detection |
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.