Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,25 +6,27 @@ import uuid
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
from utils.logger import logger
|
| 9 |
-
from utils.job_queue import
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# =====================================================
|
| 13 |
-
# INIT
|
| 14 |
# =====================================================
|
| 15 |
|
| 16 |
-
app = FastAPI(title="Fast Whisper Production
|
| 17 |
|
| 18 |
UPLOAD_DIR = "jobs"
|
| 19 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
# Start background worker ONCE
|
| 23 |
start_worker()
|
| 24 |
|
| 25 |
|
| 26 |
# =====================================================
|
| 27 |
-
# API: START
|
| 28 |
# =====================================================
|
| 29 |
|
| 30 |
@app.post("/render")
|
|
@@ -34,24 +36,26 @@ async def render(
|
|
| 34 |
):
|
| 35 |
|
| 36 |
job_id = str(uuid.uuid4())
|
| 37 |
-
|
| 38 |
|
| 39 |
-
with open(
|
| 40 |
-
shutil.copyfileobj(file.file,
|
| 41 |
|
| 42 |
-
logger.info(f"
|
| 43 |
|
| 44 |
-
job_id = create_job(
|
| 45 |
|
| 46 |
return {
|
| 47 |
"job_id": job_id,
|
|
|
|
|
|
|
| 48 |
"status_url": f"/status/{job_id}",
|
| 49 |
"download_url": f"/download/{job_id}"
|
| 50 |
}
|
| 51 |
|
| 52 |
|
| 53 |
# =====================================================
|
| 54 |
-
# API:
|
| 55 |
# =====================================================
|
| 56 |
|
| 57 |
@app.get("/status/{job_id}")
|
|
@@ -67,12 +71,13 @@ def status(job_id: str):
|
|
| 67 |
"status": job["status"],
|
| 68 |
"stage": job.get("stage"),
|
| 69 |
"progress": job.get("progress"),
|
|
|
|
| 70 |
"error": job.get("error"),
|
| 71 |
}
|
| 72 |
|
| 73 |
|
| 74 |
# =====================================================
|
| 75 |
-
# API: DOWNLOAD
|
| 76 |
# =====================================================
|
| 77 |
|
| 78 |
@app.get("/download/{job_id}")
|
|
@@ -84,20 +89,23 @@ def download(job_id: str):
|
|
| 84 |
return {"error": "Job not found"}
|
| 85 |
|
| 86 |
if job["status"] != "completed":
|
| 87 |
-
return {
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
|
|
|
| 92 |
return FileResponse(
|
| 93 |
-
|
| 94 |
media_type="video/mp4",
|
| 95 |
-
filename="
|
| 96 |
)
|
| 97 |
|
| 98 |
|
| 99 |
# =====================================================
|
| 100 |
-
# GRADIO
|
| 101 |
# =====================================================
|
| 102 |
|
| 103 |
def ui_upload(video, webhook):
|
|
@@ -105,14 +113,9 @@ def ui_upload(video, webhook):
|
|
| 105 |
if video is None:
|
| 106 |
return "No video uploaded", None
|
| 107 |
|
| 108 |
-
job_id =
|
| 109 |
-
path = f"{UPLOAD_DIR}/{job_id}.mp4"
|
| 110 |
-
|
| 111 |
-
shutil.copy(video, path)
|
| 112 |
-
|
| 113 |
-
jid = create_job(path, webhook)
|
| 114 |
|
| 115 |
-
return f"Job Started: {
|
| 116 |
|
| 117 |
|
| 118 |
def ui_status(job_id):
|
|
@@ -122,49 +125,34 @@ def ui_status(job_id):
|
|
| 122 |
if not job:
|
| 123 |
return "Job not found", None
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
if status == "
|
| 128 |
-
return "
|
| 129 |
|
| 130 |
-
|
| 131 |
-
return f"❌ Failed: {job.get('error')}", None
|
| 132 |
|
| 133 |
-
return f"Processing... {job.get('progress', 0)}%", None
|
| 134 |
|
|
|
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
# GRADIO UI LAYOUT
|
| 138 |
-
# =====================================================
|
| 139 |
-
|
| 140 |
-
with gr.Blocks(title="Fast Whisper V3 Renderer") as demo:
|
| 141 |
|
| 142 |
-
gr.
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
video = gr.Video(label="Upload Video")
|
| 146 |
-
webhook = gr.Textbox(label="Webhook URL (optional)")
|
| 147 |
-
|
| 148 |
-
start_btn = gr.Button("Start Render")
|
| 149 |
|
| 150 |
job_id_box = gr.Textbox(label="Job ID")
|
| 151 |
-
status_btn = gr.Button("Check Status")
|
| 152 |
|
| 153 |
-
|
| 154 |
-
video_out = gr.Video(label="Output")
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
inputs=[video, webhook],
|
| 159 |
-
outputs=[status_out, job_id_box],
|
| 160 |
-
)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
inputs=job_id_box,
|
| 165 |
-
outputs=[status_out, video_out],
|
| 166 |
-
)
|
| 167 |
|
| 168 |
|
| 169 |
-
# Mount Gradio inside FastAPI root
|
| 170 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
from utils.logger import logger
|
| 9 |
+
from utils.job_queue import (
|
| 10 |
+
start_worker,
|
| 11 |
+
create_job,
|
| 12 |
+
get_job
|
| 13 |
+
)
|
| 14 |
|
| 15 |
# =====================================================
|
| 16 |
+
# INIT
|
| 17 |
# =====================================================
|
| 18 |
|
| 19 |
+
app = FastAPI(title="Fast Whisper Production V4")
|
| 20 |
|
| 21 |
UPLOAD_DIR = "jobs"
|
| 22 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
+
# Start V4 worker
|
|
|
|
| 25 |
start_worker()
|
| 26 |
|
| 27 |
|
| 28 |
# =====================================================
|
| 29 |
+
# API: START BATCH RENDER
|
| 30 |
# =====================================================
|
| 31 |
|
| 32 |
@app.post("/render")
|
|
|
|
| 36 |
):
|
| 37 |
|
| 38 |
job_id = str(uuid.uuid4())
|
| 39 |
+
path = f"{UPLOAD_DIR}/{job_id}_{file.filename}"
|
| 40 |
|
| 41 |
+
with open(path, "wb") as f:
|
| 42 |
+
shutil.copyfileobj(file.file, f)
|
| 43 |
|
| 44 |
+
logger.info(f"[V4] New batch job: {job_id}")
|
| 45 |
|
| 46 |
+
job_id = create_job(path, webhook)
|
| 47 |
|
| 48 |
return {
|
| 49 |
"job_id": job_id,
|
| 50 |
+
"mode": "batch",
|
| 51 |
+
"status": "queued",
|
| 52 |
"status_url": f"/status/{job_id}",
|
| 53 |
"download_url": f"/download/{job_id}"
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
# =====================================================
|
| 58 |
+
# API: STATUS (NOW SUPPORTS CLIPS)
|
| 59 |
# =====================================================
|
| 60 |
|
| 61 |
@app.get("/status/{job_id}")
|
|
|
|
| 71 |
"status": job["status"],
|
| 72 |
"stage": job.get("stage"),
|
| 73 |
"progress": job.get("progress"),
|
| 74 |
+
"clips": job.get("clips", []),
|
| 75 |
"error": job.get("error"),
|
| 76 |
}
|
| 77 |
|
| 78 |
|
| 79 |
# =====================================================
|
| 80 |
+
# API: DOWNLOAD (FIRST CLIP OR FINAL OUTPUT)
|
| 81 |
# =====================================================
|
| 82 |
|
| 83 |
@app.get("/download/{job_id}")
|
|
|
|
| 89 |
return {"error": "Job not found"}
|
| 90 |
|
| 91 |
if job["status"] != "completed":
|
| 92 |
+
return {"error": "Job not completed"}
|
| 93 |
+
|
| 94 |
+
clips = job.get("clips", [])
|
| 95 |
+
|
| 96 |
+
if not clips:
|
| 97 |
+
return {"error": "No clips generated"}
|
| 98 |
|
| 99 |
+
# return first clip for simplicity
|
| 100 |
return FileResponse(
|
| 101 |
+
clips[0],
|
| 102 |
media_type="video/mp4",
|
| 103 |
+
filename="clip_1.mp4"
|
| 104 |
)
|
| 105 |
|
| 106 |
|
| 107 |
# =====================================================
|
| 108 |
+
# GRADIO UI
|
| 109 |
# =====================================================
|
| 110 |
|
| 111 |
def ui_upload(video, webhook):
|
|
|
|
| 113 |
if video is None:
|
| 114 |
return "No video uploaded", None
|
| 115 |
|
| 116 |
+
job_id = create_job(video, webhook)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
return f"Batch Job Started: {job_id}", job_id
|
| 119 |
|
| 120 |
|
| 121 |
def ui_status(job_id):
|
|
|
|
| 125 |
if not job:
|
| 126 |
return "Job not found", None
|
| 127 |
|
| 128 |
+
if job["status"] == "completed":
|
| 129 |
+
clips = job.get("clips", [])
|
| 130 |
+
return f"Completed ({len(clips)} clips)", clips[0] if clips else None
|
| 131 |
|
| 132 |
+
if job["status"] == "failed":
|
| 133 |
+
return f"Failed: {job.get('error')}", None
|
| 134 |
|
| 135 |
+
return f"{job['stage']} ({job.get('progress', 0)}%)", None
|
|
|
|
| 136 |
|
|
|
|
| 137 |
|
| 138 |
+
with gr.Blocks(title="Fast Whisper V4 Batch Engine") as demo:
|
| 139 |
|
| 140 |
+
gr.Markdown("# 🎬 Fast Whisper Production V4 (Batch Engine)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
video = gr.Video(label="Upload Video")
|
| 143 |
+
webhook = gr.Textbox(label="Webhook URL (optional)")
|
| 144 |
|
| 145 |
+
start = gr.Button("Start Batch Render")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
job_id_box = gr.Textbox(label="Job ID")
|
|
|
|
| 148 |
|
| 149 |
+
check = gr.Button("Check Status")
|
|
|
|
| 150 |
|
| 151 |
+
status = gr.Textbox(label="Status")
|
| 152 |
+
output = gr.Video(label="First Clip Preview")
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
start.click(ui_upload, inputs=[video, webhook], outputs=[status, job_id_box])
|
| 155 |
+
check.click(ui_status, inputs=job_id_box, outputs=[status, output])
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
|
|
|
|
| 158 |
app = gr.mount_gradio_app(app, demo, path="/")
|