Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,160 +1,100 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
import os
|
| 3 |
-
# import subprocess
|
| 4 |
-
# import shlex
|
| 5 |
-
# if os.getenv('SYSTEM') == 'spaces':
|
| 6 |
-
# git_repo = "https://github.com/huggingface/transformers.git"
|
| 7 |
-
# subprocess.call(shlex.split(f'pip install git+{git_repo}'))
|
| 8 |
-
|
| 9 |
-
import time
|
| 10 |
import torch
|
| 11 |
-
|
| 12 |
import gradio as gr
|
| 13 |
-
from
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
| 17 |
-
MODEL = os.environ.get("MODEL_ID")
|
| 18 |
-
|
| 19 |
-
TITLE = "<h1><center>MiniCPM3-4B</center></h1>"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
<p>MiniCPM3-4B is the 3rd generation of MiniCPM series.</p>
|
| 24 |
-
</center>
|
| 25 |
-
"""
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
CSS = """
|
| 29 |
-
.duplicate-button {
|
| 30 |
-
margin: auto !important;
|
| 31 |
-
color: white !important;
|
| 32 |
-
background: black !important;
|
| 33 |
-
border-radius: 100vh !important;
|
| 34 |
-
}
|
| 35 |
-
h3 {
|
| 36 |
-
text-align: center;
|
| 37 |
-
}
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
device = "cuda" # for GPU usage or "cpu" for CPU usage
|
| 41 |
-
|
| 42 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)
|
| 43 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 44 |
-
MODEL,
|
| 45 |
-
torch_dtype=torch.bfloat16,
|
| 46 |
-
device_map="auto",
|
| 47 |
-
trust_remote_code=True)
|
| 48 |
|
| 49 |
@spaces.GPU()
|
| 50 |
-
def
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
conversation.append({"role": "user", "content": message})
|
| 70 |
-
|
| 71 |
-
input_text=tokenizer.apply_chat_template(conversation, tokenize=False)
|
| 72 |
-
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 73 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 74 |
-
|
| 75 |
-
generate_kwargs = dict(
|
| 76 |
-
input_ids=inputs,
|
| 77 |
-
max_new_tokens = max_new_tokens,
|
| 78 |
-
do_sample = False if temperature == 0 else True,
|
| 79 |
-
top_p = top_p,
|
| 80 |
-
top_k = top_k,
|
| 81 |
-
temperature = temperature,
|
| 82 |
-
streamer=streamer,
|
| 83 |
-
repetition_penalty=penalty,
|
| 84 |
-
eos_token_id = [2, 73440],
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
with torch.no_grad():
|
| 88 |
-
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 89 |
-
thread.start()
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
gr.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
minimum=0.0,
|
| 142 |
-
maximum=2.0,
|
| 143 |
-
step=0.1,
|
| 144 |
-
value=1.2,
|
| 145 |
-
label="Repetition penalty",
|
| 146 |
-
render=False,
|
| 147 |
-
),
|
| 148 |
],
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
],
|
| 155 |
-
|
| 156 |
)
|
| 157 |
|
| 158 |
-
|
| 159 |
if __name__ == "__main__":
|
| 160 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
+
from diffusers import FluxInpaintPipeline
|
| 5 |
+
import random
|
| 6 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 9 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
pipe = FluxInpaintPipeline.from_pretrained(
|
| 12 |
+
"black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to(DEVICE)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@spaces.GPU()
|
| 16 |
+
def inpaintGen(
|
| 17 |
+
imgMask,
|
| 18 |
+
inpaint_prompt: str,
|
| 19 |
+
strength: float,
|
| 20 |
+
guidance: float,
|
| 21 |
+
num_steps: int,
|
| 22 |
+
seed: int,
|
| 23 |
+
randomize_seed: bool,
|
| 24 |
+
progress=gr.Progress(track_tqdm=True)):
|
| 25 |
+
|
| 26 |
+
source_img = imgMask["background"]
|
| 27 |
+
mask_img = imgMask["layers"][0]
|
| 28 |
+
|
| 29 |
+
if not source_path:
|
| 30 |
+
raise gr.Error("Please upload an image.")
|
| 31 |
+
|
| 32 |
+
if not mask_path:
|
| 33 |
+
raise gr.Error("Please draw a mask on the image.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
width, height = source_img.size
|
| 36 |
+
|
| 37 |
+
if randomize_seed:
|
| 38 |
+
seed = random.randint(0, MAX_SEED)
|
| 39 |
+
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
| 40 |
+
|
| 41 |
+
result = pipe(
|
| 42 |
+
prompt=inpaint_prompt,
|
| 43 |
+
image=source_img,
|
| 44 |
+
seed=seed,
|
| 45 |
+
mask_image=mask_img,
|
| 46 |
+
width=width,
|
| 47 |
+
height=height,
|
| 48 |
+
strength=strength,
|
| 49 |
+
num_inference_steps=num_steps,
|
| 50 |
+
generator=generator,
|
| 51 |
+
guidance_scale=guidance
|
| 52 |
+
).images[0]
|
| 53 |
+
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
with gr.Blocks(theme="ocean", title="Flux.1 dev inpaint", css=CSS) as demo:
|
| 58 |
+
gr.HTML("<h1><center>Flux.1 dev Inpaint</center></h1>")
|
| 59 |
+
gr.HTML("""
|
| 60 |
+
<p>
|
| 61 |
+
<center>
|
| 62 |
+
A partial redraw of the image based on your prompt words and occluded parts.
|
| 63 |
+
</center>
|
| 64 |
+
</p>
|
| 65 |
+
""")
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
imgMask = gr.ImageMask(type="pil", label="Image", layers=False, height=800)
|
| 69 |
+
inpaint_prompt = gr.Textbox(label='Prompts ✏️', placeholder="A hat...")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
Inpaint_sendBtn = gr.Button(value="Submit", variant='primary')
|
| 72 |
+
Inpaint_clearBtn = gr.ClearButton([imgMask, inpaint_prompt], value="Clear")
|
| 73 |
+
image_out = gr.Image(type="pil", label="Output", height=960)
|
| 74 |
+
with gr.Accordion("Advanced ⚙️", open=False):
|
| 75 |
+
strength = gr.Slider(label="Strength", minimum=0, maximum=1, value=1, step=0.1)
|
| 76 |
+
guidance = gr.Slider(label="Guidance scale", minimum=1, maximum=20, value=7.5, step=0.1)
|
| 77 |
+
num_steps = gr.Slider(label="Steps", minimum=1, maximum=20, value=20, step=1)
|
| 78 |
+
seed = gr.Number(label="Seed", value=42, precision=0)
|
| 79 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 80 |
+
|
| 81 |
+
gr.on(
|
| 82 |
+
triggers = [
|
| 83 |
+
inpaint_prompt.submit,
|
| 84 |
+
Inpaint_sendBtn.click,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
],
|
| 86 |
+
fn = inpaintGen,
|
| 87 |
+
inputs = [
|
| 88 |
+
imgMask,
|
| 89 |
+
inpaint_prompt,
|
| 90 |
+
strength,
|
| 91 |
+
guidance,
|
| 92 |
+
num_steps,
|
| 93 |
+
seed,
|
| 94 |
+
randomize_seed
|
| 95 |
],
|
| 96 |
+
outputs = [image_out, seed]
|
| 97 |
)
|
| 98 |
|
|
|
|
| 99 |
if __name__ == "__main__":
|
| 100 |
+
demo.queue(api_open=False).launch(show_api=False, share=False)
|