Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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")
|
|
@@ -28,6 +29,16 @@ LORA_STYLES = [
|
|
| 28 |
]
|
| 29 |
MAX_SEED = 2**32-1
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def infer(
|
| 32 |
image,
|
| 33 |
prompt,
|
|
@@ -43,14 +54,25 @@ def infer(
|
|
| 43 |
if image is not None:
|
| 44 |
if isinstance(image, list): # Gradio Gallery
|
| 45 |
for im in image:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
else:
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if randomize_seed:
|
| 51 |
seed = random.randint(0, MAX_SEED)
|
| 52 |
|
| 53 |
-
# Print API call log
|
| 54 |
print("[调用API] space_client.predict 输入参数:")
|
| 55 |
print(f" images: {images}")
|
| 56 |
print(f" prompt: {prompt}")
|
|
@@ -59,6 +81,7 @@ def infer(
|
|
| 59 |
print(f" randomize_seed: {randomize_seed}")
|
| 60 |
print(f" guidance_scale: {guidance_scale}")
|
| 61 |
print(f" steps: {steps}")
|
|
|
|
| 62 |
try:
|
| 63 |
result = space_client.predict(
|
| 64 |
images=images,
|
|
@@ -80,6 +103,12 @@ def infer(
|
|
| 80 |
import traceback
|
| 81 |
traceback.print_exc()
|
| 82 |
print(f"[调用API] 调用接口异常: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return None, seed
|
| 84 |
|
| 85 |
# For examples, use None for image input to avoid Gradio directory/File errors on startup.
|
|
@@ -173,4 +202,7 @@ with gr.Blocks() as demo:
|
|
| 173 |
)
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
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 |
]
|
| 30 |
MAX_SEED = 2**32-1
|
| 31 |
|
| 32 |
+
def safe_image_path(path):
|
| 33 |
+
"""Ensure the given path points to an existing, readable file."""
|
| 34 |
+
if not path or not isinstance(path, str):
|
| 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,
|
| 44 |
prompt,
|
|
|
|
| 54 |
if image is not None:
|
| 55 |
if isinstance(image, list): # Gradio Gallery
|
| 56 |
for im in image:
|
| 57 |
+
file_path = safe_image_path(im)
|
| 58 |
+
if file_path:
|
| 59 |
+
images.append({"image": {"path": file_path}, "caption": None})
|
| 60 |
+
else:
|
| 61 |
+
print(f"警告: 路径无效或找不到文件: {im}")
|
| 62 |
else:
|
| 63 |
+
file_path = safe_image_path(image)
|
| 64 |
+
if file_path:
|
| 65 |
+
images.append({"image": {"path": file_path}, "caption": None})
|
| 66 |
+
else:
|
| 67 |
+
print(f"警告: 路径无效或找不到文件: {image}")
|
| 68 |
+
|
| 69 |
+
if len(images) == 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: {images}")
|
| 78 |
print(f" prompt: {prompt}")
|
|
|
|
| 81 |
print(f" randomize_seed: {randomize_seed}")
|
| 82 |
print(f" guidance_scale: {guidance_scale}")
|
| 83 |
print(f" steps: {steps}")
|
| 84 |
+
|
| 85 |
try:
|
| 86 |
result = space_client.predict(
|
| 87 |
images=images,
|
|
|
|
| 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"
|
| 110 |
+
"可以尝试重新选择图片或修改图片格式。"
|
| 111 |
+
)
|
| 112 |
return None, seed
|
| 113 |
|
| 114 |
# For examples, use None for image input to avoid Gradio directory/File errors on startup.
|
|
|
|
| 202 |
)
|
| 203 |
|
| 204 |
if __name__ == "__main__":
|
| 205 |
+
# See: https://prithivmlmods-qwen-image-edit-2511-loras-fast.hf.space
|
| 206 |
+
# To use SSR, remove 'ssr_mode=False' (can help with speed in some settings)
|
| 207 |
+
# To create a public link, set share=True
|
| 208 |
+
demo.launch(css=css, ssr_mode=True, share=True)
|