An advanced, real-time customer experience analytics platform engineered to detect emotional trajectory shifts in support dialogues. Powered by a fine-tuned DistilBERT Transformer model trained on the 28-class GoEmotions dataset, the system performs vectorized drift calculations and predictive SLA risk analysis. Designed for high-performance operations, the project supports two deployment topologies: a scalable Web Application and a fully containerized Electron Desktop Client with an offline portable backend.
The platform supports hybrid delivery models depending on operational constraints:
Operates on cost-efficient, highly scalable serverless and containerized tiers:
- Frontend SPA: Built in React 18, Vite, and TypeScript. Deployed to Vercel Free, utilizing static edge routing.
- Microservice Backend: Built on FastAPI and Uvicorn. Deployed inside a CPU-optimized Docker container on Hugging Face Spaces. Communicates via secure, CORS-restricted RESTful APIs.
Packages the complete stack into a standalone, portable Windows installer:
- Application Shell: Electron container running a native desktop window.
- Process Orchestration: Dynamically manages the lifecycle of a background python interpreter using relative virtual environment pathways (
env/Scripts/python.exe), bypassing system-wide Python dependencies. - Process Safety: Node-native process tree tree cleanup handles process signals to prevent orphaned Python or Uvicorn worker threads.
graph TD
subgraph Cloud Topology
W_FE[React SPA : Vercel] -->|HTTPS REST| HF_BE[FastAPI Microservice : Hugging Face Spaces]
end
subgraph Desktop Topology
E_Shell[Electron Desktop Shell] -->|Lifecycle Spawn| PY_BE[FastAPI Microservice : Localhost:8000]
E_Shell -->|Render Loop| E_FE[React Static Assets]
E_FE -->|HTTP API Telemetry| PY_BE
end
The system computes real-time dialogue risk using three consecutive processing pipelines:
- Employs a fine-tuned DistilBERT architecture that processes textual dialogue segments to output probability distributions over support-oriented categories.
- Translates machine predictions into 7 normalized, human-centric support tags:
STRESSED(Anxiety, nervous indicators)OVERWHELMED(Sadness, remorse, workload exhaustion)FRUSTRATED(Disappointment, annoyance)SATISFIED(Joy, gratitude, approval)RELIEVED(Resolution acknowledgment)CONFUSED(Puzzled states, curiosity loops)ANGRY(High-intensity negative friction)NEUTRAL(Nominal fallback state)
- Incorporates rule-based syntactic parsing to handle soft-negative expressions and modifiers that frequently trigger false "Neutral" classifications in basic models.
- Heuristic Overrides Table:
Modifiers / Phrase Patterns Resolved NLP Class Drift Coefficient UI Glow Mapping "not happy","not too happy","not really happy"disappointment0.5Soft Red "not great","not feeling well","not doing great"sadness0.5Soft Purple "a bit frustrated","somewhat disappointed"annoyance0.8Neon Orange "overwhelmed by workload","extreme stress","workload"nervousness0.6Purple-Blue
- Calculates Drift Velocity in real-time by analyzing the emotional distance vectors between consecutive customer inputs.
- Triggers a proactive SLA Breach Alert when the emotional trajectory shows escalating frustration (
NEUTRAL$\rightarrow$ CONFUSED$\rightarrow$ ANGRY/FRUSTRATED) or repeated friction markers. - Automatically initiates recommended decisions via the Decision Engine Panel (e.g., automated response suppression, supervisor escalation, or tier-2 queue re-routing).
For demonstration and enterprise-readiness verification, the monitor integrates a high-fidelity conversation simulation engine:
- Interactive Scenarios: Playback templates simulate actual transaction issues (e.g., Subscription Cancellation Resistance, Mobile Checkout Friction, Empathetic Happy Path Rescue).
- Cinematic Delays: Mimics human customer input delays (
1.5s - 2.5s) and automated AI processing cycles (1.8s - 2.8s) with animated, responsive typing states. - Robust Lifecycle Safety: Features comprehensive React
useRefscheduling. Ensures all timers, timeouts, and callbacks are terminated instantly upon manual ticket switches, scenario cancellations, or component unmounting to completely eliminate memory leaks. - Manual Override Mode: Allows the operator to inject custom simulated customer messages using direct keyboard input at any point during or after simulation loops.
βββ app.py # Main production FastAPI application script
βββ hf-backend/ # Dedicated CPU-reduced backend code for Hugging Face
β βββ app.py # Optimized space server script with dynamic Port binding
β βββ requirements.txt # CPU-only PyTorch and Transformers requirements
βββ emotion_model.py # DistilBERT model loading, tokenization, and negation mapping
βββ drift_detector.py # Vectorized sentiment trajectory drift algorithms
βββ escalation.py # Rule-based SLA risk calculation and trigger engine
βββ main.js # Electron main process controller with relative Python spawn routines
βββ splash.html # Double-rotating neon orbit desktop splash screen
βββ package.json # Project root dependency manifest & build scripts
βββ requirements.txt # Main Python virtualenv package lists
βββ frontend/ # React Single-Page-Application directory
βββ src/ # Component files (LiveStream, Analytics, Alerts, Settings)
βββ package.json # React developer dependencies
βββ vercel.json # Static path SPA routing mapping for Vercel Free
- Python 3.10
- Node.js 18.x or higher (with
npm)
- Clone & Navigate:
git clone https://github.com/Yash-Singh607/Emotion_Drift_Detection.git cd Emotion_Drift_Detection - Setup Python Virtual Environment:
python -m venv env # Activate on Windows: .\env\Scripts\activate # Install Core ML & Web Packages: pip install -r requirements.txt
- Setup Node Package Managers:
npm install npm run install --prefix frontend
-
Web Paradigm (Concurrent Dev):
- Microservice (Terminal 1):
.\env\Scripts\activate uvicorn app:app --reload --port 8000
- Frontend SPA (Terminal 2):
npm run dev:web
- Console Portal:
http://localhost:5173
- Microservice (Terminal 1):
-
Desktop Paradigm (Electron Dev):
npm run dev:desktop
- Compile React Static Assets:
npm run build:web
- Generate Standalone Desktop Installer:
npm run build:desktop
For complete, detailed cloud deployment scripts, refer to deployment_instructions.md.
- Initialize the deployment from the
frontend/directory using Vercel CLI:cd frontend vercel - Add the environment variable
VITE_API_URLpointing to your running Hugging Face Space endpoint (e.g.,https://username-space.hf.space).
- Create a new Space on Hugging Face using the Docker platform option.
- Push the files under
hf-backend/along with your fine-tuned model weights directory (emotion_model_trained_final/). - Hugging Face will build the container with optimized CPU-only PyTorch libraries, running the service on port
7860. - Configure
CORS_ALLOWED_ORIGINSin your Space settings to whitelist your Vercel frontend domain.
- CORS Protection: Enforces strict origin matching utilizing
fastapi.middleware.cors. - Large File Management: Model checkpoint matrices (
model.safetensors, 268MB) are tracked via Git LFS (.gitattributes) to prevent sync blocks. - Memory Optimization: Uses optimized batch sizes and CPU PyTorch libraries to maintain RAM footprint below
1.5 GBin standard container instances.
- Set frontend env values in
frontend/.env.example(VITE_API_URL,VITE_APP_MODE=production,VITE_API_TIMEOUT_MS). - Set backend env values from
.env.production.example(APP_ENV=production,CORS_ALLOWED_ORIGINS,TRUSTED_HOSTS,PORT,LOG_LEVEL). - Use strong secrets (
AUTH_SECRET_KEY,SESSION_SECRET) with at least 32 characters. - Keep
AUTH_EXPOSE_TOKENS=falsein production. - Run quality gates locally:
npm run lint --prefix frontendnpm run test --prefix frontendnpm run build --prefix frontendpytest -q
- Backend readiness:
GET /healthshould return{ "status": "ok" }. - API runtime headers should include
x-request-idandx-process-time-ms. - API security headers should include
x-content-type-options,x-frame-options, andreferrer-policy. - Frontend operator badge in Live Stream should report
API_LINK: ONLINE.
- Users can sign in with email/password or OAuth (
Google,GitHub). - New local accounts must verify email before accessing protected prediction routes.
- Password reset uses secure one-time tokens (
/auth/forgot-password->/auth/reset-password). - API endpoints
/predict-emotion,/predict, and/timelinerequire a verified bearer token. /resetand role-management endpoints are role-gated (admin/agentonly for reset,adminonly for user-role management).- Auth routes:
POST /auth/registerPOST /auth/loginGET /auth/mePOST /auth/resend-verificationPOST /auth/verify-emailPOST /auth/forgot-passwordPOST /auth/reset-passwordGET /auth/oauth/google/startGET /auth/oauth/github/startGET /auth/users(admin)PATCH /auth/users/{user_id}/role(admin)
- Keep previous frontend deployment alias in Vercel and redeploy previous successful build.
- Revert backend container/image to last known working commit in Hugging Face Space.
- If incident scope is unknown, rotate to prior commit and disable new feature flags by keeping
VITE_APP_MODE=production.
- Inspect backend request logs for request path, status code, request id, and response latency.
- Use
/timeline?session_id=<id>to inspect ticket-scoped inference state. - If frontend shows API offline, verify backend host reachability and CORS allowlist mismatch first.