Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 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 |
HF_TOKEN = os.environ.get("girlToken")
|
| 9 |
|
| 10 |
space_client = Client(
|
| 11 |
"prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast",
|
| 12 |
token=HF_TOKEN
|
| 13 |
-
)
|
| 14 |
|
| 15 |
LORA_STYLES = [
|
| 16 |
'Multiple-Angles', 'Photo-to-Anime', 'Anime-V2', 'Light-Migration',
|
|
@@ -23,30 +21,6 @@ LORA_STYLES = [
|
|
| 23 |
MAX_SEED = 2**31 - 1
|
| 24 |
|
| 25 |
|
| 26 |
-
def image_to_gallery_item(local_path):
|
| 27 |
-
"""按照 API 定义,把本地图片转成 Gallery 元素"""
|
| 28 |
-
mime_type, _ = mimetypes.guess_type(local_path)
|
| 29 |
-
if not mime_type:
|
| 30 |
-
mime_type = "image/jpeg"
|
| 31 |
-
|
| 32 |
-
with open(local_path, "rb") as f:
|
| 33 |
-
b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 34 |
-
|
| 35 |
-
# url 字段明确支持 base64 encoded image
|
| 36 |
-
return {
|
| 37 |
-
"image": {
|
| 38 |
-
"path": None,
|
| 39 |
-
"url": f"data:{mime_type};base64,{b64}",
|
| 40 |
-
"size": os.path.getsize(local_path),
|
| 41 |
-
"orig_name": os.path.basename(local_path),
|
| 42 |
-
"mime_type": mime_type,
|
| 43 |
-
"is_stream": False,
|
| 44 |
-
"meta": {"_type": "gradio.FileData"}
|
| 45 |
-
},
|
| 46 |
-
"caption": None
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
def infer(
|
| 51 |
image,
|
| 52 |
prompt,
|
|
@@ -57,18 +31,26 @@ def infer(
|
|
| 57 |
steps,
|
| 58 |
progress=gr.Progress(track_tqdm=True),
|
| 59 |
):
|
| 60 |
-
if image is None
|
| 61 |
-
print("未上传图片
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return None, seed
|
| 63 |
|
| 64 |
if randomize_seed:
|
| 65 |
seed = random.randint(0, MAX_SEED)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
print("[调用API] 输入参数:")
|
| 70 |
-
print(f"
|
| 71 |
-
print(f"
|
| 72 |
print(f" prompt: {prompt}")
|
| 73 |
print(f" lora_adapter: {lora_adapter}")
|
| 74 |
print(f" seed: {seed}")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
import random
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
HF_TOKEN = os.environ.get("girlToken")
|
| 7 |
|
| 8 |
space_client = Client(
|
| 9 |
"prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast",
|
| 10 |
token=HF_TOKEN
|
| 11 |
+
)
|
| 12 |
|
| 13 |
LORA_STYLES = [
|
| 14 |
'Multiple-Angles', 'Photo-to-Anime', 'Anime-V2', 'Light-Migration',
|
|
|
|
| 21 |
MAX_SEED = 2**31 - 1
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def infer(
|
| 25 |
image,
|
| 26 |
prompt,
|
|
|
|
| 31 |
steps,
|
| 32 |
progress=gr.Progress(track_tqdm=True),
|
| 33 |
):
|
| 34 |
+
if image is None:
|
| 35 |
+
print("未上传图片")
|
| 36 |
+
return None, seed
|
| 37 |
+
|
| 38 |
+
if not os.path.exists(image):
|
| 39 |
+
print(f"图片路径不存在: {image}")
|
| 40 |
return None, seed
|
| 41 |
|
| 42 |
if randomize_seed:
|
| 43 |
seed = random.randint(0, MAX_SEED)
|
| 44 |
|
| 45 |
+
# 关键:用 handle_file 上传到目标 Space,得到远端可访问的文件对象
|
| 46 |
+
uploaded = handle_file(image)
|
| 47 |
+
|
| 48 |
+
# Gallery 元素格式:{"image": <上传后的文件对象>, "caption": None}
|
| 49 |
+
images_input = [{"image": uploaded, "caption": None}]
|
| 50 |
|
| 51 |
print("[调用API] 输入参数:")
|
| 52 |
+
print(f" image path: {image}")
|
| 53 |
+
print(f" uploaded: {uploaded}")
|
| 54 |
print(f" prompt: {prompt}")
|
| 55 |
print(f" lora_adapter: {lora_adapter}")
|
| 56 |
print(f" seed: {seed}")
|