kenfoo commited on
Commit
3d43976
·
verified ·
1 Parent(s): 3062245

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -61
app.py CHANGED
@@ -2,10 +2,10 @@ import gradio as gr
2
  from gradio_client import Client
3
  import random
4
  import os
5
- import requests
 
6
 
7
  HF_TOKEN = os.environ.get("girlToken")
8
- TARGET_SPACE_URL = "https://prithivmlmods-qwen-image-edit-2511-loras-fast.hf.space"
9
 
10
  space_client = Client(
11
  "prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast",
@@ -23,36 +23,28 @@ LORA_STYLES = [
23
  MAX_SEED = 2**31 - 1
24
 
25
 
26
- def upload_file_to_space(local_path):
27
- """手动上传文件到目标 Space返回远端路径"""
28
- upload_url = f"{TARGET_SPACE_URL}/upload"
29
-
30
- headers = {}
31
- if HF_TOKEN:
32
- headers["Authorization"] = f"Bearer {HF_TOKEN}"
33
-
34
- mime_type = "image/jpeg"
35
- if local_path.lower().endswith(".png"):
36
- mime_type = "image/png"
37
- elif local_path.lower().endswith(".webp"):
38
- mime_type = "image/webp"
39
 
40
  with open(local_path, "rb") as f:
41
- response = requests.post(
42
- upload_url,
43
- headers=headers,
44
- files={"files": (os.path.basename(local_path), f, mime_type)},
45
- )
46
 
47
- print(f"上传状态码: {response.status_code}")
48
- print(f"上传响应: {response.text}")
49
-
50
- if response.status_code == 200:
51
- result = response.json()
52
- remote_path = result[0] if isinstance(result, list) else result
53
- return remote_path
54
- else:
55
- raise Exception(f"上传失败: {response.status_code} {response.text}")
 
 
 
 
56
 
57
 
58
  def infer(
@@ -65,45 +57,18 @@ def infer(
65
  steps,
66
  progress=gr.Progress(track_tqdm=True),
67
  ):
68
- if image is None:
69
- print("未上传图片")
70
- return None, seed
71
-
72
- if not os.path.exists(image):
73
- print(f"图片路径不存在: {image}")
74
  return None, seed
75
 
76
  if randomize_seed:
77
  seed = random.randint(0, MAX_SEED)
78
 
79
- try:
80
- remote_path = upload_file_to_space(image)
81
- print(f"远端路径: {remote_path}")
82
- except Exception as e:
83
- print(f"上传图片失败: {e}")
84
- return None, seed
85
-
86
- mime_type = "image/jpeg"
87
- if image.lower().endswith(".png"):
88
- mime_type = "image/png"
89
- elif image.lower().endswith(".webp"):
90
- mime_type = "image/webp"
91
-
92
- images_input = [{
93
- "image": {
94
- "path": remote_path,
95
- "url": f"{TARGET_SPACE_URL}/file={remote_path}",
96
- "size": os.path.getsize(image),
97
- "orig_name": os.path.basename(image),
98
- "mime_type": mime_type,
99
- "is_stream": False,
100
- "meta": {}
101
- },
102
- "caption": None
103
- }]
104
 
105
  print("[调用API] 输入参数:")
106
- print(f" remote_path: {remote_path}")
 
107
  print(f" prompt: {prompt}")
108
  print(f" lora_adapter: {lora_adapter}")
109
  print(f" seed: {seed}")
 
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",
 
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(
 
57
  steps,
58
  progress=gr.Progress(track_tqdm=True),
59
  ):
60
+ if image is None or not os.path.exists(image):
61
+ print("未上传图片或路径不存在")
 
 
 
 
62
  return None, seed
63
 
64
  if randomize_seed:
65
  seed = random.randint(0, MAX_SEED)
66
 
67
+ images_input = [image_to_gallery_item(image)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  print("[调用API] 输入参数:")
70
+ print(f" orig_name: {os.path.basename(image)}")
71
+ print(f" mime_type: {mimetypes.guess_type(image)[0]}")
72
  print(f" prompt: {prompt}")
73
  print(f" lora_adapter: {lora_adapter}")
74
  print(f" seed: {seed}")