Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,27 @@
|
|
| 1 |
"""
|
| 2 |
-
Vocal Guard
|
| 3 |
-
FastAPI
|
| 4 |
"""
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.staticfiles import StaticFiles
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
import uvicorn
|
| 10 |
-
import numpy as np
|
| 11 |
-
import io
|
| 12 |
-
import os
|
| 13 |
-
import logging
|
| 14 |
|
| 15 |
from detector import VocalGuardDetector
|
| 16 |
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
|
|
|
|
|
|
| 20 |
app = FastAPI(
|
| 21 |
title="Vocal Guard API",
|
| 22 |
-
description="
|
| 23 |
-
version=
|
| 24 |
)
|
| 25 |
|
| 26 |
app.add_middleware(
|
|
@@ -33,42 +34,45 @@ app.add_middleware(
|
|
| 33 |
|
| 34 |
detector = VocalGuardDetector()
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
app.mount("/static", StaticFiles(directory=os.path.dirname(__file__)), name="static")
|
| 39 |
|
| 40 |
|
| 41 |
@app.get("/")
|
| 42 |
async def root():
|
| 43 |
-
return FileResponse(os.path.join(
|
| 44 |
|
| 45 |
|
| 46 |
@app.get("/health")
|
| 47 |
async def health_check():
|
| 48 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
@app.post("/analyze")
|
| 52 |
-
async def analyze_audio(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"""
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
):
|
| 61 |
-
logger.warning(f"
|
| 62 |
|
| 63 |
audio_bytes = await file.read()
|
| 64 |
-
|
| 65 |
if len(audio_bytes) < 100:
|
| 66 |
raise HTTPException(status_code=400, detail="Audio too short or empty")
|
| 67 |
|
| 68 |
-
logger.info(f"
|
| 69 |
-
|
| 70 |
-
result = detector.predict(audio_bytes)
|
| 71 |
-
return result
|
| 72 |
|
| 73 |
except HTTPException:
|
| 74 |
raise
|
|
@@ -78,24 +82,22 @@ async def analyze_audio(file: UploadFile = File(...)):
|
|
| 78 |
|
| 79 |
|
| 80 |
@app.post("/analyze-stream")
|
| 81 |
-
async def analyze_stream(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"""
|
| 86 |
try:
|
|
|
|
| 87 |
audio_bytes = await file.read()
|
| 88 |
if len(audio_bytes) < 50:
|
| 89 |
return {"label": "unknown", "confidence": 0, "processing_ms": 0}
|
| 90 |
-
|
| 91 |
-
result = detector.predict_fast(audio_bytes)
|
| 92 |
-
return result
|
| 93 |
-
|
| 94 |
except Exception as e:
|
| 95 |
logger.error(f"Stream analysis error: {e}")
|
| 96 |
-
return {"label": "unknown", "confidence": 0
|
| 97 |
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
-
port = int(os.environ.get("PORT",
|
| 101 |
uvicorn.run("app:app", host="0.0.0.0", port=port, reload=False)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Vocal Guard — Deepfake Audio Detector
|
| 3 |
+
FastAPI backend.
|
| 4 |
"""
|
| 5 |
+
import os
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from fastapi.staticfiles import StaticFiles
|
| 11 |
from fastapi.responses import FileResponse
|
| 12 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
from detector import VocalGuardDetector
|
| 15 |
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
+
APP_VERSION = "8.0.0"
|
| 20 |
+
|
| 21 |
app = FastAPI(
|
| 22 |
title="Vocal Guard API",
|
| 23 |
+
description="Deepfake audio detection (wav2vec2-based classifier)",
|
| 24 |
+
version=APP_VERSION,
|
| 25 |
)
|
| 26 |
|
| 27 |
app.add_middleware(
|
|
|
|
| 34 |
|
| 35 |
detector = VocalGuardDetector()
|
| 36 |
|
| 37 |
+
HERE = os.path.dirname(__file__)
|
| 38 |
+
app.mount("/static", StaticFiles(directory=HERE), name="static")
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
@app.get("/")
|
| 42 |
async def root():
|
| 43 |
+
return FileResponse(os.path.join(HERE, "index.html"))
|
| 44 |
|
| 45 |
|
| 46 |
@app.get("/health")
|
| 47 |
async def health_check():
|
| 48 |
+
return {
|
| 49 |
+
"status": "healthy",
|
| 50 |
+
"version": APP_VERSION,
|
| 51 |
+
"model_loaded": detector.local_model is not None,
|
| 52 |
+
}
|
| 53 |
|
| 54 |
|
| 55 |
@app.post("/analyze")
|
| 56 |
+
async def analyze_audio(
|
| 57 |
+
file: UploadFile = File(...),
|
| 58 |
+
source: str = Form("file"), # frontend sends 'mic' or 'file'
|
| 59 |
+
):
|
| 60 |
+
"""Analyze an audio file/recording for deepfake detection."""
|
| 61 |
try:
|
| 62 |
+
is_mic = source.strip().lower() == "mic"
|
| 63 |
+
|
| 64 |
+
if file.content_type and not any(
|
| 65 |
+
ct in file.content_type
|
| 66 |
+
for ct in ["audio", "octet-stream", "webm", "wav", "ogg"]
|
| 67 |
):
|
| 68 |
+
logger.warning(f"Unexpected content type: {file.content_type}")
|
| 69 |
|
| 70 |
audio_bytes = await file.read()
|
|
|
|
| 71 |
if len(audio_bytes) < 100:
|
| 72 |
raise HTTPException(status_code=400, detail="Audio too short or empty")
|
| 73 |
|
| 74 |
+
logger.info(f"Analyzing {len(audio_bytes)} bytes | source={source}")
|
| 75 |
+
return detector.predict(audio_bytes, is_mic=is_mic)
|
|
|
|
|
|
|
| 76 |
|
| 77 |
except HTTPException:
|
| 78 |
raise
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
@app.post("/analyze-stream")
|
| 85 |
+
async def analyze_stream(
|
| 86 |
+
file: UploadFile = File(...),
|
| 87 |
+
source: str = Form("mic"),
|
| 88 |
+
):
|
| 89 |
+
"""Lightweight endpoint for streaming/real-time analysis."""
|
| 90 |
try:
|
| 91 |
+
is_mic = source.strip().lower() == "mic"
|
| 92 |
audio_bytes = await file.read()
|
| 93 |
if len(audio_bytes) < 50:
|
| 94 |
return {"label": "unknown", "confidence": 0, "processing_ms": 0}
|
| 95 |
+
return detector.predict_fast(audio_bytes, is_mic=is_mic)
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
logger.error(f"Stream analysis error: {e}")
|
| 98 |
+
return {"label": "unknown", "confidence": 0, "processing_ms": 0}
|
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
port = int(os.environ.get("PORT", 7860))
|
| 103 |
uvicorn.run("app:app", host="0.0.0.0", port=port, reload=False)
|