import gradio as gr import json import base64 import os def process(payload: dict): try: image_b64 = payload["image_b64"] # 直接寫到 space sandbox tmp_path = "/tmp/tmp.jpg" with open(tmp_path, "wb") as f: f.write(base64.b64decode(image_b64)) # 再讀取確認寫入成功 file_size = os.path.getsize(tmp_path) return { "saved": True, "file_path": tmp_path, "file_size_bytes": file_size } except Exception as e: return {"error": str(e)} demo = gr.Interface( fn=process, inputs=gr.JSON(label="Input Payload (Dict format)"), outputs=gr.JSON(label="Reply"), api_name="predict" ) if __name__ == "__main__": demo.launch(mcp_server=True)