Spaces:
Running
on
Zero
Running
on
Zero
change simple
Browse files
app.py
CHANGED
|
@@ -2,11 +2,20 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
| 4 |
from PIL import Image
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
import numpy as np
|
| 8 |
import shutil
|
| 9 |
#shutil.rmtree("/home/user/app/.gradio/cached_examples/23")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def sanitize_prompt(prompt):
|
| 12 |
# Allow only alphanumeric characters, spaces, and basic punctuation
|
|
@@ -14,7 +23,26 @@ def sanitize_prompt(prompt):
|
|
| 14 |
sanitized_prompt = allowed_chars.sub("", prompt)
|
| 15 |
return sanitized_prompt
|
| 16 |
|
| 17 |
-
#@spaces.GPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def process_images(image, image2=None,prompt="a girl",inpaint_model="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0):
|
| 19 |
print("start process_images")
|
| 20 |
try:
|
|
@@ -37,11 +65,11 @@ def process_images(image, image2=None,prompt="a girl",inpaint_model="black-fores
|
|
| 37 |
mask = image['layers'][0]
|
| 38 |
|
| 39 |
|
| 40 |
-
output =
|
| 41 |
except Exception as e:
|
| 42 |
print(f"An error occurred: {e}")
|
| 43 |
gr.Error(e)
|
| 44 |
-
|
| 45 |
return output
|
| 46 |
|
| 47 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
| 4 |
from PIL import Image
|
| 5 |
+
|
| 6 |
import os
|
| 7 |
import numpy as np
|
| 8 |
import shutil
|
| 9 |
#shutil.rmtree("/home/user/app/.gradio/cached_examples/23")
|
| 10 |
+
import torch
|
| 11 |
+
from diffusers import FluxImg2ImgPipeline
|
| 12 |
+
|
| 13 |
+
dtype = torch.bfloat16
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
pipe = FluxImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16).to(device)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
|
| 20 |
def sanitize_prompt(prompt):
|
| 21 |
# Allow only alphanumeric characters, spaces, and basic punctuation
|
|
|
|
| 23 |
sanitized_prompt = allowed_chars.sub("", prompt)
|
| 24 |
return sanitized_prompt
|
| 25 |
|
| 26 |
+
#@spaces.GPU
|
| 27 |
+
def process_img2img(image,mask_image,prompt="a person",model_id="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0,num_inference_steps=4):
|
| 28 |
+
print("start process image process_image")
|
| 29 |
+
if image == None:
|
| 30 |
+
print("empty input image returned")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
generators = []
|
| 34 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 35 |
+
generators.append(generator)
|
| 36 |
+
# more parameter see https://huggingface.co/docs/diffusers/api/pipelines/flux#diffusers.FluxInpaintPipeline
|
| 37 |
+
print(prompt)
|
| 38 |
+
output = pipe(prompt=prompt, image=image,generator=generator,strength=strength
|
| 39 |
+
,guidance_scale=0,num_inference_steps=num_inference_steps,max_sequence_length=256)
|
| 40 |
+
|
| 41 |
+
# TODO support mask
|
| 42 |
+
return output.images[0]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@spaces.GPU(duration=180)
|
| 46 |
def process_images(image, image2=None,prompt="a girl",inpaint_model="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0):
|
| 47 |
print("start process_images")
|
| 48 |
try:
|
|
|
|
| 65 |
mask = image['layers'][0]
|
| 66 |
|
| 67 |
|
| 68 |
+
output = process_img2img(image["background"],mask,prompt,inpaint_model,strength,seed)
|
| 69 |
except Exception as e:
|
| 70 |
print(f"An error occurred: {e}")
|
| 71 |
gr.Error(e)
|
| 72 |
+
print("end process_images")
|
| 73 |
return output
|
| 74 |
|
| 75 |
|