-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduction_fast_system.py
More file actions
427 lines (345 loc) ยท 16 KB
/
production_fast_system.py
File metadata and controls
427 lines (345 loc) ยท 16 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python3
"""
Quickscene Production Fast System
Real implementation with optimized processing for sub-700ms queries.
This processes one video quickly to demonstrate the production system.
"""
import time
import json
import numpy as np
from pathlib import Path
import sys
import os
# Add app to path
sys.path.insert(0, str(Path(__file__).parent))
def seconds_to_video_timestamp(seconds: float, include_milliseconds: bool = False) -> str:
"""
Convert seconds to standard video timestamp format.
Args:
seconds: Time in seconds (e.g., 575.1)
include_milliseconds: Whether to include milliseconds
Returns:
Formatted timestamp:
- Under 1 hour: MM:SS (e.g., "9:35")
- Over 1 hour: HH:MM:SS (e.g., "1:05:22")
- With milliseconds: HH:MM:SS.mmm (e.g., "1:05:22.500")
"""
total_seconds = int(seconds)
milliseconds = int((seconds - total_seconds) * 1000)
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
secs = total_seconds % 60
if hours > 0:
# Format: HH:MM:SS or HH:MM:SS.mmm
if include_milliseconds and milliseconds > 0:
return f"{hours}:{minutes:02d}:{secs:02d}.{milliseconds:03d}"
else:
return f"{hours}:{minutes:02d}:{secs:02d}"
else:
# Format: MM:SS or MM:SS.mmm
if include_milliseconds and milliseconds > 0:
return f"{minutes}:{secs:02d}.{milliseconds:03d}"
else:
return f"{minutes}:{secs:02d}"
def process_single_video_fast():
"""Process one video quickly for demonstration."""
print("๐ Quickscene Production Fast System")
print("=" * 50)
# Check for videos
video_dir = Path("data/videos")
video_files = list(video_dir.glob("*.mp4"))
if not video_files:
print("โ No videos found in data/videos/")
return
# Pick the shortest video for fast processing
target_video = video_files[0] # Use first video
print(f"๐น Processing: {target_video.name}")
# Step 1: Fast transcription (using tiny model for speed)
print("\n๐ค Step 1: Fast Transcription...")
start_time = time.time()
try:
import whisper
# Use tiny model for speed
print(" Loading Whisper tiny model...")
model = whisper.load_model("tiny")
print(f" Transcribing {target_video.name}...")
result = model.transcribe(str(target_video), language="en")
# Create transcript structure
transcript = {
'video_id': target_video.stem,
'duration': result.get('duration', 0),
'language': result.get('language', 'en'),
'segments': []
}
for segment in result.get('segments', []):
transcript['segments'].append({
'start': float(segment['start']),
'end': float(segment['end']),
'text': segment['text'].strip()
})
transcription_time = time.time() - start_time
print(f" โ
Transcription complete: {transcription_time:.1f}s")
print(f" ๐ Generated {len(transcript['segments'])} segments")
except Exception as e:
print(f" โ Transcription failed: {e}")
return
# Step 2: Fast chunking
print("\nโ๏ธ Step 2: Fast Chunking...")
start_time = time.time()
chunks = []
chunk_duration = 15 # 15 second chunks
for i, segment in enumerate(transcript['segments']):
chunk = {
'video_id': transcript['video_id'],
'chunk_id': f"{transcript['video_id']}_{i:04d}",
'start_time': segment['start'],
'end_time': segment['end'],
'text': segment['text'],
'duration': segment['end'] - segment['start']
}
chunks.append(chunk)
chunking_time = time.time() - start_time
print(f" โ
Chunking complete: {chunking_time:.3f}s")
print(f" ๐ Generated {len(chunks)} chunks")
# Step 3: Fast embedding
print("\n๐งฎ Step 3: Fast Embedding...")
start_time = time.time()
try:
from sentence_transformers import SentenceTransformer
# Use fast model
print(" Loading SentenceTransformer model...")
embedder = SentenceTransformer('all-MiniLM-L6-v2')
# Extract texts
texts = [chunk['text'] for chunk in chunks]
print(f" Generating embeddings for {len(texts)} chunks...")
embeddings = embedder.encode(texts, convert_to_numpy=True, normalize_embeddings=True)
embeddings = embeddings.astype(np.float32)
embedding_time = time.time() - start_time
print(f" โ
Embedding complete: {embedding_time:.1f}s")
print(f" ๐ Generated embeddings: {embeddings.shape}")
except Exception as e:
print(f" โ Embedding failed: {e}")
return
# Step 4: Fast indexing
print("\n๐ Step 4: Fast Indexing...")
start_time = time.time()
try:
import faiss
# Create simple flat index for speed
dimension = embeddings.shape[1]
index = faiss.IndexFlatIP(dimension) # Inner product for cosine similarity
index.add(embeddings)
indexing_time = time.time() - start_time
print(f" โ
Indexing complete: {indexing_time:.3f}s")
print(f" ๐ Index size: {index.ntotal} vectors")
except Exception as e:
print(f" โ Indexing failed: {e}")
return
# Step 5: Query system ready
print("\n๐ฏ Step 5: Query System Ready!")
def fast_query(query_text, top_k=3):
"""Ultra-fast query processing."""
query_start = time.time()
# Check if this is a keyword search (single word)
is_keyword_search = len(query_text.strip().split()) == 1
keyword_lower = query_text.lower().strip()
results = []
if is_keyword_search:
# For keyword searches, scan all chunks for matches (exact and variations)
keyword_matches = []
# Create search variations for better matching
search_terms = [keyword_lower]
# Add common variations
if keyword_lower == "superintelligence":
search_terms.extend(["super-intelligence", "super intelligence"])
elif keyword_lower == "super-intelligence":
search_terms.extend(["superintelligence", "super intelligence"])
elif keyword_lower == "ai":
search_terms.extend(["artificial intelligence", "a.i.", "a i"])
elif keyword_lower == "machine":
search_terms.extend(["machines", "machinery"])
for i, chunk in enumerate(chunks):
text_lower = chunk['text'].lower()
# Check for any search term match
best_match = None
best_score = 0
for term in search_terms:
if term in text_lower:
# Calculate relevance score based on keyword frequency and position
keyword_count = text_lower.count(term)
keyword_position = text_lower.find(term)
# Higher score for more occurrences and earlier position
score = keyword_count * 0.5 + (1.0 - keyword_position / len(text_lower)) * 0.5
# Bonus for exact match
if term == keyword_lower:
score += 0.5
if score > best_score:
best_score = score
best_match = term
if best_match:
# Calculate exact timestamp (middle of the chunk for best relevance)
exact_timestamp_seconds = (chunk['start_time'] + chunk['end_time']) / 2
# Convert to proper video timestamp format
video_timestamp = seconds_to_video_timestamp(exact_timestamp_seconds)
start_timestamp = seconds_to_video_timestamp(chunk['start_time'])
end_timestamp = seconds_to_video_timestamp(chunk['end_time'])
keyword_matches.append({
'chunk_index': i,
'chunk': chunk,
'relevance_score': best_score,
'matched_term': best_match,
'timestamp': video_timestamp,
'timestamp_seconds': exact_timestamp_seconds,
'start_time': start_timestamp,
'end_time': end_timestamp
})
# Sort by relevance score (highest first)
keyword_matches.sort(key=lambda x: x['relevance_score'], reverse=True)
# Format results
for i, match in enumerate(keyword_matches[:top_k]):
result = {
'rank': i + 1,
'video_id': match['chunk']['video_id'],
'timestamp': match['timestamp'],
'timestamp_seconds': match['timestamp_seconds'],
'start_time': match['start_time'],
'end_time': match['end_time'],
'start_time_seconds': match['chunk']['start_time'],
'end_time_seconds': match['chunk']['end_time'],
'confidence': match['relevance_score'],
'dialogue': match['chunk']['text'],
'is_keyword_match': True
}
results.append(result)
else:
# For semantic searches, use embedding similarity
query_embedding = embedder.encode([query_text], convert_to_numpy=True, normalize_embeddings=True)
query_embedding = query_embedding.astype(np.float32)
# Search index
scores, indices = index.search(query_embedding, top_k)
# Format results
for i, (score, idx) in enumerate(zip(scores[0], indices[0])):
if idx >= 0: # Valid index
chunk = chunks[idx]
# Calculate exact timestamp (middle of the chunk for best relevance)
exact_timestamp_seconds = (chunk['start_time'] + chunk['end_time']) / 2
# Convert to proper video timestamp format
video_timestamp = seconds_to_video_timestamp(exact_timestamp_seconds)
start_timestamp = seconds_to_video_timestamp(chunk['start_time'])
end_timestamp = seconds_to_video_timestamp(chunk['end_time'])
result = {
'rank': i + 1,
'video_id': chunk['video_id'],
'timestamp': video_timestamp,
'timestamp_seconds': exact_timestamp_seconds,
'start_time': start_timestamp,
'end_time': end_timestamp,
'start_time_seconds': chunk['start_time'],
'end_time_seconds': chunk['end_time'],
'confidence': float(score),
'dialogue': chunk['text'],
'is_keyword_match': False
}
results.append(result)
query_time = (time.time() - query_start) * 1000 # Convert to ms
return results, query_time, is_keyword_search
# Demo queries
demo_queries = [
"artificial intelligence",
"machine learning",
"technology",
"computer science",
"programming"
]
print("\n" + "=" * 50)
print("๐ฅ FAST QUERY DEMONSTRATION")
print("=" * 50)
total_time = 0
successful_queries = 0
for i, query in enumerate(demo_queries, 1):
try:
print(f"\n๐ Query {i}: '{query}'")
results, query_time_ms, is_keyword = fast_query(query, top_k=5 if len(query.split()) == 1 else 3)
total_time += query_time_ms
successful_queries += 1
print(f"โก Response time: {query_time_ms:.1f}ms")
if query_time_ms < 700:
print("โ
MEETS <700ms REQUIREMENT!")
else:
print("โ Exceeds 700ms requirement")
if results:
if is_keyword and len(results) > 1:
print(f"\n๐ Keyword '{query}' found in {len(results)} locations:")
for result in results:
print(f" {result['rank']}. ๐ฌ {result['video_id']} | โฐ {result['timestamp']} | ๐ฌ \"{result['dialogue'][:60]}...\"")
else:
print(f"\n๐ Top Result:")
result = results[0]
print(f" ๐ฌ Video: {result['video_id']}")
print(f" โฐ Timestamp: {result['timestamp']}")
print(f" ๐ Context: {result['start_time']} - {result['end_time']}")
print(f" ๐ฏ Confidence: {result['confidence']:.3f}")
print(f" ๐ฌ Dialogue: \"{result['dialogue'][:100]}...\"")
print(f" ๐ Raw: {result['timestamp_seconds']:.1f}s")
else:
print(" โ No results found")
except Exception as e:
print(f" โ Query failed: {e}")
if successful_queries > 0:
avg_time = total_time / successful_queries
print("\n" + "=" * 50)
print("๐ PERFORMANCE SUMMARY")
print("=" * 50)
print(f"๐ฏ Target: <700ms per query")
print(f"โก Average response time: {avg_time:.1f}ms")
print(f"๐ Successful queries: {successful_queries}/{len(demo_queries)}")
if avg_time < 700:
print("\n๐ SYSTEM MEETS PERFORMANCE REQUIREMENTS!")
else:
print("\nโ ๏ธ System needs optimization")
# Interactive mode
print("\n" + "=" * 50)
print("๐ฎ INTERACTIVE MODE")
print("=" * 50)
print("Enter your queries (or 'quit' to exit):")
while True:
try:
user_query = input("\n๐ Your query: ").strip()
if user_query.lower() in ['quit', 'exit', 'q']:
break
if not user_query:
continue
# Determine if keyword search and adjust top_k
is_single_word = len(user_query.strip().split()) == 1
search_top_k = 5 if is_single_word else 3
results, query_time_ms, is_keyword = fast_query(user_query, top_k=search_top_k)
print(f"โก Response time: {query_time_ms:.1f}ms")
if query_time_ms < 700:
print("โ
MEETS <700ms REQUIREMENT!")
else:
print("โ Exceeds 700ms requirement")
if results:
if is_keyword and len(results) > 1:
print(f"\n๐ Keyword '{user_query}' Results:")
for result in results:
print(f" {result['rank']}. ๐ฌ {result['video_id']} | โฐ {result['timestamp']} | ๐ฌ \"{result['dialogue']}\"")
print(f" ๐ฏ Confidence: {result['confidence']:.3f}")
print()
else:
print("\n๐ Results:")
for result in results:
print(f" {result['rank']}. ๐ฌ {result['video_id']}")
print(f" โฐ Timestamp: {result['timestamp']}")
print(f" ๐ฌ Dialogue: \"{result['dialogue']}\"")
print(f" ๐ฏ Confidence: {result['confidence']:.3f}")
print(f" ๐ Context: {result['start_time']} - {result['end_time']}")
print()
else:
print("โ No results found")
except KeyboardInterrupt:
break
except Exception as e:
print(f"โ Error: {e}")
print("\n๐ Thanks for trying Quickscene Production System!")
if __name__ == "__main__":
process_single_video_fast()