Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,38 +5,24 @@ import librosa
|
|
| 5 |
from fastapi import FastAPI, Header, HTTPException, Depends
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from transformers import pipeline
|
| 8 |
-
|
| 9 |
-
# =========================
|
| 10 |
-
# CONFIG
|
| 11 |
-
# =========================
|
| 12 |
API_KEY = "SynxsOG"
|
| 13 |
SUPPORTED_LANGUAGES = {"Tamil", "English", "Hindi", "Malayalam", "Telugu"}
|
| 14 |
MODEL_NAME = "MelodyMachine/Deepfake-audio-detection-V2"
|
| 15 |
-
|
| 16 |
-
# Load the model - This model uses LABEL_0 for Human and LABEL_1 for AI
|
| 17 |
detector = pipeline("audio-classification", model=MODEL_NAME)
|
| 18 |
-
|
| 19 |
app = FastAPI(title="AI Generated Voice Detection API")
|
| 20 |
-
|
| 21 |
class VoiceRequest(BaseModel):
|
| 22 |
language: str
|
| 23 |
audioFormat: str
|
| 24 |
audioBase64: str
|
| 25 |
-
|
| 26 |
def verify_api_key(x_api_key: str = Header(None)):
|
| 27 |
if x_api_key != API_KEY:
|
| 28 |
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 29 |
return x_api_key
|
| 30 |
-
|
| 31 |
-
# =========================
|
| 32 |
-
# AUDIO & FORENSIC UTILS
|
| 33 |
-
# =========================
|
| 34 |
def load_audio(base64_audio: str):
|
| 35 |
clean_base64 = base64_audio.split(",")[-1]
|
| 36 |
audio_bytes = base64.b64decode(clean_base64)
|
| 37 |
y, sr = librosa.load(io.BytesIO(audio_bytes), sr=16000)
|
| 38 |
return y, sr
|
| 39 |
-
|
| 40 |
def get_forensics(y, sr):
|
| 41 |
# Forensic analysis to check for natural variation
|
| 42 |
pitch = librosa.yin(y, fmin=50, fmax=300)
|
|
@@ -44,55 +30,31 @@ def get_forensics(y, sr):
|
|
| 44 |
rms = librosa.feature.rms(y=y)[0]
|
| 45 |
rms_std = np.std(rms)
|
| 46 |
return {"pitch_std": pitch_std, "rms_std": rms_std}
|
| 47 |
-
|
| 48 |
-
# =========================
|
| 49 |
-
# FIXED HYBRID LOGIC
|
| 50 |
-
# =========================
|
| 51 |
def hybrid_decision(model_label, model_score, forensic_data):
|
| 52 |
-
# CRITICAL FIX 1: Correct Label Mapping
|
| 53 |
-
# LABEL_1 is AI/Fake, LABEL_0 is Human/Real
|
| 54 |
if model_label == "LABEL_1":
|
| 55 |
is_ai = True
|
| 56 |
base_prob = model_score
|
| 57 |
else:
|
| 58 |
is_ai = False
|
| 59 |
-
base_prob = 1 - model_score
|
| 60 |
-
|
| 61 |
-
# CRITICAL FIX 2: Realistic Forensic Penalties
|
| 62 |
-
# If it looks like a human (high variation), reduce AI probability
|
| 63 |
adjustment = 0
|
| 64 |
if forensic_data["pitch_std"] > 15:
|
| 65 |
adjustment -= 0.10
|
| 66 |
if forensic_data["rms_std"] > 0.01:
|
| 67 |
adjustment -= 0.05
|
| 68 |
-
|
| 69 |
final_ai_prob = max(0.01, min(base_prob + adjustment, 0.99))
|
| 70 |
-
|
| 71 |
-
# CRITICAL FIX 3: Result Logic
|
| 72 |
-
if final_ai_prob > 0.5:
|
| 73 |
return "AI_GENERATED", round(final_ai_prob, 4)
|
| 74 |
else:
|
| 75 |
-
# Confidence in it being human is 1 - AI probability
|
| 76 |
return "HUMAN", round(1 - final_ai_prob, 4)
|
| 77 |
-
|
| 78 |
-
# =========================
|
| 79 |
-
# API ENDPOINT
|
| 80 |
-
# =========================
|
| 81 |
@app.post("/api/voice-detection")
|
| 82 |
async def detect_voice(data: VoiceRequest, api_key: str = Depends(verify_api_key)):
|
| 83 |
try:
|
| 84 |
y, sr = load_audio(data.audioBase64)
|
| 85 |
-
|
| 86 |
-
# 1. Get Model Prediction
|
| 87 |
preds = detector(y)
|
| 88 |
top = preds[0]
|
| 89 |
-
|
| 90 |
-
# 2. Get Forensic Data
|
| 91 |
f_data = get_forensics(y, sr)
|
| 92 |
-
|
| 93 |
-
# 3. Get Hybrid Result
|
| 94 |
classification, confidence = hybrid_decision(top["label"], top["score"], f_data)
|
| 95 |
-
|
| 96 |
return {
|
| 97 |
"status": "success",
|
| 98 |
"classification": classification,
|
|
|
|
| 5 |
from fastapi import FastAPI, Header, HTTPException, Depends
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
API_KEY = "SynxsOG"
|
| 9 |
SUPPORTED_LANGUAGES = {"Tamil", "English", "Hindi", "Malayalam", "Telugu"}
|
| 10 |
MODEL_NAME = "MelodyMachine/Deepfake-audio-detection-V2"
|
|
|
|
|
|
|
| 11 |
detector = pipeline("audio-classification", model=MODEL_NAME)
|
|
|
|
| 12 |
app = FastAPI(title="AI Generated Voice Detection API")
|
|
|
|
| 13 |
class VoiceRequest(BaseModel):
|
| 14 |
language: str
|
| 15 |
audioFormat: str
|
| 16 |
audioBase64: str
|
|
|
|
| 17 |
def verify_api_key(x_api_key: str = Header(None)):
|
| 18 |
if x_api_key != API_KEY:
|
| 19 |
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 20 |
return x_api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def load_audio(base64_audio: str):
|
| 22 |
clean_base64 = base64_audio.split(",")[-1]
|
| 23 |
audio_bytes = base64.b64decode(clean_base64)
|
| 24 |
y, sr = librosa.load(io.BytesIO(audio_bytes), sr=16000)
|
| 25 |
return y, sr
|
|
|
|
| 26 |
def get_forensics(y, sr):
|
| 27 |
# Forensic analysis to check for natural variation
|
| 28 |
pitch = librosa.yin(y, fmin=50, fmax=300)
|
|
|
|
| 30 |
rms = librosa.feature.rms(y=y)[0]
|
| 31 |
rms_std = np.std(rms)
|
| 32 |
return {"pitch_std": pitch_std, "rms_std": rms_std}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def hybrid_decision(model_label, model_score, forensic_data):
|
|
|
|
|
|
|
| 34 |
if model_label == "LABEL_1":
|
| 35 |
is_ai = True
|
| 36 |
base_prob = model_score
|
| 37 |
else:
|
| 38 |
is_ai = False
|
| 39 |
+
base_prob = 1 - model_score
|
|
|
|
|
|
|
|
|
|
| 40 |
adjustment = 0
|
| 41 |
if forensic_data["pitch_std"] > 15:
|
| 42 |
adjustment -= 0.10
|
| 43 |
if forensic_data["rms_std"] > 0.01:
|
| 44 |
adjustment -= 0.05
|
|
|
|
| 45 |
final_ai_prob = max(0.01, min(base_prob + adjustment, 0.99))
|
| 46 |
+
if final_ai_prob <0.5:
|
|
|
|
|
|
|
| 47 |
return "AI_GENERATED", round(final_ai_prob, 4)
|
| 48 |
else:
|
|
|
|
| 49 |
return "HUMAN", round(1 - final_ai_prob, 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
@app.post("/api/voice-detection")
|
| 51 |
async def detect_voice(data: VoiceRequest, api_key: str = Depends(verify_api_key)):
|
| 52 |
try:
|
| 53 |
y, sr = load_audio(data.audioBase64)
|
|
|
|
|
|
|
| 54 |
preds = detector(y)
|
| 55 |
top = preds[0]
|
|
|
|
|
|
|
| 56 |
f_data = get_forensics(y, sr)
|
|
|
|
|
|
|
| 57 |
classification, confidence = hybrid_decision(top["label"], top["score"], f_data)
|
|
|
|
| 58 |
return {
|
| 59 |
"status": "success",
|
| 60 |
"classification": classification,
|