fomext commited on
Commit
4c86b30
·
verified ·
1 Parent(s): daebecb

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +12 -0
  2. app.py +118 -0
  3. requirements.txt +17 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update && apt-get install -y ffmpeg git && rm -rf /var/lib/apt/lists/*
6
+
7
+ COPY requirements.txt .
8
+ RUN pip install --no-cache-dir -r requirements.txt
9
+
10
+ COPY . .
11
+
12
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, Form
2
+ import os, uuid, subprocess, torch, cv2
3
+ import whisper
4
+ from scenedetect import VideoManager, SceneManager
5
+ from scenedetect.detectors import ContentDetector
6
+ from ultralytics import YOLO
7
+ from diffusers import StableVideoDiffusionPipeline
8
+
9
+ app = FastAPI()
10
+
11
+ UPLOAD_DIR = "uploads"
12
+ OUTPUT_DIR = "outputs"
13
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
14
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
15
+
16
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
+
18
+ # ===== Load models =====
19
+
20
+ whisper_model = whisper.load_model("base")
21
+
22
+ yolo = YOLO("yolov8n.pt")
23
+
24
+ svd = StableVideoDiffusionPipeline.from_pretrained(
25
+ "stabilityai/stable-video-diffusion-img2vid",
26
+ torch_dtype=torch.float16
27
+ ).to(DEVICE)
28
+
29
+ # ===== Endpoints =====
30
+
31
+ @app.post("/captions")
32
+ async def captions(file: UploadFile = File(...)):
33
+ path = os.path.join(UPLOAD_DIR, file.filename)
34
+ with open(path, "wb") as f:
35
+ f.write(await file.read())
36
+
37
+ result = whisper_model.transcribe(path)
38
+
39
+ return {
40
+ "segments": result["segments"],
41
+ "language": result["language"]
42
+ }
43
+
44
+
45
+ @app.post("/scene-detect")
46
+ async def scene_detect(file: UploadFile = File(...)):
47
+ path = os.path.join(UPLOAD_DIR, file.filename)
48
+ with open(path, "wb") as f:
49
+ f.write(await file.read())
50
+
51
+ video_manager = VideoManager([path])
52
+ scene_manager = SceneManager()
53
+ scene_manager.add_detector(ContentDetector(threshold=27.0))
54
+
55
+ video_manager.start()
56
+ scene_manager.detect_scenes(frame_source=video_manager)
57
+
58
+ scenes = scene_manager.get_scene_list()
59
+ video_manager.release()
60
+
61
+ return {
62
+ "scenes": [
63
+ {"start": s[0].get_seconds(), "end": s[1].get_seconds()}
64
+ for s in scenes
65
+ ]
66
+ }
67
+
68
+
69
+ @app.post("/smart-crop")
70
+ async def smart_crop(
71
+ file: UploadFile = File(...),
72
+ aspect: str = Form("9:16")
73
+ ):
74
+ path = os.path.join(UPLOAD_DIR, file.filename)
75
+ with open(path, "wb") as f:
76
+ f.write(await file.read())
77
+
78
+ cap = cv2.VideoCapture(path)
79
+ ret, frame = cap.read()
80
+ cap.release()
81
+
82
+ results = yolo(frame)
83
+ box = results[0].boxes.xyxy[0].cpu().numpy()
84
+
85
+ return {
86
+ "crop_box": box.tolist(),
87
+ "aspect": aspect
88
+ }
89
+
90
+
91
+ @app.post("/edit")
92
+ async def edit_video(
93
+ file: UploadFile = File(...),
94
+ prompt: str = Form(...)
95
+ ):
96
+ path = os.path.join(UPLOAD_DIR, file.filename)
97
+ with open(path, "wb") as f:
98
+ f.write(await file.read())
99
+
100
+ # Extract first frame
101
+ subprocess.run([
102
+ "ffmpeg", "-i", path, "-frames:v", "1",
103
+ "frame.png"
104
+ ])
105
+
106
+ from PIL import Image
107
+ img = Image.open("frame.png").resize((512, 512))
108
+
109
+ frames = svd(
110
+ image=img,
111
+ prompt=prompt,
112
+ num_frames=16
113
+ ).frames
114
+
115
+ return {
116
+ "prompt": prompt,
117
+ "frames_generated": len(frames)
118
+ }
requirements.txt ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch
4
+ torchaudio
5
+ torchvision
6
+ opencv-python
7
+ numpy
8
+ scipy
9
+ ffmpeg-python
10
+
11
+ openai-whisper
12
+ pyscenedetect
13
+ ultralytics
14
+
15
+ diffusers
16
+ transformers
17
+ accelerate