Download app.py from jacky3102/TTS: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/spaces/jacky3102/TTS/resolve/main/app.py
- Command line
-
hf download hf://spaces/jacky3102/TTS/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jacky3102/TTS/resolve/main/app.py
3.75 kB
| import inspect | |
| import random | |
| import cv2 | |
| import numpy as np | |
| import torch | |
| import gradio as gr | |
| import spaces | |
| from insightface.app import FaceAnalysis | |
| from diffusers import StableDiffusionXLPipeline | |
| torch_dtype = torch.float16 | |
| # ---- 模块级别加载并搬到 cuda(ZeroGPU 在 @spaces.GPU 外是 CUDA 模拟模式,启动期即可 .to("cuda"))---- | |
| pipe = StableDiffusionXLPipeline.from_pretrained( | |
| "stabilityai/stable-diffusion-xl-base-1.0", | |
| torch_dtype=torch_dtype, | |
| safety_checker=None, | |
| requires_safety_checker=False, | |
| ) | |
| # FaceID-PlusV2 (SDXL):上传一张脸的 embedding 即可锁定角色 | |
| pipe.load_ip_adapter( | |
| "h94/IP-Adapter-FaceID", | |
| weight_name="ip-adapter-faceid-plusv2_sdxl.bin", | |
| ) | |
| pipe.to("cuda") | |
| # ---- 人脸编码器(CPU 推理,避免启动期 CUDA provider 问题)---- | |
| face_app = FaceAnalysis(name="antelopev2", root="./", providers=["CPUExecutionProvider"]) | |
| face_app.prepare(ctx_id=0, det_size=(640, 640)) | |
| MAX_SEED = 2**32 - 1 | |
| _CALL_PARAMS = set(inspect.signature(pipe.__call__).parameters) | |
| def generate(face_image, prompt, negative_prompt, seed, steps, guidance, ip_scale): | |
| if face_image is None: | |
| raise gr.Error("请先上传一张人脸参考图(正脸效果最好)") | |
| rgb = np.array(face_image.convert("RGB")) | |
| faces = face_app.get(cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)) | |
| if len(faces) == 0: | |
| raise gr.Error("参考图里检测不到人脸,换一张清晰正脸照试试") | |
| emb = torch.from_numpy(np.asarray(faces[0].normed_embedding, dtype=np.float32)) | |
| seed = int(seed) | |
| if seed < 0: | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator(device="cuda").manual_seed(seed) | |
| kwargs = dict( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| num_inference_steps=int(steps), | |
| guidance_scale=float(guidance), | |
| generator=generator, | |
| ) | |
| if "ip_adapter_image_embeds" in _CALL_PARAMS: | |
| kwargs["ip_adapter_image_embeds"] = [emb] | |
| elif "image_embeds" in _CALL_PARAMS: | |
| kwargs["image_embeds"] = emb | |
| if "ip_adapter_scale" in _CALL_PARAMS: | |
| kwargs["ip_adapter_scale"] = float(ip_scale) | |
| image = pipe(**kwargs).images[0] | |
| return image, seed | |
| with gr.Blocks(title="CharGen 固定角色生图") as demo: | |
| gr.Markdown( | |
| "# 🎭 CharGen — 固定角色生图\n" | |
| "上传一张人脸参考图,之后所有生成图都是同一个角色。\n" | |
| "SDXL + IP-Adapter FaceID-PlusV2,ZeroGPU 免费 GPU 额度。" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| face = gr.Image(label="参考人脸(必传,正脸最佳)", type="pil") | |
| prompt = gr.Textbox( | |
| label="Prompt", | |
| value="a person smiling, cinematic lighting, 8k, detailed face", | |
| ) | |
| neg = gr.Textbox( | |
| label="Negative prompt", | |
| value="(lowres, low quality, worst quality), blurry, deformed, watermark, text, ugly", | |
| ) | |
| with gr.Row(): | |
| seed = gr.Number(label="Seed(-1 = 随机)", value=-1) | |
| steps = gr.Slider(10, 50, 30, step=1, label="采样步数") | |
| with gr.Row(): | |
| guidance = gr.Slider(1, 15, 5, step=0.5, label="CFG") | |
| ip_scale = gr.Slider(0, 1.5, 0.8, step=0.05, label="脸相似度 ip-adapter scale") | |
| btn = gr.Button("生成", variant="primary") | |
| with gr.Column(): | |
| out = gr.Image(label="生成结果") | |
| out_seed = gr.Number(label="实际使用的 seed") | |
| btn.click( | |
| generate, | |
| [face, prompt, neg, seed, steps, guidance, ip_scale], | |
| [out, out_seed], | |
| ) | |
| demo.launch() | |