rajux75 commited on
Commit
5ad0b62
·
verified ·
1 Parent(s): 3b1acf8

Create routers/videos.py

Browse files
Files changed (1) hide show
  1. routers/videos.py +52 -0
routers/videos.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # routers/videos.py
2
+ from fastapi import APIRouter, Depends, HTTPException, status
3
+ from starlette.concurrency import run_in_threadpool
4
+ import models
5
+ import config
6
+ from services import generation, security
7
+ import logging
8
+
9
+ logger = logging.getLogger(__name__)
10
+ router = APIRouter(
11
+ prefix="/videos",
12
+ tags=["Video Generation"],
13
+ dependencies=[Depends(security.get_api_key)]
14
+ )
15
+
16
+ @router.post("/image-to-video", response_model=models.VideoResponse, status_code=status.HTTP_201_CREATED)
17
+ async def create_video_from_image(request: models.VideoRequest):
18
+ """
19
+ Generates a short video clip animating a source image.
20
+ """
21
+ if not generation.model_cache.get("video_pipeline"):
22
+ raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Video generation model is not available.")
23
+
24
+ try:
25
+ logger.info(f"Received image-to-video request.")
26
+ # Video generation can be VERY slow, especially on CPU/basic GPU
27
+ video_base64, actual_format = await run_in_threadpool(
28
+ generation.generate_video_sync,
29
+ image_base64=request.image_base64,
30
+ prompt=request.prompt,
31
+ motion_bucket_id=request.motion_bucket_id,
32
+ noise_aug_strength=request.noise_aug_strength,
33
+ num_frames=request.num_frames,
34
+ fps=request.fps,
35
+ num_inference_steps=request.num_inference_steps,
36
+ guidance_scale=request.guidance_scale
37
+ )
38
+ logger.info(f"Successfully generated video.")
39
+ return models.VideoResponse(
40
+ video_base64=video_base64,
41
+ model_name=config.VIDEO_MODEL_NAME,
42
+ format=actual_format
43
+ )
44
+ except ValueError as e: # Catch specific errors like invalid base64 or frame issues
45
+ logger.error(f"Value error during video generation: {e}", exc_info=True)
46
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
47
+ except RuntimeError as e:
48
+ logger.error(f"Model runtime error during video generation: {e}", exc_info=True)
49
+ raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=f"Model inference failed: {e}")
50
+ except Exception as e:
51
+ logger.error(f"Unexpected error during video generation: {e}", exc_info=True)
52
+ raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"An unexpected error occurred: {e}")