hf-worker / processor_worker.py
Davidliang12356's picture
Update processor_worker.py
1d5e204 verified
Raw History Blame Contribute Delete
9.69 kB
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()