Spaces:
Sleeping
Sleeping
File size: 3,877 Bytes
10fba92 |
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 |
# 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
1. Copy the `backend/` folder to `mrrrme/backend/`
2. Create/update `mrrrme/backend_new.py`:
```python
"""New modular backend server"""
from backend import app
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
```
3. Run: `python mrrrme/backend_new.py`
### Option 2: Gradual Migration
Keep your old `backend_server.py` and slowly migrate endpoints:
1. Import refactored modules:
```python
from backend.auth import router as auth_router
from backend.session import validate_token
```
2. 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
- [x] Config & environment setup
- [x] Authentication (signup/login/logout)
- [x] Database management
- [x] Session validation
- [x] AI summary generation
- [x] Model loading
- [x] Video frame processing
- [x] Audio chunk processing
- [x] Emotion fusion logic
- [x] Debug endpoints
- [ ] WebSocket handler (main logic)
- [ ] Speech processing pipeline
- [ ] Greeting generation logic
- [ ] Full integration testing
## π― Benefits
1. **Modularity**: Each component has a single responsibility
2. **Testability**: Easy to unit test individual modules
3. **Maintainability**: Find and fix bugs faster
4. **Scalability**: Add new features without bloating
5. **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 `mrrrme` module imports in `models/loader.py`
- Test thoroughly before replacing production code
- Keep `backend_server.py` as backup during migration
|