File size: 7,109 Bytes
ae77244 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | import os
import json
import time
import asyncio
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from sse_starlette.sse import EventSourceResponse
from fastapi.staticfiles import StaticFiles
# Force backend to fp16 for the API server
config_path = "configs/inference_config.json"
if os.path.exists(config_path):
with open(config_path, "r") as f:
config = json.load(f)
if config.get("backend") != "fp16":
config["backend"] = "fp16"
with open(config_path, "w") as f:
json.dump(config, f, indent=4)
# MLflow DagsHub Authentication
from dotenv import load_dotenv
import mlflow
load_dotenv()
owner = os.getenv("DAGSHUB_REPO_OWNER")
name = os.getenv("DAGSHUB_REPO_NAME")
token = os.getenv("MLFLOW_TRACKING_PASSWORD")
# DagsHub's MLflow proxy authenticates as whoever the token belongs to, which
# isn't necessarily the repo owner (e.g. a collaborator's own personal access
# token). Defaults to owner for backward compatibility, but set
# DAGSHUB_USERNAME explicitly if your token was generated under a different
# account -- otherwise every request 401s regardless of how many times the
# token is regenerated.
username = os.getenv("DAGSHUB_USERNAME", owner)
if owner and name and token:
import dagshub
dagshub.auth.add_app_token(token)
os.environ["MLFLOW_TRACKING_USERNAME"] = username
os.environ["MLFLOW_TRACKING_PASSWORD"] = token
mlflow.set_tracking_uri(f"https://dagshub.com/{owner}/{name}.mlflow")
import importlib
infer_module = importlib.import_module("scripts.10_inference_pipeline")
pipeline = infer_module.InferencePipeline()
app = FastAPI(title="Scam Detection API", description="Live Voice Scam Detection", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
os.makedirs("data/uploads", exist_ok=True)
# In-memory per-session running transcript for the live 3-second-window
# monitor. Keyed by a client-generated session_id. Fine for a single
# Railway instance; wipe on restart / doesn't survive multiple replicas.
LIVE_WINDOW_SEC = 3.0
live_sessions = {}
@app.post("/live_chunk")
async def live_chunk(file: UploadFile = File(...), session_id: str = Form(...)):
"""Receives one ~3s audio window from the browser mic, transcribes just
that window, appends the text to the session's running transcript, then
re-classifies the full transcript so far."""
file_path = f"data/uploads/live_{session_id}_{int(time.time() * 1000)}_{file.filename}"
with open(file_path, "wb") as f:
f.write(await file.read())
try:
t0 = time.time()
chunk_text = (await asyncio.to_thread(pipeline._transcribe, file_path)).strip()
asr_latency = time.time() - t0
transcript = live_sessions.setdefault(session_id, "")
if chunk_text:
transcript = (transcript + " " + chunk_text).strip()
live_sessions[session_id] = transcript
t1 = time.time()
if transcript:
prediction = await asyncio.to_thread(pipeline._classify, transcript)
else:
prediction = "Legitimate (No speech yet)"
clf_latency = time.time() - t1
return {
"transcript": transcript,
"chunk_text": chunk_text,
"prediction": prediction,
"asr_latency": asr_latency,
"clf_latency": clf_latency,
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
finally:
if os.path.exists(file_path):
os.remove(file_path)
@app.post("/live_reset")
async def live_reset(session_id: str = Form(...)):
live_sessions.pop(session_id, None)
return {"status": "reset"}
@app.post("/detect")
async def detect_scam(file: UploadFile = File(...)):
"""Standard blocking endpoint. Returns everything at once."""
file_path = f"data/uploads/{int(time.time())}_{file.filename}"
with open(file_path, "wb") as f:
f.write(await file.read())
try:
# Run blocking CPU/GPU task in a thread
res = await asyncio.to_thread(pipeline.process_audio, file_path)
return res
finally:
if os.path.exists(file_path):
os.remove(file_path)
@app.post("/detect_stream")
async def detect_stream(file: UploadFile = File(...)):
"""Streaming endpoint (SSE) to update the UI step-by-step."""
file_path = f"data/uploads/{int(time.time())}_{file.filename}"
with open(file_path, "wb") as f:
f.write(await file.read())
print(f"[API] Received audio file: {file.filename}")
async def event_generator():
try:
# 1. Start Transcription
print("[API] Starting Transcription...")
yield {"event": "status", "data": json.dumps({"step": "transcribing", "message": "Converting Audio to Text..."})}
await asyncio.sleep(0.1) # Yield control to flush message
t0 = time.time()
# Run blocking Whisper inference in a threadpool so we don't freeze the async event loop!
transcript = await asyncio.to_thread(pipeline._transcribe, file_path)
asr_latency = time.time() - t0
print(f"[API] Transcription finished in {asr_latency:.3f}s: {transcript}")
# 2. Transcription Finished
yield {"event": "transcript", "data": json.dumps({"text": transcript, "latency": f"{asr_latency:.3f}s"})}
await asyncio.sleep(0.1)
# 3. Start Classification
print("[API] Starting ModernBERT Classification...")
yield {"event": "status", "data": json.dumps({"step": "classifying", "message": "Analyzing Intent via ModernBERT..."})}
await asyncio.sleep(0.1)
t1 = time.time()
# Run blocking PyTorch classification in threadpool
prediction = await asyncio.to_thread(pipeline._classify, transcript)
clf_latency = time.time() - t1
print(f"[API] Classification finished in {clf_latency:.3f}s: {prediction}")
# 4. Final Result
yield {"event": "result", "data": json.dumps({
"prediction": prediction,
"asr_latency": asr_latency,
"clf_latency": clf_latency,
"total_latency": asr_latency + clf_latency
})}
except Exception as e:
print(f"[API] ERROR: {e}")
yield {"event": "error", "data": str(e)}
finally:
if os.path.exists(file_path):
os.remove(file_path)
print("[API] Cleaned up temporary file.")
return EventSourceResponse(event_generator())
os.makedirs("static", exist_ok=True)
app.mount("/", StaticFiles(directory="static", html=True), name="static")
if __name__ == "__main__":
import uvicorn
print("Starting Scam Detection Server on http://0.0.0.0:8000")
uvicorn.run(app, port=8000)
|