-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenhanced_backend.py
More file actions
213 lines (191 loc) · 9.02 KB
/
enhanced_backend.py
File metadata and controls
213 lines (191 loc) · 9.02 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
"""
Enhanced backend with simpler, more reliable data structure for frontend
"""
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
import urllib.parse
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class SimpleCORSHandler(BaseHTTPRequestHandler):
def do_OPTIONS(self):
"""Handle preflight CORS requests"""
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
def do_POST(self):
"""Handle POST requests"""
try:
# Add CORS headers
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
# Parse the request path
parsed_path = urllib.parse.urlparse(self.path)
# Read request body
content_length = int(self.headers.get('Content-Length', 0))
post_data = self.rfile.read(content_length).decode('utf-8')
logger.info(f"Received POST to {self.path}")
logger.info(f"Request data: {post_data}")
if '/api/analyze/analyze-filename' in self.path:
# Return analysis data in the EXACT format the frontend expects
response = {
"success": True,
"analysis": {
# Emotions - should map to frontend emotions array
"emotions": [
{"timestamp": "0:15", "emotion": "joy", "confidence": 0.89},
{"timestamp": "0:45", "emotion": "excitement", "confidence": 0.76},
{"timestamp": "1:15", "emotion": "surprise", "confidence": 0.82}
],
# Scene analysis - should map to frontend scenes array
"scenes": [
{
"scene": "Indoor",
"confidence": 0.85,
"duration": "1:30",
"type": "Primary"
}
],
# Scene changes - should map to frontend sceneChanges array
"scene_changes": [
{"timestamp": "0:30", "confidence": 0.75, "type": "Cut"},
{"timestamp": "1:00", "confidence": 0.80, "type": "Fade"},
{"timestamp": "1:30", "confidence": 0.82, "type": "Dissolve"}
],
# Audio analysis - should map to frontend audioAnalysis object
"audio_analysis": {
"avg_volume": 65,
"peak_volume": 90,
"silent_segments": 2,
"music_detected": True,
"speech_quality": "Good"
},
# Motion analysis
"motion_analysis": {
"motion_type": "moderate",
"motion_intensity": 0.6,
"camera_movement": "minimal"
},
# AI suggestions
"ai_suggestions": [
{
"type": "Enhancement",
"timestamp": "0:36",
"reason": "Great emotional peak detected - consider highlighting this moment",
"confidence": 0.85
},
{
"type": "Audio",
"timestamp": "1:05",
"reason": "Audio quality is excellent in this segment",
"confidence": 0.90
}
],
# Video insights
"insights": [
"Analysis completed successfully",
"Strong emotional engagement detected",
"Good audio quality throughout",
"3 scene transitions identified",
"Recommended for social media content"
]
},
# Top-level recommendations
"recommendations": [
{
"type": "cut",
"suggestion": "Create a highlights reel from 30-90 seconds",
"confidence": 0.92,
"platform": "instagram"
},
{
"type": "filter",
"suggestion": "Apply slight saturation boost for better engagement",
"confidence": 0.78,
"platform": "tiktok"
},
{
"type": "audio",
"suggestion": "Current audio levels are optimal",
"confidence": 0.87,
"platform": "youtube"
}
]
}
elif '/api/recommendations/generate' in self.path or '/api/recommendations' in self.path:
# Return basic recommendations
response = {
"success": True,
"recommendations": [
{
"type": "cut",
"suggestion": "Create a highlights reel from 30-90 seconds",
"confidence": 0.92,
"platform": "instagram"
},
{
"type": "filter",
"suggestion": "Apply slight saturation boost for better engagement",
"confidence": 0.78,
"platform": "tiktok"
},
{
"type": "audio",
"suggestion": "Current audio levels are optimal",
"confidence": 0.87,
"platform": "youtube"
}
]
}
elif '/api/projects' in self.path:
# Return project data
response = {
"success": True,
"projects": [
{
"id": 1,
"name": "Current Video Project",
"filename": "test_video.mp4",
"created_at": "2025-08-22T00:00:00Z",
"status": "active"
}
]
}
else:
response = {"success": False, "error": f"Endpoint not found: {self.path}"}
# Send response
response_json = json.dumps(response)
self.wfile.write(response_json.encode('utf-8'))
logger.info(f"Response sent successfully: {len(response_json)} bytes")
except Exception as e:
logger.error(f"Error handling request: {e}")
self.send_response(500)
self.send_header('Content-Type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps({"success": False, "error": str(e)}).encode('utf-8'))
def run_server():
"""Start the enhanced HTTP server"""
port = 8002
server = HTTPServer(('0.0.0.0', port), SimpleCORSHandler)
logger.info(f"🚀 Enhanced backend running on http://localhost:{port}")
logger.info("✅ CORS enabled for all origins")
logger.info("📡 Endpoints available:")
logger.info(" POST /api/analyze/analyze-filename")
logger.info(" POST /api/recommendations/generate")
logger.info(" POST /api/projects/")
try:
server.serve_forever()
except KeyboardInterrupt:
logger.info("🛑 Server stopped by user")
server.shutdown()
if __name__ == "__main__":
run_server()