Create modules/__init__.py

#5
Files changed (7) hide show
  1. .gitattributes +35 -0
  2. Dockerfile +4 -28
  3. README.md +5 -7
  4. __pycache__/app.cpython-310.pyc +0 -0
  5. app.py +128 -101
  6. modules/__init__.py +0 -0
  7. requirements.txt +8 -18
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile CHANGED
@@ -1,30 +1,6 @@
1
  FROM python:3.10-slim
2
-
 
3
  WORKDIR /app
4
-
5
- # cv2 / insightface 需要的系統庫(slim 鏡像缺這些會 import cv2 炸掉)
6
- RUN apt-get update && apt-get install -y --no-install-recommends \
7
- libgl1 libglib2.0-0 libgomp1 unzip curl git g++ \
8
- && rm -rf /var/lib/apt/lists/*
9
-
10
- # 構建期下載 insightface antelopev2 人臉模型包(運行期不再聯網,避免 FaceAnalysis assertion)
11
- RUN mkdir -p /app/models && \
12
- curl -sL -o /tmp/antelopev2.zip \
13
- https://github.com/deepinsight/insightface/releases/download/v0.7/antelopev2.zip && \
14
- unzip -q /tmp/antelopev2.zip -d /app/models/ && \
15
- rm /tmp/antelopev2.zip && \
16
- ls /app/models/antelopev2
17
-
18
- # CPU 版 torch:用 extra-index-url 同時保留 PyPI,否則依賴包解析會炸
19
- RUN pip install --no-cache-dir torch==2.4.1 --extra-index-url https://download.pytorch.org/whl/cpu
20
-
21
- COPY requirements.txt .
22
- RUN pip install --no-cache-dir -r requirements.txt
23
-
24
- COPY . .
25
-
26
- EXPOSE 7860
27
- ENV GRADIO_SERVER_NAME=0.0.0.0
28
- ENV GRADIO_SERVER_PORT=7860
29
-
30
- CMD ["python", "app.py"]
 
1
  FROM python:3.10-slim
2
+ RUN apt-get update && apt-get install -y ffmpeg fonts-noto-cjk
3
+ COPY . /app
4
  WORKDIR /app
5
+ RUN pip install -r requirements.txt
6
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -1,12 +1,10 @@
1
  ---
2
- title: CharGen CPU
3
- emoji: 🎭
4
- colorFrom: purple
5
- colorTo: pink
6
  sdk: docker
7
- app_port: 7860
8
  pinned: false
9
- license: apache-2.0
10
  ---
11
 
12
- 固定角色生图(CPU 版):SD 1.5 + LCM-LoRA(4 步)+ IP-Adapter FaceID-PlusV2 锁脸。上传一张人脸参考图,所有图同一角色。跑在免费 cpu-basic(2 vCPU / 16GB RAM),512×512 约 1–5 分钟/张。
 
1
  ---
2
+ title: Video Gen
3
+ emoji: 📊
4
+ colorFrom: green
5
+ colorTo: red
6
  sdk: docker
 
7
  pinned: false
 
8
  ---
9
 
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
__pycache__/app.cpython-310.pyc DELETED
Binary file (3.75 kB)
 
app.py CHANGED
@@ -1,114 +1,141 @@
1
- import random
 
 
 
2
  import traceback
 
 
 
3
 
4
- import cv2
5
- import numpy as np
6
- import torch
7
- import gradio as gr
8
- from insightface.app import FaceAnalysis
9
- from diffusers import StableDiffusionPipeline
10
- from ip_adapter.ip_adapter_faceid import IPAdapterFaceIDPlus
11
- from huggingface_hub import hf_hub_download
12
 
13
- torch.set_num_threads(2)
14
- device = "cpu"
15
 
16
- # ---- SD 1.5 fp32(CPU 友好)----
17
- pipe = StableDiffusionPipeline.from_pretrained(
18
- "runwayml/stable-diffusion-v1-5",
19
- torch_dtype=torch.float32,
20
- safety_checker=None,
21
- requires_safety_checker=False,
22
- )
23
- pipe.to(device)
24
- pipe.enable_attention_slicing()
 
 
25
 
26
- # ---- FaceID-PlusV2:比普通版多一个 CLIP 图像编码器,锁脸更准、色彩更稳 ----
27
- ip_ckpt = hf_hub_download(
28
- repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid-plusv2_sd15.bin"
29
- )
30
- image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
31
- # 官方库默认 torch_dtype=float16;CPU 上 pipe 是 fp32,必须显式传 float32,
32
- # 否则 image_proj_model / attn processors 是 Half,和 fp32 的 embeds 冲突会炸 dtype 错误
33
- ip_model = IPAdapterFaceIDPlus(
34
- pipe, image_encoder_path, ip_ckpt, device, torch_dtype=torch.float32
35
- )
36
 
37
- # insightface 约定模型路径是 <root>/models/<name>,Dockerfile 解压到 /app/models,所以 root 必须是 /app
38
- face_app = FaceAnalysis(name="antelopev2", root="/app", providers=["CPUExecutionProvider"])
39
- face_app.prepare(ctx_id=0, det_size=(640, 640))
40
-
41
- MAX_SEED = 2**32 - 1
42
-
43
-
44
- def generate(face_image, prompt, negative_prompt, seed, steps, guidance, ip_scale):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  try:
46
- if face_image is None:
47
- raise gr.Error("请先上传一张人脸参考图(正脸效果最好)")
48
- rgb = np.array(face_image.convert("RGB"))
49
- faces = face_app.get(cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR))
50
- if len(faces) == 0:
51
- raise gr.Error("参考图里检测不到人脸,换一张清晰正脸照试试")
52
- faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0).float()
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- seed = int(seed)
55
- if seed < 0:
56
- seed = random.randint(0, MAX_SEED)
 
 
57
 
58
- # PlusV2 需要同时传 face_image(给 CLIP 编码)和 faceid_embeds(给 insightface 分支)
59
- # scale 控制锁脸强度(0.4–0.6 较稳,过高会把参考图颜色糊到脸上导致花脸)
60
- image = ip_model.generate(
61
- face_image=face_image,
62
- faceid_embeds=faceid_embed,
63
- prompt=prompt,
64
- negative_prompt=negative_prompt,
65
- num_samples=1,
66
- seed=seed,
67
- num_inference_steps=int(steps),
68
- guidance_scale=float(guidance),
69
- scale=float(ip_scale),
70
- s_scale=1.0,
71
- shortcut=True, # PlusV2 的 shortcut 能减少色彩伪影
72
- )[0]
73
- return image, seed
74
- except gr.Error:
75
- raise
76
- except Exception:
77
- raise gr.Error("生成失败:\n```\n" + traceback.format_exc()[-2500:] + "\n```")
 
 
 
 
 
 
 
 
 
78
 
 
 
79
 
80
- with gr.Blocks(title="CharGen 固定角色生图 (CPU)") as demo:
81
- gr.Markdown(
82
- "# 🎭 CharGen — 固定角色生图(CPU 版)\n"
83
- "上传一张人脸参考图,所有生成图都是同一个角色。\n"
84
- "SD1.5 + IP-Adapter FaceID-PlusV2(带 CLIP),免费 cpu-basic,512×512 约几分钟/张。"
85
- )
86
- with gr.Row():
87
- with gr.Column():
88
- face = gr.Image(label="参考人脸(必传,正脸最佳)", type="pil")
89
- prompt = gr.Textbox(
90
- label="Prompt",
91
- value="a person smiling, natural skin tone, cinematic lighting, detailed face, 8k, photorealistic",
92
- )
93
- neg = gr.Textbox(
94
- label="Negative prompt",
95
- value="(lowres, low quality, worst quality), blurry, deformed, watermark, text, ugly, oversaturated, color noise, glitch, disfigured",
96
- )
97
- with gr.Row():
98
- seed = gr.Number(label="Seed(-1 = 随机)", value=-1)
99
- steps = gr.Slider(15, 50, 30, step=1, label="采样步数")
100
- with gr.Row():
101
- guidance = gr.Slider(1, 12, 5.0, step=0.5, label="CFG(建议 4–6)")
102
- ip_scale = gr.Slider(0, 1.0, 0.5, step=0.05, label="锁脸强度 scale(建议 0.4–0.6)")
103
- btn = gr.Button("生成(CPU 较慢,请耐心等待)", variant="primary")
104
- with gr.Column():
105
- out = gr.Image(label="生成结果")
106
- out_seed = gr.Number(label="实际使用的 seed")
107
 
108
- btn.click(
109
- generate,
110
- [face, prompt, neg, seed, steps, guidance, ip_scale],
111
- [out, out_seed],
112
- )
113
 
114
- demo.queue(max_size=8).launch(server_name="0.0.0.0", server_port=7860)
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException, Form
2
+ from fastapi.responses import Response
3
+ import tempfile
4
+ import os
5
  import traceback
6
+ import sys
7
+ from pathlib import Path
8
+ from typing import Optional, List
9
 
10
+ from modules.video_processor import generate_video
11
+ from modules.lut import LUTS
12
+ from modules.particles import EFFECTS
13
+ from modules.titles import TITLE_STYLES, TITLE_ANIMATIONS, TITLE_POSITIONS
 
 
 
 
14
 
15
+ app = FastAPI(title="Video Generator with Effects API")
 
16
 
17
+ @app.get("/")
18
+ async def root():
19
+ return {
20
+ "message": "Video Generator API",
21
+ "effects": EFFECTS,
22
+ "luts": {k: v["name"] for k, v in LUTS.items()},
23
+ "title_styles": TITLE_STYLES,
24
+ "title_animations": TITLE_ANIMATIONS,
25
+ "title_positions": TITLE_POSITIONS,
26
+ "features": ["ken_burns", "typewriter", "cinemascope", "title", "video_input"],
27
+ }
28
 
29
+ @app.get("/health")
30
+ async def health():
31
+ return {"status": "healthy"}
 
 
 
 
 
 
 
32
 
33
+ @app.post("/generate")
34
+ async def generate(
35
+ images: List[UploadFile] = File(None),
36
+ video: UploadFile = File(None),
37
+ audio: UploadFile = File(...),
38
+ srt_text: str = Form(""),
39
+ bgm: Optional[UploadFile] = File(None),
40
+ effect: str = Form("snow"),
41
+ lut: str = Form("none"),
42
+ ken_burns: bool = Form(False),
43
+ typewriter: bool = Form(False),
44
+ cinemascope: bool = Form(False),
45
+ title_text: str = Form(""),
46
+ title_style: str = Form("3d"),
47
+ title_color: str = Form("white"),
48
+ title_animation: str = Form("fade_in"),
49
+ title_duration: float = Form(4.0),
50
+ title_position: str = Form("center"),
51
+ title_start: float = Form(0.0),
52
+ title_font_size: int = Form(0),
53
+ typewriter_font_size: int = Form(0),
54
+ typewriter_color: str = Form("white"),
55
+ subtitle_color: str = Form("white"),
56
+ subtitle_effect: str = Form("none"),
57
+ fps: int = Form(25),
58
+ ):
59
+ # 验证
60
+ if not images and not video:
61
+ raise HTTPException(400, "必须提供 images 或 video 中的一个")
62
+ if images and video:
63
+ raise HTTPException(400, "不能同时提供 images 和 video")
64
+
65
+ # 创建临时目录
66
+ tmp_dir = tempfile.mkdtemp()
67
  try:
68
+ # 保存文件
69
+ image_paths = None
70
+ video_path = None
71
+ if images:
72
+ image_paths = []
73
+ for i, img in enumerate(images):
74
+ suffix = Path(img.filename).suffix if img.filename else ".jpg"
75
+ path = os.path.join(tmp_dir, f"image_{i}{suffix}")
76
+ with open(path, "wb") as f:
77
+ f.write(await img.read())
78
+ image_paths.append(path)
79
+ elif video:
80
+ suffix = Path(video.filename).suffix if video.filename else ".mp4"
81
+ video_path = os.path.join(tmp_dir, f"video{suffix}")
82
+ with open(video_path, "wb") as f:
83
+ f.write(await video.read())
84
+
85
+ audio_path = os.path.join(tmp_dir, "audio" + (Path(audio.filename).suffix or ".wav"))
86
+ with open(audio_path, "wb") as f:
87
+ f.write(await audio.read())
88
 
89
+ bgm_path = None
90
+ if bgm:
91
+ bgm_path = os.path.join(tmp_dir, "bgm" + (Path(bgm.filename).suffix or ".mp3"))
92
+ with open(bgm_path, "wb") as f:
93
+ f.write(await bgm.read())
94
 
95
+ output_path = os.path.join(tmp_dir, "output.mp4")
96
+
97
+ # 调用主合成函数
98
+ generate_video(
99
+ image_paths=image_paths,
100
+ video_path=video_path,
101
+ audio_path=audio_path,
102
+ srt_text=srt_text,
103
+ bgm_path=bgm_path,
104
+ effect=effect,
105
+ output_path=output_path,
106
+ lut=lut,
107
+ ken_burns=ken_burns,
108
+ typewriter=typewriter,
109
+ cinemascope=cinemascope,
110
+ title_text=title_text,
111
+ title_style=title_style,
112
+ title_color=title_color,
113
+ title_animation=title_animation,
114
+ title_duration=title_duration,
115
+ title_position=title_position,
116
+ title_start=title_start,
117
+ title_font_size=title_font_size,
118
+ typewriter_font_size=typewriter_font_size,
119
+ typewriter_color=typewriter_color,
120
+ subtitle_color=subtitle_color,
121
+ subtitle_effect=subtitle_effect,
122
+ fps=fps,
123
+ )
124
 
125
+ with open(output_path, "rb") as f:
126
+ video_bytes = f.read()
127
 
128
+ import shutil
129
+ shutil.rmtree(tmp_dir, ignore_errors=True)
130
+ return Response(content=video_bytes, media_type="video/mp4",
131
+ headers={"Content-Disposition": "attachment; filename=output.mp4"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
+ except Exception as e:
134
+ print(traceback.format_exc(), file=sys.stderr)
135
+ import shutil
136
+ shutil.rmtree(tmp_dir, ignore_errors=True)
137
+ raise HTTPException(500, detail=str(e))
138
 
139
+ if __name__ == "__main__":
140
+ import uvicorn
141
+ uvicorn.run(app, host="0.0.0.0", port=7860)
modules/__init__.py ADDED
File without changes
requirements.txt CHANGED
@@ -1,18 +1,8 @@
1
- diffusers==0.30.3
2
- transformers==4.44.2
3
- accelerate==0.34.2
4
- safetensors
5
- peft
6
- einops
7
- insightface==0.7.3
8
- onnxruntime
9
- opencv-python-headless
10
- git+https://github.com/tencent-ailab/IP-Adapter.git
11
- # gradio 4.44 需和 gradio_client 1.3.0 + pydantic<2.12 搭配,否則 /api_info 炸掉主页 500
12
- gradio==4.44.1
13
- gradio_client==1.3.0
14
- pydantic==2.10.6
15
- fastapi==0.115.7
16
- starlette==0.41.3
17
- pillow
18
- numpy<2
 
1
+ fastapi==0.104.1
2
+ uvicorn==0.24.0
3
+ python-multipart==0.0.6
4
+ numpy>=1.24.0
5
+ opencv-python-headless>=4.8.0
6
+ Pillow>=10.0.0
7
+ pydub>=0.25.0
8
+ librosa>=0.10.0