Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import json | |
| import base64 | |
| import os | |
| from huggingface_hub import HfApi | |
| import gradio as gr | |
| # ===================================================== | |
| # Settings | |
| # ===================================================== | |
| REPO_ID = "OppaAi/Robot-MCP" # δ½ η HuggingFace Space repo | |
| REPO_TYPE = "space" | |
| UPLOAD_FILENAME = "tmp.jpg" | |
| api = HfApi() | |
| # ===================================================== | |
| # Function: Save & Upload Base64 Image | |
| # ===================================================== | |
| def save_and_upload_base64_image(base64_str): | |
| try: | |
| # Remove base64 header (if exists) | |
| if "," in base64_str: | |
| base64_str = base64_str.split(",", 1)[1] | |
| # Decode and save locally | |
| with open(UPLOAD_FILENAME, "wb") as f: | |
| f.write(base64.b64decode(base64_str)) | |
| print("Saved tmp.jpg locally.") | |
| # Upload to repo so you can see it in Files | |
| api.upload_file( | |
| path_or_fileobj=UPLOAD_FILENAME, | |
| path_in_repo=UPLOAD_FILENAME, # Upload to root folder | |
| repo_id=REPO_ID, | |
| repo_type=REPO_TYPE, | |
| ) | |
| print("Uploaded tmp.jpg to HuggingFace Repo.") | |
| return "Success: tmp.jpg uploaded to HF repo!" | |
| except Exception as e: | |
| print("Error:", str(e)) | |
| return f"Error: {str(e)}" | |
| # ===================================================== | |
| # Gradio UI | |
| # ===================================================== | |
| def handle_input(base64_image): | |
| print("Received base64 image.") | |
| return save_and_upload_base64_image(base64_image) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π Base64 β tmp.jpg β Upload to HF Repo") | |
| base64_input = gr.Textbox(label="Input Base64 Image String") | |
| output_msg = gr.Textbox(label="Result") | |
| run_btn = gr.Button("Upload") | |
| run_btn.click(handle_input, inputs=base64_input, outputs=output_msg) | |
| # Run Gradio | |
| if __name__ == "__main__": | |
| demo.launch() |