basyx commited on
Commit
8fa8304
·
verified ·
1 Parent(s): 3e3d2e4

Update utils/job_queue.py

Browse files
Files changed (1) hide show
  1. utils/job_queue.py +64 -33
utils/job_queue.py CHANGED
@@ -9,6 +9,8 @@ from .validators import validate_video
9
  from .transcription import transcribe_video
10
  from .highlights import detect_highlights
11
  from .viral_scorer import score_clip
 
 
12
  from .clipper import create_clip
13
 
14
 
@@ -23,7 +25,7 @@ WORKER_STARTED = False
23
 
24
 
25
  # =====================================================
26
- # JOB CREATION (VIRAL MODE)
27
  # =====================================================
28
 
29
  def create_job(video_path: str, webhook: str | None = None):
@@ -35,9 +37,16 @@ def create_job(video_path: str, webhook: str | None = None):
35
  "status": "queued",
36
  "stage": "waiting",
37
  "progress": 0,
 
38
  "video": video_path,
 
 
39
  "clips": [],
40
  "scores": [],
 
 
 
 
41
  "webhook": webhook,
42
  "error": None,
43
  "created_at": time.time(),
@@ -45,13 +54,13 @@ def create_job(video_path: str, webhook: str | None = None):
45
 
46
  queue.put(job_id)
47
 
48
- logger.info(f"[V5] Viral job queued: {job_id}")
49
 
50
  return job_id
51
 
52
 
53
  # =====================================================
54
- # JOB ACCESS
55
  # =====================================================
56
 
57
  def get_job(job_id: str):
@@ -59,7 +68,7 @@ def get_job(job_id: str):
59
 
60
 
61
  # =====================================================
62
- # UPDATE HELPER
63
  # =====================================================
64
 
65
  def update(job_id, **kwargs):
@@ -68,12 +77,12 @@ def update(job_id, **kwargs):
68
 
69
 
70
  # =====================================================
71
- # WORKER (V5 VIRAL ENGINE)
72
  # =====================================================
73
 
74
  def worker():
75
 
76
- logger.info("[V5] Viral engine worker started")
77
 
78
  while True:
79
 
@@ -95,60 +104,82 @@ def worker():
95
  words = transcribe_video(job["video"])
96
 
97
  # -----------------------------
98
- # HIGHLIGHTS DETECTION
99
  # -----------------------------
100
- update(job_id, stage="detecting highlights", progress=40)
101
- highlights = detect_highlights(words)
 
 
 
 
102
 
103
  # -----------------------------
104
- # VIRAL SCORING ENGINE (CORE V5)
105
  # -----------------------------
106
- update(job_id, stage="scoring virality", progress=55)
107
 
108
- scored_clips = []
109
 
110
- for i, segment in enumerate(highlights):
 
111
 
112
- score = score_clip(segment)
113
 
114
- # FILTER LOW VIRALITY
115
- if score < 45:
116
  continue
117
 
118
  clip_path = create_clip(
119
  job["video"],
120
- segment[0]["start"],
121
- segment[-1]["end"],
122
  i
123
  )
124
 
125
- scored_clips.append({
126
- "path": clip_path,
127
- "score": score
128
- })
129
 
130
  update(job_id,
131
- progress=55 + int((i / max(len(highlights),1)) * 35))
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  # -----------------------------
134
- # SORT BY VIRALITY
135
  # -----------------------------
136
- scored_clips.sort(key=lambda x: x["score"], reverse=True)
137
 
138
- top_clips = [c["path"] for c in scored_clips[:5]]
139
- top_scores = [c["score"] for c in scored_clips[:5]]
 
 
 
140
 
141
  # -----------------------------
142
- # COMPLETE
143
  # -----------------------------
144
  update(job_id,
145
  status="completed",
146
- stage="viral selection done",
147
  progress=100,
148
  clips=top_clips,
149
- scores=top_scores)
 
 
 
150
 
151
- logger.info(f"[V5] Viral job complete: {job_id}")
152
 
153
  except Exception as e:
154
 
@@ -164,7 +195,7 @@ def worker():
164
 
165
 
166
  # =====================================================
167
- # START WORKER (SAFE SINGLETON)
168
  # =====================================================
169
 
170
  def start_worker():
@@ -179,4 +210,4 @@ def start_worker():
179
  t = threading.Thread(target=worker, daemon=True)
180
  t.start()
181
 
182
- logger.info("[V5] Worker initialized")
 
9
  from .transcription import transcribe_video
10
  from .highlights import detect_highlights
