kenfoo commited on
Commit
8688650
·
verified ·
1 Parent(s): 61da7bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -65
app.py CHANGED
@@ -1,14 +1,16 @@
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,6 +23,23 @@ LORA_STYLES = [
21
  MAX_SEED = 2**31 - 1
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def infer(
25
  image,
26
  prompt,
@@ -31,31 +50,25 @@ def infer(
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}")
57
- print(f" guidance_scale: {guidance_scale}")
58
- print(f" steps: {steps}")
59
 
60
  try:
61
  result = space_client.predict(
@@ -69,21 +82,18 @@ 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
 
@@ -98,50 +108,17 @@ 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],
@@ -151,4 +128,11 @@ with gr.Blocks(css=css) as demo:
151
  )
152
 
153
  if __name__ == "__main__":
154
- demo.launch(ssr_mode=False, share=True)
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from gradio_client import Client
3
  import random
4
  import os
5
+ import requests
6
+ import tempfile
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
  MAX_SEED = 2**31 - 1
24
 
25
 
26
+ def upload_to_imgbb(image_path):
27
+ """上传图片到免费图床,返回公开 URL"""
28
+ IMGBB_API_KEY = os.environ.get("IMGBB_API_KEY")
29
+ if not IMGBB_API_KEY:
30
+ return None
31
+ with open(image_path, "rb") as f:
32
+ import base64
33
+ b64 = base64.b64encode(f.read()).decode()
34
+ resp = requests.post(
35
+ "https://api.imgbb.com/1/upload",
36
+ data={"key": IMGBB_API_KEY, "image": b64}
37
+ )
38
+ if resp.status_code == 200:
39
+ return resp.json()["data"]["url"]
40
+ return None
41
+
42
+
43
  def infer(
44
  image,
45
  prompt,
 
50
  steps,
51
  progress=gr.Progress(track_tqdm=True),
52
  ):
53
+ if image is None or not os.path.exists(image):
54
  print("未上传图片")
55
  return None, seed
56
 
 
 
 
 
57
  if randomize_seed:
58
  seed = random.randint(0, MAX_SEED)
59
 
60
+ # 方案:上传到公开图床 URL 再传给远端
61
+ public_url = upload_to_imgbb(image)
62
+ if not public_url:
63
+ print("图床上传失败,请检查 IMGBB_API_KEY")
64
+ return None, seed
65
+
66
+ print(f"图片公开 URL: {public_url}")
67
 
68
+ # 直接URL 字符串,让远端自己下载
69
+ images_input = [{"image": {"path": None, "url": public_url, "size": None, "orig_name": os.path.basename(image), "mime_type": None, "is_stream": False, "meta": {"_type": "gradio.FileData"}}, "caption": None}]
70
 
71
+ print(f"prompt: {prompt}, lora: {lora_adapter}, seed: {seed}")
 
 
 
 
 
 
 
72
 
73
  try:
74
  result = space_client.predict(
 
82
  api_name="/infer",
83
  )
84
 
85
+ print(f"返回值: {result}")
 
86
  image_info, seed_used = result
 
87
  if isinstance(image_info, dict):
88
  img_out = image_info.get("path") or image_info.get("url")
89
  else:
90
  img_out = image_info
 
91
  return img_out, int(seed_used)
92
 
93
  except Exception as e:
94
  import traceback
95
  traceback.print_exc()
96
+ print(f"异常: {e}")
97
  return None, seed
98
 
99
 
 
108
  with gr.Column(elem_id="col-container"):
109
  gr.Markdown("# 图像编辑 Demo\n基于 prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
110
 
111
+ image = gr.Image(label="上传图片", sources=["upload"], type="filepath")
112
+ prompt = gr.Text(label="编辑描述(Prompt)", placeholder="请输入图片编辑描述...")
113
+ lora_adapter = gr.Dropdown(label="编辑风格(Style)", choices=LORA_STYLES, value="Photo-to-Anime")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  run_button = gr.Button("执行编辑", variant="primary")
115
+ result = gr.Image(label="结果图片")
 
116
 
117
  with gr.Accordion("高级设置", open=False):
118
+ seed = gr.Slider(label="随机种子", minimum=0, maximum=MAX_SEED, step=1, value=0)
 
 
 
 
 
 
119
  randomize_seed = gr.Checkbox(label="随机化种子", value=True)
120
+ guidance_scale = gr.Slider(label="引导强度", minimum=0.1, maximum=10.0, step=0.1, value=1.0)
121
+ steps = gr.Slider(label="推理步数", minimum=1, maximum=50, step=1, value=4)
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  gr.on(
124
  triggers=[run_button.click, prompt.submit],
 
128
  )
129
 
130
  if __name__ == "__main__":
131
+ demo.launch(ssr_mode=False, share=True)
132
+ # ```
133
+
134
+ # 然后去 [imgbb.com](https://imgbb.com/api) 免费申请一个 API key,存到 HF Space 的 Secrets 里,变量名 `IMGBB_API_KEY`。
135
+
136
+ # 这样的流程是:
137
+ # ```
138
+