Spaces:
Running on Zero
Running on Zero
[Admin maintenance] Migrate grant to ZeroGPU
#114
by multimodalart HF Staff - opened
- app.py +35 -20
- requirements.txt +6 -5
app.py
CHANGED
|
@@ -5,6 +5,7 @@ subprocess.run(
|
|
| 5 |
"pip install ./gradio_magicquill-0.0.1-py3-none-any.whl"
|
| 6 |
)
|
| 7 |
)
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
from gradio_magicquill import MagicQuill
|
| 10 |
import random
|
|
@@ -13,8 +14,7 @@ import numpy as np
|
|
| 13 |
from PIL import Image, ImageOps
|
| 14 |
import base64
|
| 15 |
import io
|
| 16 |
-
from fastapi import
|
| 17 |
-
import uvicorn
|
| 18 |
from MagicQuill import folder_paths
|
| 19 |
from MagicQuill.scribble_color_edit import ScribbleColorEditModel
|
| 20 |
from gradio_client import Client, handle_file
|
|
@@ -28,7 +28,13 @@ import gc
|
|
| 28 |
snapshot_download(repo_id="LiuZichen/MagicQuill-models", repo_type="model", local_dir="models")
|
| 29 |
# HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 30 |
# The client has been made public. Welcome to duplicate our repo.
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
scribbleColorEditModel = ScribbleColorEditModel()
|
| 33 |
|
| 34 |
def tensor_to_numpy(tensor):
|
|
@@ -130,7 +136,7 @@ def guess_prompt_handler(original_image, add_color_image, add_edge_image):
|
|
| 130 |
add_color_image_file.close()
|
| 131 |
add_edge_mask_file.close()
|
| 132 |
|
| 133 |
-
res = client.predict(
|
| 134 |
handle_file(original_image_file.name),
|
| 135 |
handle_file(add_color_image_file.name),
|
| 136 |
handle_file(add_edge_mask_file.name),
|
|
@@ -186,7 +192,20 @@ def generate(ckpt_name, total_mask, original_image, add_color_image, add_edge_im
|
|
| 186 |
|
| 187 |
return final_image_base64
|
| 188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
def generate_image_handler(x, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
|
|
|
| 190 |
if seed == -1:
|
| 191 |
seed = random.randint(0, 2**32 - 1)
|
| 192 |
ms_data = x['from_frontend']
|
|
@@ -321,23 +340,19 @@ with gr.Blocks(css=css, head=head) as demo:
|
|
| 321 |
""")
|
| 322 |
demo.queue(max_size=20, status_update_rate=0.1)
|
| 323 |
|
| 324 |
-
app = FastAPI()
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
data = await request.json()
|
| 329 |
-
res = guess_prompt_handler(data['original_image'], data['add_color_image'], data['add_edge_image'])
|
| 330 |
-
return res
|
| 331 |
|
| 332 |
-
@app.post("/magic_quill/
|
| 333 |
-
async def
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
resized_img_base64 = "data:image/png;base64," + tensor_to_base64(resized_img_tensor)
|
| 337 |
-
return resized_img_base64
|
| 338 |
|
| 339 |
-
app
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
|
| 341 |
-
|
| 342 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 343 |
-
# demo.launch()
|
|
|
|
| 5 |
"pip install ./gradio_magicquill-0.0.1-py3-none-any.whl"
|
| 6 |
)
|
| 7 |
)
|
| 8 |
+
import spaces
|
| 9 |
import gradio as gr
|
| 10 |
from gradio_magicquill import MagicQuill
|
| 11 |
import random
|
|
|
|
| 14 |
from PIL import Image, ImageOps
|
| 15 |
import base64
|
| 16 |
import io
|
| 17 |
+
from fastapi import Request
|
|
|
|
| 18 |
from MagicQuill import folder_paths
|
| 19 |
from MagicQuill.scribble_color_edit import ScribbleColorEditModel
|
| 20 |
from gradio_client import Client, handle_file
|
|
|
|
| 28 |
snapshot_download(repo_id="LiuZichen/MagicQuill-models", repo_type="model", local_dir="models")
|
| 29 |
# HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 30 |
# The client has been made public. Welcome to duplicate our repo.
|
| 31 |
+
_HELPER_SPACE = "LiuZichen/MagicQuillHelper"
|
| 32 |
+
_client = None
|
| 33 |
+
def client():
|
| 34 |
+
global _client
|
| 35 |
+
if _client is None:
|
| 36 |
+
_client = Client(_HELPER_SPACE)
|
| 37 |
+
return _client
|
| 38 |
scribbleColorEditModel = ScribbleColorEditModel()
|
| 39 |
|
| 40 |
def tensor_to_numpy(tensor):
|
|
|
|
| 136 |
add_color_image_file.close()
|
| 137 |
add_edge_mask_file.close()
|
| 138 |
|
| 139 |
+
res = client().predict(
|
| 140 |
handle_file(original_image_file.name),
|
| 141 |
handle_file(add_color_image_file.name),
|
| 142 |
handle_file(add_edge_mask_file.name),
|
|
|
|
| 192 |
|
| 193 |
return final_image_base64
|
| 194 |
|
| 195 |
+
def _generate_duration(x, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
| 196 |
+
try:
|
| 197 |
+
s = int(steps or 20)
|
| 198 |
+
except Exception:
|
| 199 |
+
s = 20
|
| 200 |
+
return max(60, 3 * s)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
from MagicQuill.comfy import model_management as _mm
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
@spaces.GPU(duration=_generate_duration)
|
| 207 |
def generate_image_handler(x, ckpt_name, negative_prompt, fine_edge, grow_size, edge_strength, color_strength, inpaint_strength, seed, steps, cfg, sampler_name, scheduler):
|
| 208 |
+
_mm.unload_all_models()
|
| 209 |
if seed == -1:
|
| 210 |
seed = random.randint(0, 2**32 - 1)
|
| 211 |
ms_data = x['from_frontend']
|
|
|
|
| 340 |
""")
|
| 341 |
demo.queue(max_size=20, status_update_rate=0.1)
|
| 342 |
|
|
|
|
| 343 |
|
| 344 |
+
if __name__ == "__main__":
|
| 345 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, prevent_thread_lock=True)
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
+
@demo.app.post("/magic_quill/guess_prompt")
|
| 348 |
+
async def guess_prompt(request: Request):
|
| 349 |
+
data = await request.json()
|
| 350 |
+
return guess_prompt_handler(data['original_image'], data['add_color_image'], data['add_edge_image'])
|
|
|
|
|
|
|
| 351 |
|
| 352 |
+
@demo.app.post("/magic_quill/process_background_img")
|
| 353 |
+
async def process_background_img(request: Request):
|
| 354 |
+
img = await request.json()
|
| 355 |
+
resized_img_tensor = load_and_resize_image(img)
|
| 356 |
+
return "data:image/png;base64," + tensor_to_base64(resized_img_tensor)
|
| 357 |
|
| 358 |
+
demo.block_thread()
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -14,7 +14,7 @@ anyio==4.4.0
|
|
| 14 |
async-timeout==4.0.3
|
| 15 |
attrs==23.2.0
|
| 16 |
beautifulsoup4==4.12.3
|
| 17 |
-
bitsandbytes
|
| 18 |
certifi==2024.7.4
|
| 19 |
cffi==1.16.0
|
| 20 |
chardet==5.2.0
|
|
@@ -33,7 +33,7 @@ einops-exts==0.0.4
|
|
| 33 |
embreex==2.17.7.post5
|
| 34 |
eval-type-backport==0.2.0
|
| 35 |
exceptiongroup==1.2.2
|
| 36 |
-
fastapi
|
| 37 |
ffmpy==0.4.0
|
| 38 |
filelock==3.15.4
|
| 39 |
flatbuffers==24.3.25
|
|
@@ -132,11 +132,11 @@ sounddevice==0.4.7
|
|
| 132 |
soupsieve==2.5
|
| 133 |
spandrel==0.3.4
|
| 134 |
stanza==1.1.1
|
| 135 |
-
starlette
|
| 136 |
svg-path==6.3
|
| 137 |
svglib==1.5.1
|
| 138 |
svgwrite==1.4.3
|
| 139 |
-
sympy==1.13.
|
| 140 |
tabulate==0.9.0
|
| 141 |
termcolor==2.4.0
|
| 142 |
threadpoolctl==3.5.0
|
|
@@ -151,7 +151,8 @@ tqdm==4.66.5
|
|
| 151 |
trampoline==0.1.2
|
| 152 |
transformers==4.37.2
|
| 153 |
trimesh==4.4.3
|
| 154 |
-
|
|
|
|
| 155 |
torchsde==0.2.6
|
| 156 |
typer==0.12.5
|
| 157 |
typing-extensions==4.12.2
|
|
|
|
| 14 |
async-timeout==4.0.3
|
| 15 |
attrs==23.2.0
|
| 16 |
beautifulsoup4==4.12.3
|
| 17 |
+
bitsandbytes
|
| 18 |
certifi==2024.7.4
|
| 19 |
cffi==1.16.0
|
| 20 |
chardet==5.2.0
|
|
|
|
| 33 |
embreex==2.17.7.post5
|
| 34 |
eval-type-backport==0.2.0
|
| 35 |
exceptiongroup==1.2.2
|
| 36 |
+
fastapi<0.112
|
| 37 |
ffmpy==0.4.0
|
| 38 |
filelock==3.15.4
|
| 39 |
flatbuffers==24.3.25
|
|
|
|
| 132 |
soupsieve==2.5
|
| 133 |
spandrel==0.3.4
|
| 134 |
stanza==1.1.1
|
| 135 |
+
starlette<0.38
|
| 136 |
svg-path==6.3
|
| 137 |
svglib==1.5.1
|
| 138 |
svgwrite==1.4.3
|
| 139 |
+
sympy==1.13.3
|
| 140 |
tabulate==0.9.0
|
| 141 |
termcolor==2.4.0
|
| 142 |
threadpoolctl==3.5.0
|
|
|
|
| 151 |
trampoline==0.1.2
|
| 152 |
transformers==4.37.2
|
| 153 |
trimesh==4.4.3
|
| 154 |
+
torch==2.8.0
|
| 155 |
+
torchvision==0.23.0
|
| 156 |
torchsde==0.2.6
|
| 157 |
typer==0.12.5
|
| 158 |
typing-extensions==4.12.2
|