Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,62 @@
|
|
| 1 |
-
import
|
| 2 |
-
from gradio_client import Client, handle_file
|
| 3 |
-
import random
|
| 4 |
import os
|
| 5 |
|
| 6 |
HF_TOKEN = os.environ.get("girlToken")
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 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 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
try:
|
| 61 |
result = space_client.predict(
|
|
@@ -69,86 +70,12 @@ def infer(
|
|
| 69 |
api_name="/infer",
|
| 70 |
)
|
| 71 |
|
| 72 |
-
print(f"[调用API] 返回值: {result}")
|
| 73 |
-
|
| 74 |
image_info, seed_used = result
|
| 75 |
-
|
| 76 |
-
if isinstance(image_info, dict):
|
| 77 |
-
img_out = image_info.get("path") or image_info.get("url")
|
| 78 |
-
else:
|
| 79 |
-
img_out = image_info
|
| 80 |
-
|
| 81 |
return img_out, int(seed_used)
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
import traceback
|
| 85 |
traceback.print_exc()
|
| 86 |
print(f"[调用API] 异常: {e}")
|
| 87 |
-
return None, seed
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
css = """
|
| 91 |
-
#col-container {
|
| 92 |
-
margin: 0 auto;
|
| 93 |
-
max-width: 640px;
|
| 94 |
-
}
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
-
with gr.Blocks(css=css) as demo:
|
| 98 |
-
with gr.Column(elem_id="col-container"):
|
| 99 |
-
gr.Markdown("# 图像编辑 Demo\n基于 prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
|
| 100 |
-
|
| 101 |
-
image = gr.Image(
|
| 102 |
-
label="上传图片",
|
| 103 |
-
sources=["upload"],
|
| 104 |
-
type="filepath",
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
prompt = gr.Text(
|
| 108 |
-
label="编辑描述(Prompt)",
|
| 109 |
-
placeholder="请输入图片编辑描述...",
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
lora_adapter = gr.Dropdown(
|
| 113 |
-
label="编辑风格(Style)",
|
| 114 |
-
choices=LORA_STYLES,
|
| 115 |
-
value="Photo-to-Anime"
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
run_button = gr.Button("执行编辑", variant="primary")
|
| 119 |
-
|
| 120 |
-
result = gr.Image(label="结果图片", show_label=True)
|
| 121 |
-
|
| 122 |
-
with gr.Accordion("高级设置", open=False):
|
| 123 |
-
seed = gr.Slider(
|
| 124 |
-
label="随机种子",
|
| 125 |
-
minimum=0,
|
| 126 |
-
maximum=MAX_SEED,
|
| 127 |
-
step=1,
|
| 128 |
-
value=0,
|
| 129 |
-
)
|
| 130 |
-
randomize_seed = gr.Checkbox(label="随机化种子", value=True)
|
| 131 |
-
guidance_scale = gr.Slider(
|
| 132 |
-
label="引导强度 (Guidance Scale)",
|
| 133 |
-
minimum=0.1,
|
| 134 |
-
maximum=10.0,
|
| 135 |
-
step=0.1,
|
| 136 |
-
value=1.0,
|
| 137 |
-
)
|
| 138 |
-
steps = gr.Slider(
|
| 139 |
-
label="推理步数 (Steps)",
|
| 140 |
-
minimum=1,
|
| 141 |
-
maximum=50,
|
| 142 |
-
step=1,
|
| 143 |
-
value=4,
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
gr.on(
|
| 147 |
-
triggers=[run_button.click, prompt.submit],
|
| 148 |
-
fn=infer,
|
| 149 |
-
inputs=[image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
if __name__ == "__main__":
|
| 154 |
-
demo.launch(ssr_mode=False, share=True)
|
|
|
|
| 1 |
+
import requests
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
HF_TOKEN = os.environ.get("girlToken")
|
| 5 |
+
TARGET_SPACE_URL = "https://prithivmlmods-qwen-image-edit-2511-loras-fast.hf.space"
|
| 6 |
+
|
| 7 |
+
def upload_file_to_space(local_path):
|
| 8 |
+
"""手动上传文件到目标 Space,返回远端 URL"""
|
| 9 |
+
upload_url = f"{TARGET_SPACE_URL}/upload"
|
| 10 |
+
|
| 11 |
+
headers = {}
|
| 12 |
+
if HF_TOKEN:
|
| 13 |
+
headers["Authorization"] = f"Bearer {HF_TOKEN}"
|
| 14 |
+
|
| 15 |
+
with open(local_path, "rb") as f:
|
| 16 |
+
response = requests.post(
|
| 17 |
+
upload_url,
|
| 18 |
+
headers=headers,
|
| 19 |
+
files={"files": (os.path.basename(local_path), f, "image/jpeg")},
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
print(f"上传状态码: {response.status_code}")
|
| 23 |
+
print(f"上传响应: {response.text}")
|
| 24 |
+
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
result = response.json()
|
| 27 |
+
# 返回的是文件路径列表,取第一个
|
| 28 |
+
remote_path = result[0] if isinstance(result, list) else result
|
| 29 |
+
return remote_path
|
| 30 |
+
else:
|
| 31 |
+
raise Exception(f"上传失败: {response.status_code} {response.text}")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def infer(image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps, progress=gr.Progress(track_tqdm=True)):
|
| 35 |
+
if image is None or not os.path.exists(image):
|
|
|
|
|
|
|
| 36 |
return None, seed
|
| 37 |
|
| 38 |
if randomize_seed:
|
| 39 |
seed = random.randint(0, MAX_SEED)
|
| 40 |
|
| 41 |
+
# 手动上传,拿到远端路径
|
| 42 |
+
remote_path = upload_file_to_space(image)
|
| 43 |
+
print(f"远端路径: {remote_path}")
|
| 44 |
+
|
| 45 |
+
# 用远端路径构造 images 参数
|
| 46 |
+
images_input = [{
|
| 47 |
+
"image": {
|
| 48 |
+
"path": remote_path,
|
| 49 |
+
"url": f"{TARGET_SPACE_URL}/file={remote_path}",
|
| 50 |
+
"size": os.path.getsize(image),
|
| 51 |
+
"orig_name": os.path.basename(image),
|
| 52 |
+
"mime_type": "image/jpeg",
|
| 53 |
+
"is_stream": False,
|
| 54 |
+
"meta": {}
|
| 55 |
+
},
|
| 56 |
+
"caption": None
|
| 57 |
+
}]
|
| 58 |
+
|
| 59 |
+
print(f"images_input: {images_input}")
|
| 60 |
|
| 61 |
try:
|
| 62 |
result = space_client.predict(
|
|
|
|
| 70 |
api_name="/infer",
|
| 71 |
)
|
| 72 |
|
|
|
|
|
|
|
| 73 |
image_info, seed_used = result
|
| 74 |
+
img_out = image_info.get("path") or image_info.get("url")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return img_out, int(seed_used)
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
import traceback
|
| 79 |
traceback.print_exc()
|
| 80 |
print(f"[调用API] 异常: {e}")
|
| 81 |
+
return None, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|