A REST API that detects whether a voice sample is AI-generated or spoken by a real human. Built with FastAPI and Google Gemini AI.
- 🎤 Detects AI-generated vs Human voices
- 🌐 Supports 5 languages: Tamil, English, Hindi, Malayalam, Telugu
- 🔐 API Key authentication
- 📊 Returns confidence scores and explanations
- 🚀 Two-model fallback system for reliability
- 💰 Token-efficient audio processing
- ⚡ Intelligent audio duration reduction
- Light Model (
gemini-2.5-flash-lite): Tried first for cost efficiency - Heavy Model (
gemini-3-flash-preview): Fallback if light model fails
- Automatically reduces long audio to ~30 seconds (samples from beginning, middle, end)
- Converts to mono and reduces sample rate to save tokens
- Lowers bitrate for efficient processing
- Python 3.9+
- Google Gemini API Key
- FFmpeg (required by pydub for audio processing)
-
Clone or navigate to the project directory:
cd "c:\codes\Dev\Hackathon\Scam Detection"
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
# Windows venv\Scripts\activate # Linux/Mac source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment variables:
# Copy the example env file copy .env.example .env # Edit .env and add your Gemini API key
Edit the .env file with your settings:
# Gemini API Configuration
GEMINI_API_KEY=your_gemini_api_key_here
# Two-model system: Light model (cheaper) first, Heavy model (fallback)
GEMINI_MODEL_LIGHT=gemini-2.5-flash-lite
GEMINI_MODEL_HEAVY=gemini-2.0-flash
# API Authentication
API_SECRET_KEY=sk_test_123456789
# Server Configuration
HOST=0.0.0.0
PORT=8000
# Audio Processing Configuration
MAX_AUDIO_DURATION_SECONDS=30
AUDIO_SAMPLE_RATE=16000python run.pyOr using uvicorn directly:
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadThe API will be available at http://localhost:8000
Once the server is running, visit:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
POST /api/voice-detection
Analyzes an audio sample to determine if it's AI-generated or human-spoken.
Headers:
Content-Type: application/json
x-api-key: YOUR_API_KEY
Request Body:
{
"language": "Tamil",
"audioFormat": "mp3",
"audioBase64": "SUQzBAAAAAAA..."
}Success Response (200):
{
"status": "success",
"language": "Tamil",
"classification": "AI_GENERATED",
"confidenceScore": 0.91,
"explanation": "Unnatural pitch consistency and robotic speech patterns detected"
}Error Response (400/401/500):
{
"status": "error",
"message": "Invalid API key or malformed request"
}GET /api/health
Returns the API health status.
Response:
{
"status": "healthy",
"service": "AI Voice Detection API",
"supported_languages": ["Tamil", "English", "Hindi", "Malayalam", "Telugu"]
}curl -X POST http://localhost:8000/api/voice-detection \
-H "Content-Type: application/json" \
-H "x-api-key: sk_test_123456789" \
-d '{
"language": "Tamil",
"audioFormat": "mp3",
"audioBase64": "SUQzBAAAAAAAAI1RTU..."
}'import requests
import base64
# Read and encode audio file
with open("audio.mp3", "rb") as f:
audio_base64 = base64.b64encode(f.read()).decode("utf-8")
# Make API request
response = requests.post(
"http://localhost:8000/api/voice-detection",
json={
"language": "English",
"audioFormat": "mp3",
"audioBase64": audio_base64
},
headers={
"Content-Type": "application/json",
"x-api-key": "sk_test_123456789"
}
)
print(response.json())Run the test suite:
python test_api.py| Language | Code |
|---|---|
| Tamil | Tamil |
| English | English |
| Hindi | Hindi |
| Malayalam | Malayalam |
| Telugu | Telugu |
AI_GENERATED→ Voice created using AI or synthetic systemsHUMAN→ Voice spoken by a real human
| Field | Description |
|---|---|
| status | success or error |
| language | Language of the audio |
| classification | AI_GENERATED or HUMAN |
| confidenceScore | Value between 0.0 and 1.0 |
| explanation | Short reason for the decision |
Scam Detection/
├── app/
│ ├── __init__.py
│ ├── auth.py # API key authentication
│ ├── config.py # Configuration settings
│ ├── main.py # FastAPI application
│ ├── models.py # Pydantic models
│ ├── routes.py # API routes
│ └── voice_detector.py # Gemini AI integration
├── .env # Environment variables
├── .env.example # Example environment file
├── requirements.txt # Python dependencies
├── run.py # Server startup script
├── test_api.py # API test suite
└── README.md # This file
This project is for educational/hackathon purposes.