OppaAI commited on
Commit
7964d77
·
verified ·
1 Parent(s): 3be9d33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -47
app.py CHANGED
@@ -2,64 +2,67 @@ import gradio as gr
2
  import json
3
  import base64
4
  import os
5
- from huggingface_hub import upload_file
 
6
 
7
- # --- Configuration for Hugging Face Hub Upload ---
8
- # The HF_TOKEN secret is automatically loaded into the environment by Hugging Face Spaces
9
- HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
 
 
 
10
 
11
- # !! REPLACE THIS with your actual dataset repo ID (e.g., "Wauplin/my-image-data") !!
12
- HF_DATASET_REPO = "OppaAI/ROBOT_MCP"
13
 
14
- def process_and_upload(payload: dict):
15
- if not HF_TOKEN:
16
- return {"error": "HF_TOKEN secret not found in Space settings."}
17
-
18
  try:
19
- image_b64 = payload["image_b64"]
20
- image_bytes = base64.b64decode(image_b64)
 
21
 
22
- # 1. Save temporarily to the local ephemeral storage (/tmp) first
23
- # This file will be immediately deleted after the function finishes or the space restarts
24
- local_tmp_path = "/tmp/uploaded_image.jpg"
25
- with open(local_tmp_path, "wb") as f:
26
- f.write(image_bytes)
27
 
28
- # 2. Upload the temporary file to the persistent Hugging Face Dataset Repo
29
- # The path in the repo can be dynamic, e.g., using a timestamp
30
- path_in_repo = f"images/uploaded_image_{len(image_bytes)}.jpg"
31
-
32
- upload_file(
33
- path_or_fileobj=local_tmp_path,
34
- path_in_repo=path_in_repo,
35
- repo_id=HF_DATASET_REPO,
36
- token=HF_TOKEN,
37
- repo_type="dataset",
38
- )
39
 
40
- # 3. Clean up the local temporary file
41
- os.remove(local_tmp_path)
 
 
 
 
 
42
 
43
- # 4. Return success message
44
- return {
45
- "saved_to_hf_hub": True,
46
- "repo_id": HF_DATASET_REPO,
47
- "path_in_repo": path_in_repo,
48
- "file_size_bytes": len(image_bytes)
49
- }
50
 
51
  except Exception as e:
52
- # Check the HF Space logs for full traceback if an error occurs
53
- print(f"Upload failed: {e}")
54
- return {"error": f"Failed to upload to HF Hub: {str(e)}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
 
 
56
 
57
- demo = gr.Interface(
58
- fn=process_and_upload,
59
- inputs=gr.JSON(label="Input Payload (Dict format with 'image_b64')"),
60
- outputs=gr.JSON(label="Reply"),
61
- api_name="predict"
62
- )
63
 
 
64
  if __name__ == "__main__":
65
- demo.launch(mcp_server=True)
 
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()