Spaces:
Sleeping
Sleeping
MrrrMe Backend - Refactored Structure
This is a refactored version of backend_server.py split into modular components.
π Directory Structure
backend/
βββ __init__.py # Main package init
βββ app.py # FastAPI app setup
βββ config.py # Configuration & constants
βββ websocket.py # WebSocket handler (TO BE CREATED)
β
βββ auth/ # Authentication
β βββ __init__.py
β βββ models.py # Pydantic models
β βββ database.py # Database init & helpers
β βββ routes.py # Auth endpoints
β
βββ session/ # Session management
β βββ __init__.py
β βββ manager.py # Token validation, user context
β βββ summary.py # AI summary generation
β
βββ models/ # AI model loading
β βββ __init__.py
β βββ loader.py # Async model initialization
β
βββ processing/ # Core processing logic
β βββ __init__.py
β βββ video.py # Video frame processing
β βββ audio.py # Audio chunk processing
β βββ speech.py # Speech processing (TO BE CREATED)
β βββ fusion.py # Emotion fusion
β
βββ debug/ # Debug endpoints
β βββ __init__.py
β βββ routes.py # Debug routes
β
βββ utils/ # Utilities
βββ __init__.py
βββ helpers.py # Helper functions
βββ patches.py # GPU & system patches
π How to Use
Option 1: Drop-in Replacement
- Copy the
backend/folder tomrrrme/backend/ - Create/update
mrrrme/backend_new.py:
"""New modular backend server"""
from backend import app
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
- Run:
python mrrrme/backend_new.py
Option 2: Gradual Migration
Keep your old backend_server.py and slowly migrate endpoints:
- Import refactored modules:
from backend.auth import router as auth_router
from backend.session import validate_token
- Replace sections incrementally
π§ Still Need to Create
The following files need the full logic from backend_server.py:
websocket.py (~400 lines)
- Main WebSocket endpoint logic
- Message type routing
- Greeting generation
- Video/audio/speech handling orchestration
processing/speech.py (~250 lines)
- Full speech processing pipeline
- Transcription filtering
- Emotion detection coordination
- LLM context preparation
- Avatar TTS integration
π Migration Checklist
- Config & environment setup
- Authentication (signup/login/logout)
- Database management
- Session validation
- AI summary generation
- Model loading
- Video frame processing
- Audio chunk processing
- Emotion fusion logic
- Debug endpoints
- WebSocket handler (main logic)
- Speech processing pipeline
- Greeting generation logic
- Full integration testing
π― Benefits
- Modularity: Each component has a single responsibility
- Testability: Easy to unit test individual modules
- Maintainability: Find and fix bugs faster
- Scalability: Add new features without bloating
- Collaboration: Multiple devs can work on different modules
π¦ Original File Size
backend_server.py: 47KB (1,300 lines)- Refactored: 12 modules (~100-300 lines each)
β οΈ Important Notes
- Ensure all imports match your project structure
- Update
mrrrmemodule imports inmodels/loader.py - Test thoroughly before replacing production code
- Keep
backend_server.pyas backup during migration