Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from gradio_client import Client
|
| 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")
|
|
@@ -29,15 +30,17 @@ LORA_STYLES = [
|
|
| 29 |
]
|
| 30 |
MAX_SEED = 2**32-1
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
"""
|
| 34 |
-
if not
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
return None
|
| 36 |
-
if os.path.isfile(path):
|
| 37 |
-
return path
|
| 38 |
-
# Try to handle Gradio's nested path structure if needed
|
| 39 |
-
# (Could be adjusted depending how Gradio saves upload images)
|
| 40 |
-
return None
|
| 41 |
|
| 42 |
def infer(
|
| 43 |
image,
|
|
@@ -49,32 +52,32 @@ def infer(
|
|
| 49 |
steps,
|
| 50 |
progress=gr.Progress(track_tqdm=True),
|
| 51 |
):
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
if image is not None:
|
| 55 |
if isinstance(image, list): # Gradio Gallery
|
| 56 |
for im in image:
|
| 57 |
-
|
| 58 |
-
if
|
| 59 |
-
|
| 60 |
else:
|
| 61 |
-
print(f"警告: 路径无效或
|
| 62 |
else:
|
| 63 |
-
|
| 64 |
-
if
|
| 65 |
-
|
| 66 |
else:
|
| 67 |
-
print(f"警告: 路径无效或
|
| 68 |
|
| 69 |
-
if len(
|
| 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: {
|
| 78 |
print(f" prompt: {prompt}")
|
| 79 |
print(f" lora_adapter: {lora_adapter}")
|
| 80 |
print(f" seed: {seed}")
|
|
@@ -83,8 +86,10 @@ def infer(
|
|
| 83 |
print(f" steps: {steps}")
|
| 84 |
|
| 85 |
try:
|
|
|
|
|
|
|
| 86 |
result = space_client.predict(
|
| 87 |
-
images=
|
| 88 |
prompt=prompt,
|
| 89 |
lora_adapter=lora_adapter,
|
| 90 |
seed=float(seed),
|
|
@@ -94,16 +99,13 @@ def infer(
|
|
| 94 |
api_name="/infer",
|
| 95 |
)
|
| 96 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 97 |
-
# result is a tuple: (image_dict, seed)
|
| 98 |
image_info, seed_used = result
|
| 99 |
-
|
| 100 |
-
img_url = image_info.get("url") or image_info.get("path")
|
| 101 |
return img_url, seed_used
|
| 102 |
except Exception as e:
|
| 103 |
import traceback
|
| 104 |
traceback.print_exc()
|
| 105 |
print(f"[调用API] 调用接口异常: {e}")
|
| 106 |
-
# More verbose error message for image-processing errors
|
| 107 |
if hasattr(e, 'message') and "Could not process uploaded images" in str(e):
|
| 108 |
print(
|
| 109 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
+
import base64
|
| 6 |
|
| 7 |
# API client for the external Space
|
| 8 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
|
|
|
| 30 |
]
|
| 31 |
MAX_SEED = 2**32-1
|
| 32 |
|
| 33 |
+
def encode_image_to_base64(image_path):
|
| 34 |
+
"""Read image file and encode as base64 string."""
|
| 35 |
+
if not image_path or not isinstance(image_path, str):
|
| 36 |
+
return None
|
| 37 |
+
try:
|
| 38 |
+
with open(image_path, "rb") as f:
|
| 39 |
+
encoded = base64.b64encode(f.read()).decode("utf-8")
|
| 40 |
+
return encoded
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"无法读取图片或编码失败: {image_path}: {e}")
|
| 43 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def infer(
|
| 46 |
image,
|
|
|
|
| 52 |
steps,
|
| 53 |
progress=gr.Progress(track_tqdm=True),
|
| 54 |
):
|
| 55 |
+
# Process input image(s) as base64 for API (instead of file path)
|
| 56 |
+
images_input = []
|
| 57 |
if image is not None:
|
| 58 |
if isinstance(image, list): # Gradio Gallery
|
| 59 |
for im in image:
|
| 60 |
+
img_b64 = encode_image_to_base64(im)
|
| 61 |
+
if img_b64:
|
| 62 |
+
images_input.append({"image": img_b64, "caption": None})
|
| 63 |
else:
|
| 64 |
+
print(f"警告: 路径无效或无法读取: {im}")
|
| 65 |
else:
|
| 66 |
+
img_b64 = encode_image_to_base64(image)
|
| 67 |
+
if img_b64:
|
| 68 |
+
images_input.append({"image": img_b64, "caption": None})
|
| 69 |
else:
|
| 70 |
+
print(f"警告: 路径无效或无法读取: {image}")
|
| 71 |
|
| 72 |
+
if len(images_input) == 0:
|
| 73 |
+
print("未检测到有效图片,未能上传图片。")
|
| 74 |
return None, seed
|
| 75 |
|
| 76 |
if randomize_seed:
|
| 77 |
seed = random.randint(0, MAX_SEED)
|
| 78 |
|
| 79 |
print("[调用API] space_client.predict 输入参数:")
|
| 80 |
+
print(f" images (base64): {[len(i['image']) for i in images_input]}")
|
| 81 |
print(f" prompt: {prompt}")
|
| 82 |
print(f" lora_adapter: {lora_adapter}")
|
| 83 |
print(f" seed: {seed}")
|
|
|
|
| 86 |
print(f" steps: {steps}")
|
| 87 |
|
| 88 |
try:
|
| 89 |
+
# The API might expect images as list of base64 dicts instead of {path: ...}
|
| 90 |
+
# If error persists, check the API interface definition.
|
| 91 |
result = space_client.predict(
|
| 92 |
+
images=images_input,
|
| 93 |
prompt=prompt,
|
| 94 |
lora_adapter=lora_adapter,
|
| 95 |
seed=float(seed),
|
|
|
|
| 99 |
api_name="/infer",
|
| 100 |
)
|
| 101 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
|
|
|
| 102 |
image_info, seed_used = result
|
| 103 |
+
img_url = image_info.get("url") or image_info.get("path") or None
|
|
|
|
| 104 |
return img_url, seed_used
|
| 105 |
except Exception as e:
|
| 106 |
import traceback
|
| 107 |
traceback.print_exc()
|
| 108 |
print(f"[调用API] 调用接口异常: {e}")
|
|
|
|
| 109 |
if hasattr(e, 'message') and "Could not process uploaded images" in str(e):
|
| 110 |
print(
|
| 111 |
"\n[错误] 上传图片处理失败。请确保图片文件有效且未损坏。\n"
|