Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
# API client for the external Space
|
| 8 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
|
@@ -31,20 +30,11 @@ LORA_STYLES = [
|
|
| 31 |
MAX_SEED = 2**31 - 1
|
| 32 |
|
| 33 |
def encode_image_to_gallery_dict(image_path):
|
| 34 |
-
|
| 35 |
-
Returns the dict structure required for GalleryData, containing "image" (binary data) and "orig_name".
|
| 36 |
-
This directly loads the image and provides the binary data instead of a path, fixing the GalleryData validation error.
|
| 37 |
-
"""
|
| 38 |
-
if not image_path or not isinstance(image_path, str) or not os.path.isfile(image_path):
|
| 39 |
return None
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
image_bytes = f.read()
|
| 44 |
-
return {
|
| 45 |
-
"image": image_bytes,
|
| 46 |
-
"orig_name": os.path.basename(image_path)
|
| 47 |
-
}
|
| 48 |
except Exception as e:
|
| 49 |
print(f"无法读取图片: {image_path}: {e}")
|
| 50 |
return None
|
|
@@ -59,10 +49,10 @@ def infer(
|
|
| 59 |
steps,
|
| 60 |
progress=gr.Progress(track_tqdm=True),
|
| 61 |
):
|
| 62 |
-
#
|
| 63 |
images_input = []
|
| 64 |
if image is not None:
|
| 65 |
-
if isinstance(image, list):
|
| 66 |
for im in image:
|
| 67 |
img_obj = encode_image_to_gallery_dict(im)
|
| 68 |
if img_obj:
|
|
@@ -85,7 +75,6 @@ def infer(
|
|
| 85 |
|
| 86 |
print("[调用API] space_client.predict 输入参数:")
|
| 87 |
print(f" images count: {len(images_input)}")
|
| 88 |
-
print(f" images structure: {images_input}")
|
| 89 |
print(f" prompt: {prompt}")
|
| 90 |
print(f" lora_adapter: {lora_adapter}")
|
| 91 |
print(f" seed: {seed}")
|
|
@@ -94,7 +83,7 @@ def infer(
|
|
| 94 |
print(f" steps: {steps}")
|
| 95 |
|
| 96 |
try:
|
| 97 |
-
#
|
| 98 |
result = space_client.predict(
|
| 99 |
images=images_input,
|
| 100 |
prompt=prompt,
|
|
@@ -108,6 +97,7 @@ def infer(
|
|
| 108 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 109 |
# result可能不是元组形式,需增加健壮性
|
| 110 |
if isinstance(result, dict):
|
|
|
|
| 111 |
image_info = result
|
| 112 |
seed_used = result.get("seed", seed)
|
| 113 |
elif isinstance(result, (tuple, list)) and len(result) == 2:
|
|
@@ -116,9 +106,12 @@ def infer(
|
|
| 116 |
print(f"[错误] space_client.predict 返回类型未知: {type(result)},内容: {result}")
|
| 117 |
return None, seed
|
| 118 |
|
|
|
|
| 119 |
if isinstance(image_info, dict):
|
| 120 |
img_url = image_info.get("url") or image_info.get("path") or None
|
|
|
|
| 121 |
if img_url is None and "data" in image_info:
|
|
|
|
| 122 |
return f"data:image/png;base64,{image_info['data']}", seed_used
|
| 123 |
return img_url, seed_used
|
| 124 |
else:
|
|
@@ -133,11 +126,6 @@ def infer(
|
|
| 133 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 134 |
"可以尝试重新选择图片或修改图片格式。"
|
| 135 |
)
|
| 136 |
-
elif "validation errors for GalleryData" in str(e):
|
| 137 |
-
print(
|
| 138 |
-
"\n[错误] 图片数据上传格式错误。请检查图片输入的数据结构,"
|
| 139 |
-
"确保为文件路径字符串或有效图片文件。"
|
| 140 |
-
)
|
| 141 |
else:
|
| 142 |
print("\n[错误] 调用推理服务失败,请稍后重试或联系开发者。")
|
| 143 |
return None, seed
|
|
|
|
| 2 |
from gradio_client import Client, handle_file
|
| 3 |
import random
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# API client for the external Space
|
| 7 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
|
|
|
| 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
|
|
|
|
| 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:
|
|
|
|
| 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}")
|
|
|
|
| 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,
|
|
|
|
| 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:
|
|
|
|
| 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:
|
|
|
|
| 126 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
| 127 |
"可以尝试重新选择图片或修改图片格式。"
|
| 128 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
else:
|
| 130 |
print("\n[错误] 调用推理服务失败,请稍后重试或联系开发者。")
|
| 131 |
return None, seed
|