Update app.py
Browse files
app.py
CHANGED
|
@@ -2,49 +2,46 @@ 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")
|
| 8 |
|
| 9 |
LORA_STYLES = [
|
| 10 |
-
'Multiple-Angles',
|
| 11 |
-
'
|
| 12 |
-
'
|
| 13 |
-
'
|
| 14 |
-
'
|
| 15 |
-
'
|
| 16 |
-
'Manga-Tone',
|
| 17 |
-
'Anything2Real',
|
| 18 |
-
'Fal-Multiple-Angles',
|
| 19 |
-
'Polaroid-Photo',
|
| 20 |
-
'Unblur-Anything',
|
| 21 |
-
'Midnight-Noir-Eyes-Spotlight',
|
| 22 |
-
'Hyper-Realistic-Portrait',
|
| 23 |
-
'Ultra-Realistic-Portrait',
|
| 24 |
-
'Pixar-Inspired-3D',
|
| 25 |
-
'Noir-Comic-Book',
|
| 26 |
-
'Any-light',
|
| 27 |
-
'Studio-DeLight',
|
| 28 |
-
'Cinematic-FlatLog',
|
| 29 |
]
|
| 30 |
MAX_SEED = 2**31 - 1
|
| 31 |
|
| 32 |
|
| 33 |
def encode_image_for_gallery(image_path):
|
| 34 |
-
"""
|
| 35 |
if not image_path or not isinstance(image_path, str):
|
| 36 |
return None
|
| 37 |
if not os.path.exists(image_path):
|
| 38 |
print(f"文件不存在: {image_path}")
|
| 39 |
return None
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return {
|
| 42 |
"image": {
|
| 43 |
-
"path":
|
| 44 |
-
"url":
|
| 45 |
"size": os.path.getsize(image_path),
|
| 46 |
"orig_name": os.path.basename(image_path),
|
| 47 |
-
"mime_type":
|
| 48 |
"is_stream": False,
|
| 49 |
"meta": {}
|
| 50 |
},
|
|
@@ -70,14 +67,10 @@ def infer(
|
|
| 70 |
obj = encode_image_for_gallery(im)
|
| 71 |
if obj:
|
| 72 |
images_input.append(obj)
|
| 73 |
-
else:
|
| 74 |
-
print(f"警告: 路径无效或无法读取: {im}")
|
| 75 |
else:
|
| 76 |
obj = encode_image_for_gallery(image)
|
| 77 |
if obj:
|
| 78 |
images_input.append(obj)
|
| 79 |
-
else:
|
| 80 |
-
print(f"警告: 路径无效或无法读取: {image}")
|
| 81 |
|
| 82 |
if len(images_input) == 0:
|
| 83 |
print("未检测到有效图片,请上传图片后再试。")
|
|
@@ -91,7 +84,6 @@ def infer(
|
|
| 91 |
print(f" prompt: {prompt}")
|
| 92 |
print(f" lora_adapter: {lora_adapter}")
|
| 93 |
print(f" seed: {seed}")
|
| 94 |
-
print(f" randomize_seed: {randomize_seed}")
|
| 95 |
print(f" guidance_scale: {guidance_scale}")
|
| 96 |
print(f" steps: {steps}")
|
| 97 |
|
|
@@ -109,7 +101,6 @@ def infer(
|
|
| 109 |
|
| 110 |
print(f"[调用API] 返回值: {result}")
|
| 111 |
|
| 112 |
-
# result 是 (image_dict, seed_float) 元组
|
| 113 |
image_info, seed_used = result
|
| 114 |
|
| 115 |
if isinstance(image_info, dict):
|
|
@@ -140,7 +131,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 140 |
label="上传图片",
|
| 141 |
sources=["upload"],
|
| 142 |
type="filepath",
|
| 143 |
-
elem_id="input-image"
|
| 144 |
)
|
| 145 |
|
| 146 |
prompt = gr.Text(
|
|
|
|
| 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 |
|
| 10 |
LORA_STYLES = [
|
| 11 |
+
'Multiple-Angles', 'Photo-to-Anime', 'Anime-V2', 'Light-Migration',
|
| 12 |
+
'Upscaler', 'Style-Transfer', 'Manga-Tone', 'Anything2Real',
|
| 13 |
+
'Fal-Multiple-Angles', 'Polaroid-Photo', 'Unblur-Anything',
|
| 14 |
+
'Midnight-Noir-Eyes-Spotlight', 'Hyper-Realistic-Portrait',
|
| 15 |
+
'Ultra-Realistic-Portrait', 'Pixar-Inspired-3D', 'Noir-Comic-Book',
|
| 16 |
+
'Any-light', 'Studio-DeLight', 'Cinematic-FlatLog',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
]
|
| 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 |
},
|
|
|
|
| 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 len(images_input) == 0:
|
| 76 |
print("未检测到有效图片,请上传图片后再试。")
|
|
|
|
| 84 |
print(f" prompt: {prompt}")
|
| 85 |
print(f" lora_adapter: {lora_adapter}")
|
| 86 |
print(f" seed: {seed}")
|
|
|
|
| 87 |
print(f" guidance_scale: {guidance_scale}")
|
| 88 |
print(f" steps: {steps}")
|
| 89 |
|
|
|
|
| 101 |
|
| 102 |
print(f"[调用API] 返回值: {result}")
|
| 103 |
|
|
|
|
| 104 |
image_info, seed_used = result
|
| 105 |
|
| 106 |
if isinstance(image_info, dict):
|
|
|
|
| 131 |
label="上传图片",
|
| 132 |
sources=["upload"],
|
| 133 |
type="filepath",
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
prompt = gr.Text(
|