OppaAI commited on
Commit
17438da
·
verified ·
1 Parent(s): 7964d77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -57
app.py CHANGED
@@ -2,67 +2,39 @@ import gradio as gr
2
  import json
3
  import base64
4
  import os
5
- from huggingface_hub import HfApi
6
- import gradio as gr
7
-
8
- # =====================================================
9
- # Settings
10
- # =====================================================
11
- REPO_ID = "OppaAi/Robot-MCP" # 你的 HuggingFace Space repo
12
- REPO_TYPE = "space"
13
- UPLOAD_FILENAME = "tmp.jpg"
14
-
15
- api = HfApi()
16
 
17
- # =====================================================
18
- # Function: Save & Upload Base64 Image
19
- # =====================================================
20
- def save_and_upload_base64_image(base64_str):
21
  try:
22
- # Remove base64 header (if exists)
23
- if "," in base64_str:
24
- base64_str = base64_str.split(",", 1)[1]
25
-
26
- # Decode and save locally
27
- with open(UPLOAD_FILENAME, "wb") as f:
28
- f.write(base64.b64decode(base64_str))
29
-
30
- print("Saved tmp.jpg locally.")
31
-
32
- # Upload to repo so you can see it in Files
33
- api.upload_file(
34
- path_or_fileobj=UPLOAD_FILENAME,
35
- path_in_repo=UPLOAD_FILENAME, # Upload to root folder
36
- repo_id=REPO_ID,
37
- repo_type=REPO_TYPE,
38
- )
39
-
40
- print("Uploaded tmp.jpg to HuggingFace Repo.")
41
- return "Success: tmp.jpg uploaded to HF repo!"
42
 
43
  except Exception as e:
44
- print("Error:", str(e))
45
- return f"Error: {str(e)}"
46
-
47
-
48
- # =====================================================
49
- # Gradio UI
50
- # =====================================================
51
- def handle_input(base64_image):
52
- print("Received base64 image.")
53
- return save_and_upload_base64_image(base64_image)
54
-
55
-
56
- with gr.Blocks() as demo:
57
- gr.Markdown("## 🚀 Base64 → tmp.jpg → Upload to HF Repo")
58
-
59
- base64_input = gr.Textbox(label="Input Base64 Image String")
60
- output_msg = gr.Textbox(label="Result")
61
-
62
- run_btn = gr.Button("Upload")
63
- run_btn.click(handle_input, inputs=base64_input, outputs=output_msg)
64
 
 
 
 
 
 
 
65
 
66
- # Run Gradio
67
  if __name__ == "__main__":
68
- demo.launch()
 
2
  import json
3
  import base64
4
  import os
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ def process(payload: dict):
 
 
 
7
  try:
8
+ image_b64 = payload["image_b64"]
9
+
10
+ # Base64 bytes
11
+ img_bytes = base64.b64decode(image_b64)
12
+
13
+ # Save to local file
14
+ tmp_path = "tmp.jpg"
15
+ with open(tmp_path, "wb") as f:
16
+ f.write(img_bytes)
17
+
18
+ # Check file exists + return success
19
+ if os.path.exists(tmp_path):
20
+ size = os.path.getsize(tmp_path)
21
+ return {
22
+ "saved": True,
23
+ "path": tmp_path,
24
+ "file_size_bytes": size
25
+ }
26
+ else:
27
+ return {"saved": False, "error": "File not found after save."}
28
 
29
  except Exception as e:
30
+ return {"saved": False, "error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ demo = gr.Interface(
33
+ fn=process,
34
+ inputs=gr.JSON(label="Input Payload (Dict format)"),
35
+ outputs=gr.JSON(label="Reply"),
36
+ api_name="predict"
37
+ )
38
 
 
39
  if __name__ == "__main__":
40
+ demo.launch(mcp_server=True)