scam_dectect_sheid / api_server.py
adsutraemp1
init
ae77244
Raw History Blame Contribute Delete
7.11 kB
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)