File size: 9,693 Bytes
eae351f
 
 
 
 
 
5c71691
eae351f
1d5e204
1a7294b
 
eae351f
5c71691
eae351f
 
 
 
 
 
 
 
 
 
 
5c71691
 
 
 
eae351f
 
5c71691
 
eae351f
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
1d5e204
eae351f
 
1d5e204
eae351f
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
 
1d5e204
 
eae351f
 
 
 
 
 
 
 
 
1d5e204
eae351f
1d5e204
eae351f
 
 
 
 
 
1d5e204
eae351f
 
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
 
1d5e204
eae351f
 
1d5e204
eae351f
 
1d5e204
eae351f
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d5e204
 
 
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7294b
1d5e204
1a7294b
eae351f
 
 
 
 
 
 
1a7294b
1d5e204
eae351f
1a7294b
1d5e204
1a7294b
 
1d5e204
1a7294b
 
 
 
1d5e204
 
 
 
1a7294b
 
 
 
 
1d5e204
1a7294b
1d5e204
eae351f
 
1a7294b
 
eae351f
 
 
 
1d5e204
eae351f
 
 
 
cb69a5a
 
eae351f
 
 
 
 
 
 
 
cb69a5a
 
 
271b49b
eae351f
1d5e204
eae351f
 
 
 
 
 
 
 
 
 
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
import os
import json
import time
import cv2
import requests
from dotenv import load_dotenv
from upstash_redis import Redis

# ⭐ 引入智能模型路由器(你已上传 model_router.py)
from model_router import ModelRouter

# ============================================================
# 1. 加载环境变量并连接 Upstash Redis(REST API)
# ============================================================
load_dotenv()

redis_url = os.getenv("UPSTASH_REDIS_REST_URL")
redis_token = os.getenv("UPSTASH_REDIS_REST_TOKEN")

print("Worker 正在使用的 Redis URL:", redis_url)

if not redis_url or not redis_token:
    raise ValueError("❌ 未找到 UPSTASH_REDIS_REST_URL 或 UPSTASH_REDIS_REST_TOKEN,请检查 HuggingFace Secrets")

# ⭐ 使用 Upstash 官方 REST 客户端
redis_client = Redis(
    url=redis_url,
    token=redis_token,
)

print("🔌 Upstash Redis REST API connected in Worker")


# ============================================================
# 2. 从 Redis 队列中获取任务
# ============================================================
def get_next_task():
    task_id = redis_client.lpop("tasks:queue")

    if isinstance(task_id, bytes):
        task_id = task_id.decode("utf-8")

    if not task_id:
        return None

    task_key = f"task:{task_id}"
    raw = redis_client.get(task_key)

    if not raw:
        print(f"⚠️ 任务 {task_id} 不存在,跳过")
        return None

    if isinstance(raw, bytes):
        raw = raw.decode("utf-8")

    try:
        task = json.loads(raw)
    except:
        print(f"⚠️ 任务 {task_id} JSON 解析失败")
        return None

    task["status"] = "processing"
    task["updatedAt"] = int(time.time() * 1000)
    task["progress"] = task.get("progress", 50)

    redis_client.set(task_key, json.dumps(task, ensure_ascii=False))

    print(f"🟡 任务进入处理状态: {task_id}")
    return task


# ============================================================
# 3. 下载视频
# ============================================================
def download_video(url, task_id):
    print(f"⬇️ 下载视频: {url}")

    if isinstance(url, bytes):
        url = url.decode("utf-8")

    if url.startswith("http://") or url.startswith("https://"):
        os.makedirs("./tmp", exist_ok=True)
        local_path = f"./tmp/{task_id}.mp4"

        r = requests.get(url)
        if r.status_code != 200:
            raise ValueError(f"无法下载视频,HTTP 状态码: {r.status_code}")

        with open(local_path, "wb") as f:
            f.write(r.content)

        print(f"📁 视频已保存到: {local_path}")
        return local_path

    else:
        if not os.path.exists(url):
            raise FileNotFoundError(f"本地视频不存在: {url}")
        return url


# ============================================================
# 4. 写回任务状态
# ============================================================
def finish_task(task_id, status, result=None, error=None):
    task_key = f"task:{task_id}"

    raw = redis_client.get(task_key)
    if raw:
        task = json.loads(raw.decode("utf-8"))
    else:
        task = {"id": task_id}

    task["status"] = status
    task["updatedAt"] = int(time.time() * 1000)

    if status == "done":
        task["progress"] = 100
    else:
        task["progress"] = task.get("progress", 0)

    if result is not None:
        task["result"] = result
    if error is not None:
        task["error"] = error

    redis_client.set(task_key, json.dumps(task, ensure_ascii=False))

    if status == "done":
        print(f"✅ 任务完成: {task_id}")
    else:
        print(f"❌ 任务失败: {task_id}, 错误: {error}")


