Spaces:
Running on Zero
Running on Zero
Add Black Forest Labs content moderation
#23
by alexanderwangus - opened
Files changed (2)
- app.py+19-4
- requirements.txt+1-1
13 unmodified lines141516171819136 unmodified lines15615715815916016116216316416516616716814 unmodified lines18318418518618718831 unmodified lines22022122222322422513 unmodified linesimport jsonimport base64from huggingface_hub import InferenceClientdtype = torch.bfloat16device = "cuda" if torch.cuda.is_available() else "cpu"136 unmodified linesreturn new_width, new_height# Updated duration function to match generate_image arguments (including progress)def get_duration(prompt_embeds, image_list, width, height, num_inference_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):num_images = 0 if image_list is None else len(image_list)step_duration = 1 + 0.8 * num_imagesreturn max(65, num_inference_steps * step_duration + 10)@spaces.GPU(duration=get_duration)def generate_image(prompt_embeds, image_list, width, height, num_inference_steps, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):# Move embeddings to GPU only when inside the GPU decorated functionprompt_embeds = prompt_embeds.to(device)14 unmodified linesprogress(0, desc="Starting generation...")image = pipe(**pipe_kwargs).images[0]return imagedef infer(prompt, input_images=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, prompt_upsampling=False, progress=gr.Progress(track_tqdm=True)):31 unmodified linesnum_inference_steps,guidance_scale,seed,progress)13 unmodified lines1415161718192021222324252627282930136 unmodified lines16716816917017117217317417517617717817918014 unmodified lines19519619719819920020131 unmodified lines23323423523623723823924013 unmodified linesimport jsonimport base64from huggingface_hub import InferenceClientfrom huggingface_hub import hf_hub_downloadhf_hub_download("black-forest-labs/hf-spaces-moderation-client","bfl_moderation.py",revision="main",local_dir=os.path.dirname(os.path.abspath(__file__)),)import bfl_moderation # noqa: E402SOURCE = "hf-space:black-forest-labs/FLUX.2-dev"dtype = torch.bfloat16device = "cuda" if torch.cuda.is_available() else "cpu"136 unmodified linesreturn new_width, new_height# Updated duration function to match generate_image arguments (including progress)def get_duration(prompt_embeds, image_list, width, height, num_inference_steps, guidance_scale, seed, prompt, final_prompt, progress=gr.Progress(track_tqdm=True)):num_images = 0 if image_list is None else len(image_list)step_duration = 1 + 0.8 * num_imagesreturn max(75, num_inference_steps * step_duration + 20)@spaces.GPU(duration=get_duration, size="xlarge")def generate_image(prompt_embeds, image_list, width, height, num_inference_steps, guidance_scale, seed, prompt, final_prompt, progress=gr.Progress(track_tqdm=True)):pending = bfl_moderation.start_input_check(source=SOURCE, prompt=prompt, images=image_list or [])# Move embeddings to GPU only when inside the GPU decorated functionprompt_embeds = prompt_embeds.to(device)14 unmodified linesprogress(0, desc="Starting generation...")image = pipe(**pipe_kwargs).images[0]bfl_moderation.check_output(source=SOURCE, prompt=final_prompt, image=image, input_check=pending.result())return imagedef infer(prompt, input_images=None, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, prompt_upsampling=False, progress=gr.Progress(track_tqdm=True)):31 unmodified linesnum_inference_steps,guidance_scale,seed,prompt,final_prompt,progress)
3 unmodified lines4567893 unmodified linesacceleratesafetensorsbitsandbytestorchaokernelstqdm==4.67.13 unmodified lines4567893 unmodified linesacceleratesafetensorsbitsandbytestorchao==0.17.0kernelstqdm==4.67.1
app.py+19-4
requirements.txt+1-1