11
  from .viral_scorer import score_clip
12
+ from .retention import predict_retention
13
+ from .caption_seo import generate_caption
14
  from .clipper import create_clip
15
 
16
 
 
25
 
26
 
27
  # =====================================================
28
+ # CREATE STRATEGY JOB
29
  # =====================================================
30
 
31
  def create_job(video_path: str, webhook: str | None = None):
 
37
  "status": "queued",
38
  "stage": "waiting",
39
  "progress": 0,
40
+
41
  "video": video_path,
42
+
43
+ # V6 INTELLIGENCE OUTPUTS
44
  "clips": [],
45
  "scores": [],
46
+ "retention_scores": [],
47
+ "caption": None,
48
+ "strategy_summary": None,
49
+
50
  "webhook": webhook,
51
  "error": None,
52
  "created_at": time.time(),
 
54
 
55
  queue.put(job_id)
56
 
57
+ logger.info(f"[V6] Strategy job queued: {job_id}")
58
 
59
  return job_id
60
 
61
 
62
  # =====================================================
63
+ # GET JOB
64
  # =====================================================
65
 
66
  def get_job(job_id: str):
 
68
 
69
 
70
  # =====================================================
71
+ # UPDATE HELPERS
72
  # =====================================================
73
 
74
  def update(job_id, **kwargs):
 
77
 
78
 
79
  # =====================================================
80
+ # WORKER (V6 STRATEGIC ENGINE)
81
  # =====================================================
82
 
83
  def worker():
84
 
85
+ logger.info("[V6] Viral AI Strategist worker started")
86
 
87
  while True:
88
 
 
104
  words = transcribe_video(job["video"])
105
 
106
  # -----------------------------
107
+ # HIGHLIGHT DETECTION
108
  # -----------------------------
109
+ update(job_id, stage="detecting moments", progress=35)
110
+ segments = detect_highlights(words)
111
+
112
+ clips = []
113
+ scores = []
114
+ retention_scores = []
115
 
116
  # -----------------------------
117
+ # STRATEGIC SCORING ENGINE
118
  # -----------------------------
119
+ update(job_id, stage="strategic analysis", progress=50)
120
 
121
+ for i, seg in enumerate(segments):
122
 
123
+ virality = score_clip(seg)
124
+ retention = predict_retention(seg)
125
 
126
+ final_score = (virality * 0.6) + (retention * 0.4)
127
 
128
+ if final_score < 50:
 
129
  continue
130
 
131
  clip_path = create_clip(
132
  job["video"],
133
+ seg[0]["start"],
134
+ seg[-1]["end"],
135
  i
136
  )
137
 
138
+ clips.append(clip_path)
139
+ scores.append(final_score)
140
+ retention_scores.append(retention)
 
141
 
142
  update(job_id,
143
+ progress=50 + int((i / max(len(segments),1)) * 40))
144
+
145
+ # -----------------------------
146
+ # RANKING (STRATEGY CORE)
147
+ # -----------------------------
148
+ ranked = sorted(
149
+ zip(clips, scores, retention_scores),
150
+ key=lambda x: x[1],
151
+ reverse=True
152
+ )
153
+
154
+ top_clips = [c[0] for c in ranked[:5]]
155
+ top_scores = [c[1] for c in ranked[:5]]
156
+ top_retention = [c[2] for c in ranked[:5]]
157
 
158
  # -----------------------------
159
+ # CAPTION STRATEGY
160
  # -----------------------------
161
+ caption = generate_caption(words)
162
 
163
+ strategy_summary = {
164
+ "total_segments": len(segments),
165
+ "kept_clips": len(top_clips),
166
+ "avg_score": sum(top_scores) / max(len(top_scores), 1)
167
+ }
168
 
169
  # -----------------------------
170
+ # FINALIZE
171
  # -----------------------------
172
  update(job_id,
173
  status="completed",
174
+ stage="strategy finalized",
175
  progress=100,
176
  clips=top_clips,
177
+ scores=top_scores,
178
+ retention_scores=top_retention,
179
+ caption=caption,
180
+ strategy_summary=strategy_summary)
181
 
182
+ logger.info(f"[V6] Strategy complete: {job_id}")
183
 
184
  except Exception as e:
185
 
 
195
 
196
 
197
  # =====================================================
198
+ # START WORKER
199
  # =====================================================
200
 
201
  def start_worker():
 
210
  t = threading.Thread(target=worker, daemon=True)
211
  t.start()
212
 
213
+ logger.info("[V6] Worker initialized")