# ============================================================
# 5. 任务处理主入口
# ============================================================
def process_video_task(task_data):
    print(f"🎯 收到任务: {task_data}")

    task_type = task_data.get("type")
    task_id = task_data.get("id")

    if not task_id:
        print("⚠️ 任务缺少 id,跳过")
        return

    try:
        video_url = task_data.get("videoUrl")

        if task_type == "video_analysis":
            local_path = download_video(video_url, task_id)
            task_data["local_video_path"] = local_path

        if task_type == "video_generation":
            result = generate_video_with_sora(task_data)

        elif task_type == "video_analysis":
            result = analyze_video_style(task_data)

        elif task_type == "digital_human":
            result = generate_digital_human_video(task_data)

        elif task_type == "video_processing":
            result = process_video_file(task_data)

        elif task_type == "generate_script":
            result = generate_ecommerce_script(task_data)

        else:
            raise ValueError(f"未知任务类型: {task_type}")

        finish_task(task_id, "done", result=result)

    except Exception as e:
        finish_task(task_id, "failed", error=str(e))


# ============================================================
# 6. 视频风格分析
# ============================================================
def analyze_video_style(task_data):
    video_path = task_data.get("local_video_path")

    if not video_path or not os.path.exists(video_path):
        raise FileNotFoundError(f"视频不存在: {video_path}")

    print(f"🎨 分析视频风格: {video_path}")

    cap = cv2.VideoCapture(video_path)
    frames = []

    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = cap.get(cv2.CAP_PROP_FPS) or 25
    step = max(int(fps), 1)

    for i in range(0, frame_count, step):
        cap.set(cv2.CAP_PROP_POS_FRAMES, i)
        ret, frame = cap.read()
        if ret:
            frames.append(frame)

    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    cap.release()

    style_tags = []

    if frames:
        avg_brightness = sum(cv2.mean(f)[0] for f in frames) / len(frames)
        if avg_brightness > 150:
            style_tags.append("明亮")
        elif avg_brightness < 100:
            style_tags.append("暗色调")

        motion_score = 0
        for i in range(1, len(frames)):
            diff = cv2.absdiff(frames[i - 1], frames[i])
            motion_score += cv2.mean(diff)[0]

        if motion_score / len(frames) > 50:
            style_tags.append("动态")
        else:
            style_tags.append("静态")

    return {
        "style_tags": style_tags,
        "frame_count": len(frames),
        "fps": fps,
        "duration": frame_count / fps if fps > 0 else 0,
        "resolution": f"{width}x{height}"
    }


# ============================================================
# 7. 其他任务(保持原样)
# ============================================================
def generate_video_with_sora(task_data):
    time.sleep(5)
    task_id = task_data.get("id")
    return {
        "video_url": f"/generated/{task_id}.mp4",
        "thumbnail_url": f"/thumbnails/{task_id}.jpg",
        "duration": 5,
        "resolution": "1920x1080",
        "format": "mp4"
    }


def generate_digital_human_video(task_data):
    time.sleep(10)
    task_id = task_data.get("id")
    return {
        "video_url": f"/digital_human/{task_id}.mp4",
        "audio_url": f"/audio/{task_id}.wav",
        "lip_sync_score": 0.95,
        "processing_time": 10
    }


def process_video_file(task_data):
    return {"message": "视频处理功能未启用"}


# ============================================================
# 7+. 电商脚本生成(接入 ModelRouter)
# ============================================================
def generate_ecommerce_script(task_data):
    payload = task_data.get("payload") or {}

    product_description = payload.get("productDescription") or ""
    product_url = payload.get("productUrl") or ""
    style = payload.get("style") or "通用风格"

    prompt = f"""
你是一名专业的中文短视频电商带货脚本编剧,请根据以下信息生成一份完整的口播脚本。

【产品信息】
{product_description}

【产品链接】
{product_url}

【带货风格】
{style}

【要求】
- 口语化、真实、有代入感
- 开头抓人,中间讲卖点,结尾强行动引导
- 300~600 字
"""

    router = ModelRouter()
    script_text = router.generate(prompt).strip()

    # 简单提取 highlights
    lines = [line.strip() for line in script_text.split("\n") if line.strip()]
    highlights = "\n".join(lines[:5]) if lines else "核心卖点:请根据脚本内容提炼。"

    return {
        "script": script_text,
        "highlights": highlights,
    }


# ============================================================
# 8. Worker 主循环(含心跳)
# ============================================================
def run_worker():
    print("🚀 AI Worker 已启动,监听 Redis 队列 tasks:queue ...")

    last_heartbeat = time.time()

    while True:
        try:
            task = get_next_task()
            if task:
                process_video_task(task)
            else:
                time.sleep(1)

            if time.time() - last_heartbeat > 30:
                print("💓 Worker heartbeat - still alive")
                last_heartbeat = time.time()

        except Exception as e:
            print(f"⚠️ Worker 错误: {e}")
            import traceback
            traceback.print_exc()
            time.sleep(2)


# ============================================================
# 9. 启动 Worker
# ============================================================
if __name__ == "__main__":
    run_worker()