Spaces:
Sleeping
Sleeping
import gradio as gr | |
import json | |
import time | |
status_file = "status.json" | |
command_file = "command.json" | |
output_file = "output.json" | |
HEARTBEAT_TIMEOUT = 20 # seconds | |
# Initialize storage files | |
with open(status_file, "w") as f: | |
json.dump({}, f) | |
with open(command_file, "w") as f: | |
json.dump({"command": ""}, f) | |
with open(output_file, "w") as f: | |
json.dump({"output": ""}, f) | |
# Register PC as online | |
def register_pc(name, password): | |
with open(status_file, "r") as f: | |
status = json.load(f) | |
status[name] = {"password": password, "timestamp": time.time()} | |
with open(status_file, "w") as f: | |
json.dump(status, f) | |
return f"PC '{name}' registered as online." | |
# Check if PC is online | |
def check_pc(name): | |
with open(status_file, "r") as f: | |
status = json.load(f) | |
if name in status and time.time() - status[name]["timestamp"] < HEARTBEAT_TIMEOUT: | |
return "Online" | |
return "Offline" | |
# Send command (or register if command is "REGISTER") | |
def send_command(name, password, command): | |
if command == "REGISTER": | |
return register_pc(name, password) | |
with open(status_file, "r") as f: | |
status = json.load(f) | |
if name in status and status[name]["password"] == password: | |
with open(command_file, "w") as f: | |
json.dump({"command": command}, f) | |
return f"Command sent to {name}." | |
return "Authentication failed." | |
# Get command (for PC side polling) | |
def get_command(): | |
with open(command_file, "r") as f: | |
cmd_data = json.load(f) | |
return cmd_data.get("command", "") | |
# Upload command output from PC | |
def upload_output(name, password, output): | |
with open(status_file, "r") as f: | |
status = json.load(f) | |
if name in status and status[name]["password"] == password: | |
with open(output_file, "w") as f: | |
json.dump({"output": output}, f) | |
return "Output uploaded successfully." | |
return "Authentication failed." | |
# Retrieve command output (to view on Hugging Face) | |
def get_output(): | |
with open(output_file, "r") as f: | |
data = json.load(f) | |
return data.get("output", "") | |
with gr.Blocks() as ui: | |
with gr.Row(): | |
name_input = gr.Textbox(label="PC Name") | |
pass_input = gr.Textbox(label="Password", type="password") | |
check_btn = gr.Button("Check Online Status") | |
check_output = gr.Textbox(label="Status") | |
check_btn.click(check_pc, inputs=name_input, outputs=check_output) | |
with gr.Row(): | |
cmd_input = gr.Textbox(label="Command", lines=5) # Multi-line for larger scripts | |
send_btn = gr.Button("Send Command") | |
send_output = gr.Textbox(label="Response", lines=2) | |
send_btn.click(send_command, inputs=[name_input, pass_input, cmd_input], outputs=send_output) | |
# Hidden endpoints for PC polling and uploading output: | |
hidden_get_cmd_btn = gr.Button("Hidden Get Command", visible=False) | |
hidden_cmd_output = gr.Textbox(visible=False) | |
hidden_get_cmd_btn.click(get_command, inputs=[], outputs=hidden_cmd_output, api_name="/get_command") | |
output_input = gr.Textbox(label="Output", visible=False) | |
hidden_upload_btn = gr.Button("Hidden Upload Output", visible=False) | |
hidden_upload_output = gr.Textbox(visible=False) | |
hidden_upload_btn.click(upload_output, inputs=[name_input, pass_input, output_input], outputs=hidden_upload_output, api_name="/upload_output") | |
hidden_get_output_btn = gr.Button("Hidden Get Output", visible=False) | |
hidden_get_output = gr.Textbox(visible=False) | |
hidden_get_output_btn.click(get_output, inputs=[], outputs=hidden_get_output, api_name="/get_output") | |
ui.launch(share=True) | |