OppaAI commited on
Commit
d2ce059
·
verified ·
1 Parent(s): 920ee9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -4,9 +4,11 @@ from PIL import Image
4
  import io
5
  import json
6
 
7
- def process(json_input):
 
8
  try:
9
- data = json_input
 
10
 
11
  # decode base64 image
12
  img_bytes = base64.b64decode(data["image_b64"])
@@ -19,19 +21,23 @@ def process(json_input):
19
  "size": img.size
20
  }
21
 
22
- # return only the msg: (Jetson JSON)
23
- return reply
 
 
24
 
25
  except Exception as e:
 
26
  return None, {"error": str(e)}
27
 
28
 
29
  demo = gr.Interface(
30
  fn=process,
31
- inputs=gr.JSON(label="Jetson JSON"),
 
32
  outputs=[
33
- gr.Image(type="pil", label="Image Preview"),
34
- gr.JSON(label="Reply to Jetson")
35
  ],
36
  api_name="predict"
37
  )
 
4
  import io
5
  import json
6
 
7
+ # 修改函式以確保它接收一個字典(這是 gradio_client 預設發送的格式)
8
+ def process(payload):
9
  try:
10
+ # 如果客戶端已經傳送字典,直接使用 payload:
11
+ data = payload
12
 
13
  # decode base64 image
14
  img_bytes = base64.b64decode(data["image_b64"])
 
21
  "size": img.size
22
  }
23
 
24
+ # *** 關鍵修改:回傳一個包含圖片和 JSON 回覆的元組 (tuple) ***
25
+ # Gradio 會自動將第一個值賦給第一個輸出元件 (gr.Image)
26
+ # 第二個值賦給第二個輸出元件 (gr.JSON)
27
+ return img, reply
28
 
29
  except Exception as e:
30
+ # 發生錯誤時,確保回傳兩個值,其中圖片值為 None
31
  return None, {"error": str(e)}
32
 
33
 
34
  demo = gr.Interface(
35
  fn=process,
36
+ # 我們將輸入定義為 JSON,這允許後端接收字典格式
37
+ inputs=gr.JSON(label="Input Payload (Dict format)"),
38
  outputs=[
39
+ gr.Image(type="pil", label="Image Preview"), # 現在將接收 img 物件
40
+ gr.JSON(label="Reply to Jetson") # 現在將接收 reply 字典
41
  ],
42
  api_name="predict"
43
  )