Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
|
|
|
| 26 |
prompt,
|
| 27 |
-
|
| 28 |
seed,
|
| 29 |
randomize_seed,
|
| 30 |
-
width,
|
| 31 |
-
height,
|
| 32 |
guidance_scale,
|
| 33 |
-
|
| 34 |
progress=gr.Progress(track_tqdm=True),
|
| 35 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
-
"Astronaut in
|
| 56 |
-
"
|
| 57 |
-
"A delicious ceviche cheesecake slice",
|
| 58 |
]
|
| 59 |
|
| 60 |
css = """
|
|
@@ -66,89 +83,80 @@ css = """
|
|
| 66 |
|
| 67 |
with gr.Blocks(css=css) as demo:
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" #
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
| 73 |
-
label="Prompt",
|
| 74 |
-
|
| 75 |
-
max_lines=1,
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
-
container=False,
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
|
|
|
|
| 92 |
seed = gr.Slider(
|
| 93 |
-
label="
|
| 94 |
minimum=0,
|
| 95 |
maximum=MAX_SEED,
|
| 96 |
step=1,
|
| 97 |
value=0,
|
| 98 |
)
|
| 99 |
|
| 100 |
-
randomize_seed = gr.Checkbox(label="
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
step=0.1,
|
| 125 |
-
value=0.0, # Replace with defaults that work for your model
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
num_inference_steps = gr.Slider(
|
| 129 |
-
label="Number of inference steps",
|
| 130 |
-
minimum=1,
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
gr.on(
|
| 138 |
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
| 140 |
inputs=[
|
|
|
|
| 141 |
prompt,
|
| 142 |
-
|
| 143 |
seed,
|
| 144 |
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
guidance_scale,
|
| 148 |
-
|
| 149 |
],
|
| 150 |
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
# API client for the external Space
|
| 6 |
+
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
| 7 |
+
|
| 8 |
+
LORA_STYLES = [
|
| 9 |
+
'Multiple-Angles',
|
| 10 |
+
'Photo-to-Anime',
|
| 11 |
+
'Anime-V2',
|
| 12 |
+
'Light-Migration',
|
| 13 |
+
'Upscaler',
|
| 14 |
+
'Style-Transfer',
|
| 15 |
+
'Manga-Tone',
|
| 16 |
+
'Anything2Real',
|
| 17 |
+
'Fal-Multiple-Angles',
|
| 18 |
+
'Polaroid-Photo',
|
| 19 |
+
'Unblur-Anything',
|
| 20 |
+
'Midnight-Noir-Eyes-Spotlight',
|
| 21 |
+
'Hyper-Realistic-Portrait',
|
| 22 |
+
'Ultra-Realistic-Portrait',
|
| 23 |
+
'Pixar-Inspired-3D',
|
| 24 |
+
'Noir-Comic-Book',
|
| 25 |
+
'Any-light',
|
| 26 |
+
'Studio-DeLight',
|
| 27 |
+
'Cinematic-FlatLog',
|
| 28 |
+
]
|
| 29 |
+
MAX_SEED = 2**32-1
|
| 30 |
|
|
|
|
| 31 |
def infer(
|
| 32 |
+
image,
|
| 33 |
prompt,
|
| 34 |
+
lora_adapter,
|
| 35 |
seed,
|
| 36 |
randomize_seed,
|
|
|
|
|
|
|
| 37 |
guidance_scale,
|
| 38 |
+
steps,
|
| 39 |
progress=gr.Progress(track_tqdm=True),
|
| 40 |
):
|
| 41 |
+
# Prepare images input as per API (expects Gallery [list of dicts])
|
| 42 |
+
images = []
|
| 43 |
+
if image is not None:
|
| 44 |
+
if isinstance(image, list): # Gradio Gallery
|
| 45 |
+
for im in image:
|
| 46 |
+
images.append({"image": {"path": im}, "caption": None})
|
| 47 |
+
else:
|
| 48 |
+
images.append({"image": {"path": image}, "caption": None})
|
| 49 |
+
|
| 50 |
if randomize_seed:
|
| 51 |
seed = random.randint(0, MAX_SEED)
|
| 52 |
|
| 53 |
+
try:
|
| 54 |
+
result = space_client.predict(
|
| 55 |
+
images=images,
|
| 56 |
+
prompt=prompt,
|
| 57 |
+
lora_adapter=lora_adapter,
|
| 58 |
+
seed=float(seed),
|
| 59 |
+
randomize_seed=bool(randomize_seed),
|
| 60 |
+
guidance_scale=float(guidance_scale),
|
| 61 |
+
steps=float(steps),
|
| 62 |
+
api_name="/infer",
|
| 63 |
+
)
|
| 64 |
+
# result is a tuple: (image_dict, seed)
|
| 65 |
+
image_info, seed_used = result
|
| 66 |
+
# The API may return image at .url or .path, we use .url if available
|
| 67 |
+
img_url = image_info.get("url") or image_info.get("path")
|
| 68 |
+
return img_url, seed_used
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return None, seed
|
| 71 |
|
| 72 |
examples = [
|
| 73 |
+
["", "Astronaut in jungle, anime style", "Photo-to-Anime", 0, True, 1.0, 4],
|
| 74 |
+
["", "A delicious ceviche cheesecake slice", "Style-Transfer", 0, True, 1.0, 4],
|
|
|
|
| 75 |
]
|
| 76 |
|
| 77 |
css = """
|
|
|
|
| 83 |
|
| 84 |
with gr.Blocks(css=css) as demo:
|
| 85 |
with gr.Column(elem_id="col-container"):
|
| 86 |
+
gr.Markdown(" # 图像编辑 API Demo (基于 prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast)")
|
| 87 |
|
| 88 |
+
with gr.Row():
|
| 89 |
+
image = gr.Image(
|
| 90 |
+
label="上传图片",
|
| 91 |
+
source="upload",
|
| 92 |
+
tool=None,
|
| 93 |
+
type="filepath",
|
| 94 |
+
elem_id="input-image"
|
| 95 |
+
)
|
| 96 |
with gr.Row():
|
| 97 |
prompt = gr.Text(
|
| 98 |
+
label="编辑描述(Prompt)",
|
| 99 |
+
placeholder="请输入图片编辑描述...",
|
|
|
|
|
|
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
+
with gr.Row():
|
| 103 |
+
lora_adapter = gr.Dropdown(
|
| 104 |
+
label="编辑风格(Style)",
|
| 105 |
+
choices=LORA_STYLES,
|
| 106 |
+
value="Photo-to-Anime"
|
| 107 |
+
)
|
| 108 |
|
| 109 |
+
run_button = gr.Button("执行编辑", scale=1, variant="primary")
|
| 110 |
|
| 111 |
+
result = gr.Image(label="结果图片", show_label=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
with gr.Accordion("高级设置", open=False):
|
| 114 |
seed = gr.Slider(
|
| 115 |
+
label="随机种子",
|
| 116 |
minimum=0,
|
| 117 |
maximum=MAX_SEED,
|
| 118 |
step=1,
|
| 119 |
value=0,
|
| 120 |
)
|
| 121 |
|
| 122 |
+
randomize_seed = gr.Checkbox(label="随机种子", value=True)
|
| 123 |
+
|
| 124 |
+
guidance_scale = gr.Slider(
|
| 125 |
+
label="引导强度(Guidance Scale)",
|
| 126 |
+
minimum=0.1,
|
| 127 |
+
maximum=10.0,
|
| 128 |
+
step=0.1,
|
| 129 |
+
value=1.0,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
steps = gr.Slider(
|
| 133 |
+
label="推理步数(Steps)",
|
| 134 |
+
minimum=1,
|
| 135 |
+
maximum=50,
|
| 136 |
+
step=1,
|
| 137 |
+
value=4,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
gr.Examples(
|
| 141 |
+
examples=examples,
|
| 142 |
+
inputs=[image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 143 |
+
label="示例",
|
| 144 |
+
)
|
| 145 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
gr.on(
|
| 147 |
triggers=[run_button.click, prompt.submit],
|
| 148 |
fn=infer,
|
| 149 |
inputs=[
|
| 150 |
+
image,
|
| 151 |
prompt,
|
| 152 |
+
lora_adapter,
|
| 153 |
seed,
|
| 154 |
randomize_seed,
|
|
|
|
|
|
|
| 155 |
guidance_scale,
|
| 156 |
+
steps,
|
| 157 |
],
|
| 158 |
outputs=[result, seed],
|
| 159 |
)
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
+
demo.launch()
|