Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
-
|
| 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)
|