Update app.py
Browse files
app.py
CHANGED
|
@@ -28,16 +28,21 @@ LORA_STYLES = [
|
|
| 28 |
'Studio-DeLight',
|
| 29 |
'Cinematic-FlatLog',
|
| 30 |
]
|
| 31 |
-
MAX_SEED = 2**32-1
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
print(f"无法读取图片或编码失败: {image_path}: {e}")
|
| 43 |
return None
|
|
@@ -52,20 +57,20 @@ def infer(
|
|
| 52 |
steps,
|
| 53 |
progress=gr.Progress(track_tqdm=True),
|
| 54 |
):
|
| 55 |
-
# Process input image(s) as
|
| 56 |
images_input = []
|
| 57 |
if image is not None:
|
| 58 |
if isinstance(image, list): # Gradio Gallery
|
| 59 |
for im in image:
|
| 60 |
-
|
| 61 |
-
if
|
| 62 |
-
images_input.append(
|
| 63 |
else:
|
| 64 |
print(f"警告: 路径无效或无法读取: {im}")
|
| 65 |
else:
|
| 66 |
-
|
| 67 |
-
if
|
| 68 |
-
images_input.append(
|
| 69 |
else:
|
| 70 |
print(f"警告: 路径无效或无法读取: {image}")
|
| 71 |
|
|
@@ -77,7 +82,7 @@ def infer(
|
|
| 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,8 +91,7 @@ def infer(
|
|
| 86 |
print(f" steps: {steps}")
|
| 87 |
|
| 88 |
try:
|
| 89 |
-
#
|
| 90 |
-
# If error persists, check the API interface definition.
|
| 91 |
result = space_client.predict(
|
| 92 |
images=images_input,
|
| 93 |
prompt=prompt,
|
|
@@ -100,6 +104,7 @@ def 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:
|
|
@@ -113,7 +118,6 @@ def infer(
|
|
| 113 |
)
|
| 114 |
return None, seed
|
| 115 |
|
| 116 |
-
# For examples, use None for image input to avoid Gradio directory/File errors on startup.
|
| 117 |
examples = [
|
| 118 |
[None, "Astronaut in jungle, anime style", "Photo-to-Anime", 0, True, 1.0, 4],
|
| 119 |
[None, "A delicious ceviche cheesecake slice", "Style-Transfer", 0, True, 1.0, 4],
|
|
|
|
| 28 |
'Studio-DeLight',
|
| 29 |
'Cinematic-FlatLog',
|
| 30 |
]
|
| 31 |
+
MAX_SEED = 2**32 - 1
|
| 32 |
|
| 33 |
+
def encode_image_to_gallery_dict(image_path):
|
| 34 |
+
"""
|
| 35 |
+
Read image file and encode as a dictionary suitable for the Hugging Face Space GalleryData API.
|
| 36 |
+
It must be {"image": {"data": <base64 str>}} for image input.
|
| 37 |
+
"""
|
| 38 |
if not image_path or not isinstance(image_path, str):
|
| 39 |
return None
|
| 40 |
try:
|
| 41 |
with open(image_path, "rb") as f:
|
| 42 |
+
img_bytes = f.read()
|
| 43 |
+
b64_str = base64.b64encode(img_bytes).decode("utf-8")
|
| 44 |
+
# API expects: {"image": {"data": <base64 str>}}
|
| 45 |
+
return {"image": {"data": b64_str}}
|
| 46 |
except Exception as e:
|
| 47 |
print(f"无法读取图片或编码失败: {image_path}: {e}")
|
| 48 |
return None
|
|
|
|
| 57 |
steps,
|
| 58 |
progress=gr.Progress(track_tqdm=True),
|
| 59 |
):
|
| 60 |
+
# Process input image(s) as a list of dict, each of form {"image": {"data": ...}}
|
| 61 |
images_input = []
|
| 62 |
if image is not None:
|
| 63 |
if isinstance(image, list): # Gradio Gallery
|
| 64 |
for im in image:
|
| 65 |
+
img_obj = encode_image_to_gallery_dict(im)
|
| 66 |
+
if img_obj:
|
| 67 |
+
images_input.append(img_obj)
|
| 68 |
else:
|
| 69 |
print(f"警告: 路径无效或无法读取: {im}")
|
| 70 |
else:
|
| 71 |
+
img_obj = encode_image_to_gallery_dict(image)
|
| 72 |
+
if img_obj:
|
| 73 |
+
images_input.append(img_obj)
|
| 74 |
else:
|
| 75 |
print(f"警告: 路径无效或无法读取: {image}")
|
| 76 |
|
|
|
|
| 82 |
seed = random.randint(0, MAX_SEED)
|
| 83 |
|
| 84 |
print("[调用API] space_client.predict 输入参数:")
|
| 85 |
+
print(f" images (base64 bytes): {[len(i['image']['data']) for i in images_input]}")
|
| 86 |
print(f" prompt: {prompt}")
|
| 87 |
print(f" lora_adapter: {lora_adapter}")
|
| 88 |
print(f" seed: {seed}")
|
|
|
|
| 91 |
print(f" steps: {steps}")
|
| 92 |
|
| 93 |
try:
|
| 94 |
+
# Gradio Space expects list of {"image": {"data": <b64 str>}} elements (see AppError in prompt)
|
|
|
|
| 95 |
result = space_client.predict(
|
| 96 |
images=images_input,
|
| 97 |
prompt=prompt,
|
|
|
|
| 104 |
)
|
| 105 |
print(f"[调用API] space_client.predict 返回值: {result}")
|
| 106 |
image_info, seed_used = result
|
| 107 |
+
# API may return url, path, or other keys for the resulting image
|
| 108 |
img_url = image_info.get("url") or image_info.get("path") or None
|
| 109 |
return img_url, seed_used
|
| 110 |
except Exception as e:
|
|
|
|
| 118 |
)
|
| 119 |
return None, seed
|
| 120 |
|
|
|
|
| 121 |
examples = [
|
| 122 |
[None, "Astronaut in jungle, anime style", "Photo-to-Anime", 0, True, 1.0, 4],
|
| 123 |
[None, "A delicious ceviche cheesecake slice", "Style-Transfer", 0, True, 1.0, 4],
|