Update app.py
Browse files
app.py
CHANGED
|
@@ -1,226 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# API client for the external Space
|
| 7 |
-
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
| 8 |
-
|
| 9 |
-
LORA_STYLES = [
|
| 10 |
-
'Multiple-Angles',
|
| 11 |
-
'Photo-to-Anime',
|
| 12 |
-
'Anime-V2',
|
| 13 |
-
'Light-Migration',
|
| 14 |
-
'Upscaler',
|
| 15 |
-
'Style-Transfer',
|
| 16 |
-
'Manga-Tone',
|
| 17 |
-
'Anything2Real',
|
| 18 |
-
'Fal-Multiple-Angles',
|
| 19 |
-
'Polaroid-Photo',
|
| 20 |
-
'Unblur-Anything',
|
| 21 |
-
'Midnight-Noir-Eyes-Spotlight',
|
| 22 |
-
'Hyper-Realistic-Portrait',
|
| 23 |
-
'Ultra-Realistic-Portrait',
|
| 24 |
-
'Pixar-Inspired-3D',
|
| 25 |
-
'Noir-Comic-Book',
|
| 26 |
-
'Any-light',
|
| 27 |
-
'Studio-DeLight',
|
| 28 |
-
'Cinematic-FlatLog',
|
| 29 |
-
]
|
| 30 |
-
MAX_SEED = 2**31 - 1
|
| 31 |
-
|
| 32 |
-
def encode_image_to_gallery_dict(image_path):
|
| 33 |
-
if not image_path or not isinstance(image_path, str):
|
| 34 |
-
return None
|
| 35 |
-
try:
|
| 36 |
-
file_data = handle_file(image_path)
|
| 37 |
-
return {"image": file_data, "caption": ""}
|
| 38 |
-
except Exception as e:
|
| 39 |
-
print(f"无法读取图片: {image_path}: {e}")
|
| 40 |
-
return None
|
| 41 |
-
|
| 42 |
-
def infer(
|
| 43 |
-
image,
|
| 44 |
-
prompt,
|
| 45 |
-
lora_adapter,
|
| 46 |
-
seed,
|
| 47 |
-
randomize_seed,
|
| 48 |
-
guidance_scale,
|
| 49 |
-
steps,
|
| 50 |
-
progress=gr.Progress(track_tqdm=True),
|
| 51 |
-
):
|
| 52 |
-
# Process input image(s) as a list of dict, each of form {"image": {"data": ...}}
|
| 53 |
-
images_input = []
|
| 54 |
-
if image is not None:
|
| 55 |
-
if isinstance(image, list): # Gradio Gallery
|
| 56 |
-
for im in image:
|
| 57 |
-
img_obj = encode_image_to_gallery_dict(im)
|
| 58 |
-
if img_obj:
|
| 59 |
-
images_input.append(img_obj)
|
| 60 |
-
else:
|
| 61 |
-
print(f"警告: 路径无效或无法读取: {im}")
|
| 62 |
-
else:
|
| 63 |
-
img_obj = encode_image_to_gallery_dict(image)
|
| 64 |
-
if img_obj:
|
| 65 |
-
images_input.append(img_obj)
|
| 66 |
-
else:
|
| 67 |
-
print(f"警告: 路径无效或无法读取: {image}")
|
| 68 |
-
|
| 69 |
-
if len(images_input) == 0:
|
| 70 |
-
print("未检测到有效图片,未能上传图片。")
|
| 71 |
-
return None, seed
|
| 72 |
-
|
| 73 |
-
if randomize_seed:
|
| 74 |
-
seed = random.randint(0, MAX_SEED)
|
| 75 |
-
|
| 76 |
-
print("[调用API] space_client.predict 输入参数:")
|
| 77 |
-
print(f" images count: {len(images_input)}")
|
| 78 |
-
print(f" prompt: {prompt}")
|
| 79 |
-
print(f" lora_adapter: {lora_adapter}")
|
| 80 |
-
print(f" seed: {seed}")
|
| 81 |
-
print(f" randomize_seed: {randomize_seed}")
|
| 82 |
-
print(f" guidance_scale: {guidance_scale}")
|
| 83 |
-
print(f" steps: {steps}")
|
| 84 |
-
|
| 85 |
-
try:
|
| 86 |
-
# Gradio Space expects list of {"image": {"data": <b64 str>}} elements (see AppError in prompt)
|
| 87 |
-
result = space_client.predict(
|
| 88 |
-
images=images_input,
|
| 89 |
-
prompt=prompt,
|
| 90 |
-
lora_adapter=lora_adapter,
|
| 91 |
-
seed=float(seed),
|
| 92 |
-
randomize_seed=bool(randomize_seed),
|
| 93 |
-
guidance_scale=float(guidance_scale),
|
| 94 |
-
steps=float(steps),
|
| 95 |
-
api_name="/infer",
|
| 96 |
-
)
|
| 97 |
-
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 98 |
-
# result可能不是元组形式,需增加健壮性
|
| 99 |
-
if isinstance(result, dict):
|
| 100 |
-
# 新API只返回dict
|
| 101 |
-
image_info = result
|
| 102 |
-
seed_used = result.get("seed", seed)
|
| 103 |
-
elif isinstance(result, (tuple, list)) and len(result) == 2:
|
| 104 |
-
image_info, seed_used = result
|
| 105 |
-
else:
|
| 106 |
-
print(f"[错误] space_client.predict 返回类型未知: {type(result)},内容: {result}")
|
| 107 |
-
return None, seed
|
| 108 |
-
|
| 109 |
-
# 检查image_info是否包含url/path
|
| 110 |
-
if isinstance(image_info, dict):
|
| 111 |
-
img_url = image_info.get("url") or image_info.get("path") or None
|
| 112 |
-
# 获取base64图片(如果有)
|
| 113 |
-
if img_url is None and "data" in image_info:
|
| 114 |
-
# 返回base64图片(data格式)
|
| 115 |
-
return f"data:image/png;base64,{image_info['data']}", seed_used
|
| 116 |
-
return img_url, seed_used
|
| 117 |
-
else:
|
| 118 |
-
print(f"[错误] image_info 格式异常: {image_info}")
|
| 119 |
-
return None, seed
|
| 120 |
-
except Exception as e:
|
| 121 |
-
import traceback
|
| 122 |
-
traceback.print_exc()
|
| 123 |
-
print(f"[调用API] 调用接口异常: {e}")
|
| 124 |
-
if "Could not process uploaded images" in str(e):
|
| 125 |
-
print(
|
| 126 |
-
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 127 |
-
"可以尝试重新选择图片或修改图片格式。"
|
| 128 |
-
)
|
| 129 |
-
else:
|
| 130 |
-
print("\n[错误] 调用推理服务失败,请稍后重试或联系开发者。")
|
| 131 |
-
return None, seed
|
| 132 |
-
|
| 133 |
-
examples = [
|
| 134 |
-
[None, "Astronaut in jungle, anime style", "Photo-to-Anime", 0, True, 1.0, 4],
|
| 135 |
-
[None, "A delicious ceviche cheesecake slice", "Style-Transfer", 0, True, 1.0, 4],
|
| 136 |
-
]
|
| 137 |
-
|
| 138 |
-
css = """
|
| 139 |
-
#col-container {
|
| 140 |
-
margin: 0 auto;
|
| 141 |
-
max-width: 640px;
|
| 142 |
-
}
|
| 143 |
-
"""
|
| 144 |
-
|
| 145 |
-
with gr.Blocks() as demo:
|
| 146 |
-
with gr.Column(elem_id="col-container"):
|
| 147 |
-
gr.Markdown(" # 图像编辑 API Demo (基于 prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast)")
|
| 148 |
-
|
| 149 |
-
with gr.Row():
|
| 150 |
-
image = gr.Image(
|
| 151 |
-
label="上传图片",
|
| 152 |
-
sources=["upload"],
|
| 153 |
-
type="filepath",
|
| 154 |
-
elem_id="input-image"
|
| 155 |
-
)
|
| 156 |
-
with gr.Row():
|
| 157 |
-
prompt = gr.Text(
|
| 158 |
-
label="编辑描述(Prompt)",
|
| 159 |
-
placeholder="请输入图片编辑描述...",
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
with gr.Row():
|
| 163 |
-
lora_adapter = gr.Dropdown(
|
| 164 |
-
label="编辑风格(Style)",
|
| 165 |
-
choices=LORA_STYLES,
|
| 166 |
-
value="Photo-to-Anime"
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
run_button = gr.Button("执行编辑", scale=1, variant="primary")
|
| 170 |
-
|
| 171 |
-
result = gr.Image(label="结果图片", show_label=True)
|
| 172 |
-
|
| 173 |
-
with gr.Accordion("高级设置", open=False):
|
| 174 |
-
seed = gr.Slider(
|
| 175 |
-
label="随机种子",
|
| 176 |
-
minimum=0,
|
| 177 |
-
maximum=MAX_SEED,
|
| 178 |
-
step=1,
|
| 179 |
-
value=0,
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
randomize_seed = gr.Checkbox(label="随机种子", value=True)
|
| 183 |
-
|
| 184 |
-
guidance_scale = gr.Slider(
|
| 185 |
-
label="引导强度(Guidance Scale)",
|
| 186 |
-
minimum=0.1,
|
| 187 |
-
maximum=10.0,
|
| 188 |
-
step=0.1,
|
| 189 |
-
value=1.0,
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
steps = gr.Slider(
|
| 193 |
-
label="推理步数(Steps)",
|
| 194 |
-
minimum=1,
|
| 195 |
-
maximum=50,
|
| 196 |
-
step=1,
|
| 197 |
-
value=4,
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
# Only show example text/inputs, but avoid file path errors (set image to None)
|
| 201 |
-
gr.Examples(
|
| 202 |
-
examples=examples,
|
| 203 |
-
inputs=[image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 204 |
-
label="示例",
|
| 205 |
-
)
|
| 206 |
-
|
| 207 |
-
gr.on(
|
| 208 |
-
triggers=[run_button.click, prompt.submit],
|
| 209 |
-
fn=infer,
|
| 210 |
-
inputs=[
|
| 211 |
-
image,
|
| 212 |
-
prompt,
|
| 213 |
-
lora_adapter,
|
| 214 |
-
seed,
|
| 215 |
-
randomize_seed,
|
| 216 |
-
guidance_scale,
|
| 217 |
-
steps,
|
| 218 |
-
],
|
| 219 |
-
outputs=[result, seed],
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
if __name__ == "__main__":
|
| 223 |
-
# See: https://prithivmlmods-qwen-image-edit-2511-loras-fast.hf.space
|
| 224 |
-
# To use SSR, remove 'ssr_mode=False' (can help with speed in some settings)
|
| 225 |
-
# To create a public link, set share=True
|
| 226 |
-
demo.launch(css=css, ssr_mode=True, share=True)
|
|
|
|
| 1 |
+
from gradio_client import Client
|
| 2 |
+
client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
| 3 |
+
print(client.view_api())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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