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