Skip to content

Latest commit

 

History

History
275 lines (200 loc) · 5.71 KB

README.md

File metadata and controls

275 lines (200 loc) · 5.71 KB

AI-Generated Voice Detection API

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.

Features

  • 🎤 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

Architecture

Two-Model System

  • Light Model (gemini-2.5-flash-lite): Tried first for cost efficiency
  • Heavy Model (gemini-3-flash-preview): Fallback if light model fails

Audio Optimization

  • 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

Prerequisites

  • Python 3.9+
  • Google Gemini API Key
  • FFmpeg (required by pydub for audio processing)

Installation

  1. Clone or navigate to the project directory:

    cd "c:\codes\Dev\Hackathon\Scam Detection"
  2. Create a virtual environment:

    python -m venv venv
  3. Activate the virtual environment:

    # Windows
    venv\Scripts\activate
    
    # Linux/Mac
    source venv/bin/activate
  4. Install dependencies:

    pip install -r requirements.txt
  5. Configure environment variables:

    # Copy the example env file
    copy .env.example .env
    
    # Edit .env and add your Gemini API key

Configuration

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=16000

Running the Server

python run.py

Or using uvicorn directly:

uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload

The API will be available at http://localhost:8000

API Documentation

Once the server is running, visit:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

API Endpoints

1. Voice Detection

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"
}

2. Health Check

GET /api/health

Returns the API health status.

Response:

{
    "status": "healthy",
    "service": "AI Voice Detection API",
    "supported_languages": ["Tamil", "English", "Hindi", "Malayalam", "Telugu"]
}

cURL Example

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..."
  }'

Python Example

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())

Testing

Run the test suite:

python test_api.py

Supported Languages

Language Code
Tamil Tamil
English English
Hindi Hindi
Malayalam Malayalam
Telugu Telugu

Classification Rules

  • AI_GENERATED → Voice created using AI or synthetic systems
  • HUMAN → Voice spoken by a real human

Response Fields

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

Project Structure

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

License

This project is for educational/hackathon purposes.