Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from gradio_client import Client
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
-
import base64
|
| 6 |
-
import mimetypes
|
| 7 |
|
| 8 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
| 9 |
|
|
@@ -18,37 +16,6 @@ LORA_STYLES = [
|
|
| 18 |
MAX_SEED = 2**31 - 1
|
| 19 |
|
| 20 |
|
| 21 |
-
def encode_image_for_gallery(image_path):
|
| 22 |
-
"""将图片转为 base64 data URL,嵌入 url 字段传给远端"""
|
| 23 |
-
if not image_path or not isinstance(image_path, str):
|
| 24 |
-
return None
|
| 25 |
-
if not os.path.exists(image_path):
|
| 26 |
-
print(f"文件不存在: {image_path}")
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
mime_type, _ = mimetypes.guess_type(image_path)
|
| 30 |
-
if not mime_type:
|
| 31 |
-
mime_type = "image/png"
|
| 32 |
-
|
| 33 |
-
with open(image_path, "rb") as f:
|
| 34 |
-
b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 35 |
-
|
| 36 |
-
data_url = f"data:{mime_type};base64,{b64}"
|
| 37 |
-
|
| 38 |
-
return {
|
| 39 |
-
"image": {
|
| 40 |
-
"path": None,
|
| 41 |
-
"url": data_url, # base64 嵌入这里
|
| 42 |
-
"size": os.path.getsize(image_path),
|
| 43 |
-
"orig_name": os.path.basename(image_path),
|
| 44 |
-
"mime_type": mime_type,
|
| 45 |
-
"is_stream": False,
|
| 46 |
-
"meta": {}
|
| 47 |
-
},
|
| 48 |
-
"caption": None
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
def infer(
|
| 53 |
image,
|
| 54 |
prompt,
|
|
@@ -59,28 +26,26 @@ def infer(
|
|
| 59 |
steps,
|
| 60 |
progress=gr.Progress(track_tqdm=True),
|
| 61 |
):
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if isinstance(image, list):
|
| 66 |
-
for im in image:
|
| 67 |
-
obj = encode_image_for_gallery(im)
|
| 68 |
-
if obj:
|
| 69 |
-
images_input.append(obj)
|
| 70 |
-
else:
|
| 71 |
-
obj = encode_image_for_gallery(image)
|
| 72 |
-
if obj:
|
| 73 |
-
images_input.append(obj)
|
| 74 |
|
| 75 |
-
if
|
| 76 |
-
print("
|
| 77 |
return None, seed
|
| 78 |
|
| 79 |
if randomize_seed:
|
| 80 |
seed = random.randint(0, MAX_SEED)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
print("[调用API] 输入参数:")
|
| 83 |
-
print(f"
|
|
|
|
| 84 |
print(f" prompt: {prompt}")
|
| 85 |
print(f" lora_adapter: {lora_adapter}")
|
| 86 |
print(f" seed: {seed}")
|
|
@@ -105,14 +70,15 @@ def infer(
|
|
| 105 |
|
| 106 |
if isinstance(image_info, dict):
|
| 107 |
img_out = image_info.get("path") or image_info.get("url")
|
| 108 |
-
return img_out, int(seed_used)
|
| 109 |
else:
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
import traceback
|
| 114 |
traceback.print_exc()
|
| 115 |
-
print(f"[调用API]
|
| 116 |
return None, seed
|
| 117 |
|
| 118 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
import random
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
| 7 |
|
|
|
|
| 16 |
MAX_SEED = 2**31 - 1
|
| 17 |
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def infer(
|
| 20 |
image,
|
| 21 |
prompt,
|
|
|
|
| 26 |
steps,
|
| 27 |
progress=gr.Progress(track_tqdm=True),
|
| 28 |
):
|
| 29 |
+
if image is None:
|
| 30 |
+
print("未上传图片")
|
| 31 |
+
return None, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
if not os.path.exists(image):
|
| 34 |
+
print(f"图片路径不存在: {image}")
|
| 35 |
return None, seed
|
| 36 |
|
| 37 |
if randomize_seed:
|
| 38 |
seed = random.randint(0, MAX_SEED)
|
| 39 |
|
| 40 |
+
# 关键:用 handle_file 上传到目标 Space,得到远端可访问的文件对象
|
| 41 |
+
uploaded = handle_file(image)
|
| 42 |
+
|
| 43 |
+
# Gallery 元素格式:{"image": <上传后的文件对象>, "caption": None}
|
| 44 |
+
images_input = [{"image": uploaded, "caption": None}]
|
| 45 |
+
|
| 46 |
print("[调用API] 输入参数:")
|
| 47 |
+
print(f" image path: {image}")
|
| 48 |
+
print(f" uploaded: {uploaded}")
|
| 49 |
print(f" prompt: {prompt}")
|
| 50 |
print(f" lora_adapter: {lora_adapter}")
|
| 51 |
print(f" seed: {seed}")
|
|
|
|
| 70 |
|
| 71 |
if isinstance(image_info, dict):
|
| 72 |
img_out = image_info.get("path") or image_info.get("url")
|
|
|
|
| 73 |
else:
|
| 74 |
+
img_out = image_info
|
| 75 |
+
|
| 76 |
+
return img_out, int(seed_used)
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
import traceback
|
| 80 |
traceback.print_exc()
|
| 81 |
+
print(f"[调用API] 异常: {e}")
|
| 82 |
return None, seed
|
| 83 |
|
| 84 |
|