kenfoo commited on
Commit
96d0dd4
·
verified ·
1 Parent(s): de9bd64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -26
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 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,
@@ -49,32 +52,32 @@ def infer(
49
  steps,
50
  progress=gr.Progress(track_tqdm=True),
51
  ):
52
- # Prepare images input as per API (expects Gallery [list of dicts])
53
- images = []
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}")
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=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
- # The API may return image at .url or .path, we use .url if available
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"