kenfoo commited on
Commit
2fa6a20
·
verified ·
1 Parent(s): 4b26e47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -2
app.py CHANGED
@@ -1,3 +1,193 @@
 
1
  from gradio_client import Client
2
- client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
3
- print(client.view_api())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ 'Photo-to-Anime',
12
+ 'Anime-V2',
13
+ 'Light-Migration',
14
+ 'Upscaler',
15
+ 'Style-Transfer',
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
+ """把本地文件路径转成 Gallery 期望的 dict 格式"""
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": image_path,
44
+ "url": None,
45
+ "size": os.path.getsize(image_path),
46
+ "orig_name": os.path.basename(image_path),
47
+ "mime_type": None,
48
+ "is_stream": False,
49
+ "meta": {}
50
+ },
51
+ "caption": None
52
+ }
53
+
54
+
55
+ def infer(
56
+ image,
57
+ prompt,
58
+ lora_adapter,
59
+ seed,
60
+ randomize_seed,
61
+ guidance_scale,
62
+ steps,
63
+ progress=gr.Progress(track_tqdm=True),
64
+ ):
65
+ images_input = []
66
+
67
+ if image is not None:
68
+ if isinstance(image, list):
69
+ for im in image:
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("未检测到有效图片,请上传图片后再试。")
84
+ return None, seed
85
+
86
+ if randomize_seed:
87
+ seed = random.randint(0, MAX_SEED)
88
+
89
+ print("[调用API] 输入参数:")
90
+ print(f" images count: {len(images_input)}")
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
+
98
+ try:
99
+ result = space_client.predict(
100
+ images=images_input,
101
+ prompt=prompt,
102
+ lora_adapter=lora_adapter,
103
+ seed=float(seed),
104
+ randomize_seed=bool(randomize_seed),
105
+ guidance_scale=float(guidance_scale),
106
+ steps=float(steps),
107
+ api_name="/infer",
108
+ )
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):
116
+ img_out = image_info.get("path") or image_info.get("url")
117
+ return img_out, int(seed_used)
118
+ else:
119
+ return image_info, int(seed_used)
120
+
121
+ except Exception as e:
122
+ import traceback
123
+ traceback.print_exc()
124
+ print(f"[调用API] 调用接口异常: {e}")
125
+ return None, seed
126
+
127
+
128
+ css = """
129
+ #col-container {
130
+ margin: 0 auto;
131
+ max-width: 640px;
132
+ }
133
+ """
134
+
135
+ with gr.Blocks(css=css) as demo:
136
+ with gr.Column(elem_id="col-container"):
137
+ gr.Markdown("# 图像编辑 Demo\n基于 prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
138
+
139
+ image = gr.Image(
140
+ label="上传图片",
141
+ sources=["upload"],
142
+ type="filepath",
143
+ elem_id="input-image"
144
+ )
145
+
146
+ prompt = gr.Text(
147
+ label="编辑描述(Prompt)",
148
+ placeholder="请输入图片编辑描述...",
149
+ )
150
+
151
+ lora_adapter = gr.Dropdown(
152
+ label="编辑风格(Style)",
153
+ choices=LORA_STYLES,
154
+ value="Photo-to-Anime"
155
+ )
156
+
157
+ run_button = gr.Button("执行编辑", variant="primary")
158
+
159
+ result = gr.Image(label="结果图片", show_label=True)
160
+
161
+ with gr.Accordion("高级设置", open=False):
162
+ seed = gr.Slider(
163
+ label="随机种子",
164
+ minimum=0,
165
+ maximum=MAX_SEED,
166
+ step=1,
167
+ value=0,
168
+ )
169
+ randomize_seed = gr.Checkbox(label="随机化种子", value=True)
170
+ guidance_scale = gr.Slider(
171
+ label="引导强度 (Guidance Scale)",
172
+ minimum=0.1,
173
+ maximum=10.0,
174
+ step=0.1,
175
+ value=1.0,
176
+ )
177
+ steps = gr.Slider(
178
+ label="推理步数 (Steps)",
179
+ minimum=1,
180
+ maximum=50,
181
+ step=1,
182
+ value=4,
183
+ )
184
+
185
+ gr.on(
186
+ triggers=[run_button.click, prompt.submit],
187
+ fn=infer,
188
+ inputs=[image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
189
+ outputs=[result, seed],
190
+ )
191
+
192
+ if __name__ == "__main__":
193
+ demo.launch(ssr_mode=False, share=True)